View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.tcp.perf;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.buffer.IoBuffer;
25  import org.apache.mina.core.future.ConnectFuture;
26  import org.apache.mina.core.service.IoConnector;
27  import org.apache.mina.core.service.IoHandlerAdapter;
28  import org.apache.mina.core.session.IdleStatus;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.transport.socket.nio.NioSocketConnector;
31  
32  /**
33   * An UDP client taht just send thousands of small messages to a UdpServer. 
34   * 
35   * This class is used for performance test purposes. It does nothing at all, but send a message
36   * repetitly to a server.
37   * 
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class TcpClient extends IoHandlerAdapter {
41      /** The connector */
42      private IoConnector connector;
43  
44      /** The session */
45      private static IoSession session;
46  
47      private boolean received = false;
48  
49      /**
50       * Create the UdpClient's instance
51       */
52      public TcpClient() {
53          connector = new NioSocketConnector();
54  
55          connector.setHandler(this);
56          ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", TcpServer.PORT));
57  
58          connFuture.awaitUninterruptibly();
59  
60          session = connFuture.getSession();
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
68          cause.printStackTrace();
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public void messageReceived(IoSession session, Object message) throws Exception {
76          received = true;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public void messageSent(IoSession session, Object message) throws Exception {
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void sessionClosed(IoSession session) throws Exception {
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public void sessionCreated(IoSession session) throws Exception {
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void sessionOpened(IoSession session) throws Exception {
112     }
113 
114     /**
115      * The main method : instanciates a client, and send N messages. We sleep 
116      * between each K messages sent, to avoid the server saturation.
117      * @param args
118      * @throws Exception
119      */
120     public static void main(String[] args) throws Exception {
121         TcpClient client = new TcpClient();
122 
123         long t0 = System.currentTimeMillis();
124 
125         for (int i = 0; i <= TcpServer.MAX_RECEIVED; i++) {
126             //if (i % 2 == 0) {
127             //Thread.sleep(1);
128             //}
129 
130             IoBuffer buffer = IoBuffer.allocate(4);
131             buffer.putInt(i);
132             buffer.flip();
133             session.write(buffer);
134 
135             while (client.received == false) {
136                 Thread.sleep(1);
137             }
138 
139             client.received = false;
140 
141             if (i % 10000 == 0) {
142                 System.out.println("Sent " + i + " messages");
143             }
144         }
145 
146         long t1 = System.currentTimeMillis();
147 
148         System.out.println("Sent messages delay : " + (t1 - t0));
149 
150         Thread.sleep(100000);
151 
152         client.connector.dispose(true);
153     }
154 }