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