|
1 | 1 | package echo;
|
2 | 2 |
|
3 | 3 | import java.net.InetSocketAddress;
|
| 4 | +import java.util.concurrent.CountDownLatch; |
| 5 | +import java.util.concurrent.Executors; |
| 6 | + |
| 7 | +import org.jboss.netty.channel.ChannelFactory; |
| 8 | +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; |
| 9 | + |
| 10 | +import com.google.protobuf.ServiceException; |
4 | 11 |
|
5 | 12 | import muduo.rpc.RpcChannel;
|
6 | 13 | import muduo.rpc.RpcClient;
|
|
10 | 17 | import echo.EchoProto.EchoService.BlockingInterface;
|
11 | 18 |
|
12 | 19 | public class EchoClient {
|
| 20 | + static final int kRequests = 20000; |
| 21 | + |
| 22 | + public static class Client implements Runnable { |
| 23 | + private ChannelFactory channelFactory; |
| 24 | + private InetSocketAddress serverAddr; |
| 25 | + private CountDownLatch latch; |
| 26 | + |
| 27 | + public Client(ChannelFactory channelFactory, InetSocketAddress server, CountDownLatch latch) { |
| 28 | + this.channelFactory = channelFactory; |
| 29 | + this.serverAddr = server; |
| 30 | + this.latch = latch; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void run() { |
| 35 | + System.out.println(Thread.currentThread()); |
| 36 | + RpcClient client = new RpcClient(channelFactory); |
| 37 | + RpcChannel channel = client.blockingConnect(serverAddr); |
| 38 | + BlockingInterface remoteService = EchoService.newBlockingStub(channel); |
| 39 | + String payload = new String(new byte[100]); |
| 40 | + payload = "Hello"; |
| 41 | + EchoRequest request = EchoRequest.newBuilder().setPayload(payload).build(); |
| 42 | + |
| 43 | + for (int i = 0; i < kRequests; ++i) { |
| 44 | + EchoResponse response; |
| 45 | + try { |
| 46 | + response = remoteService.echo(null, request); |
| 47 | + assert response.getPayload().equals(payload); |
| 48 | + } catch (ServiceException e) { |
| 49 | + // TODO Auto-generated catch block |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + // System.out.println(response); |
| 53 | + } |
| 54 | + latch.countDown(); |
| 55 | + System.out.println(Thread.currentThread()); |
| 56 | + // System.out.println(response); |
| 57 | + channel.disconnect(); |
| 58 | + // client.stop(); |
| 59 | + } |
| 60 | + } |
13 | 61 |
|
14 | 62 | public static void main(String[] args) throws Exception {
|
15 |
| - RpcClient client = new RpcClient(); |
16 |
| - RpcChannel channel = client.blockingConnect(new InetSocketAddress(args[0], 8888)); |
17 |
| - BlockingInterface remoteService = EchoService.newBlockingStub(channel); |
18 |
| - String payload = new String(new byte[100]); |
19 |
| - payload = "Hello"; |
20 |
| - EchoRequest request = EchoRequest.newBuilder().setPayload(payload).build(); |
| 63 | + ChannelFactory channelFactory = new NioClientSocketChannelFactory( |
| 64 | + Executors.newCachedThreadPool(), |
| 65 | + Executors.newCachedThreadPool()); |
| 66 | + InetSocketAddress server = new InetSocketAddress(args[0], 8888); |
| 67 | + int N = 4; |
| 68 | + CountDownLatch latch = new CountDownLatch(N); |
21 | 69 | long start = System.currentTimeMillis();
|
22 |
| - int N = 20000; |
| 70 | + Thread[] threads = new Thread[N]; |
23 | 71 | for (int i = 0; i < N; ++i) {
|
24 |
| - EchoResponse response = remoteService.echo(null, request); |
25 |
| - assert response.getPayload().equals(payload); |
26 |
| - //System.out.println(response); |
| 72 | + threads[i] = new Thread(new Client(channelFactory, server, latch)); |
| 73 | + threads[i].start(); |
27 | 74 | }
|
| 75 | + latch.await(); |
28 | 76 | long end = System.currentTimeMillis();
|
29 |
| - System.err.println(end-start); |
30 |
| - System.err.println(N*1000L/(end-start)); |
31 |
| - // System.out.println(response); |
32 |
| - channel.disconnect(); |
33 |
| - client.stop(); |
| 77 | + System.err.println(end - start); |
| 78 | + System.err.println(N * kRequests * 1000L / (end - start)); |
34 | 79 | }
|
35 | 80 |
|
36 | 81 | }
|
0 commit comments