001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.example.udp.perf; 021 022import java.net.InetSocketAddress; 023 024import org.apache.mina.core.buffer.IoBuffer; 025import org.apache.mina.core.future.ConnectFuture; 026import org.apache.mina.core.service.IoConnector; 027import org.apache.mina.core.service.IoHandlerAdapter; 028import org.apache.mina.core.session.IdleStatus; 029import org.apache.mina.core.session.IoSession; 030import org.apache.mina.transport.socket.nio.NioDatagramConnector; 031 032/** 033 * An UDP client taht just send thousands of small messages to a UdpServer. 034 * 035 * This class is used for performance test purposes. It does nothing at all, but send a message 036 * repetitly to a server. 037 * 038 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 039 */ 040public class UdpClient extends IoHandlerAdapter { 041 /** The connector */ 042 private IoConnector connector; 043 044 /** The session */ 045 private static IoSession session; 046 047 /** 048 * Create the UdpClient's instance 049 */ 050 public UdpClient() { 051 connector = new NioDatagramConnector(); 052 053 connector.setHandler(this); 054 055 ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", UdpServer.PORT)); 056 057 connFuture.awaitUninterruptibly(); 058 059 session = connFuture.getSession(); 060 } 061 062 /** 063 * {@inheritDoc} 064 */ 065 @Override 066 public void exceptionCaught(IoSession session, Throwable cause) throws Exception { 067 cause.printStackTrace(); 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 @Override 074 public void messageReceived(IoSession session, Object message) throws Exception { 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 @Override 081 public void messageSent(IoSession session, Object message) throws Exception { 082 } 083 084 /** 085 * {@inheritDoc} 086 */ 087 @Override 088 public void sessionClosed(IoSession session) throws Exception { 089 } 090 091 /** 092 * {@inheritDoc} 093 */ 094 @Override 095 public void sessionCreated(IoSession session) throws Exception { 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 public void sessionIdle(IoSession session, IdleStatus status) throws Exception { 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public void sessionOpened(IoSession session) throws Exception { 110 } 111 112 /** 113 * The main method : instanciates a client, and send N messages. We sleep 114 * between each K messages sent, to avoid the server saturation. 115 * @param args The arguments 116 * @throws Exception If something went wrong 117 */ 118 public static void main(String[] args) throws Exception { 119 UdpClient client = new UdpClient(); 120 121 long t0 = System.currentTimeMillis(); 122 123 for (int i = 0; i <= UdpServer.MAX_RECEIVED; i++) { 124 Thread.sleep(1); 125 126 String str = Integer.toString(i); 127 byte[] data = str.getBytes(); 128 IoBuffer buffer = IoBuffer.allocate(data.length); 129 buffer.put(data); 130 buffer.flip(); 131 session.write(buffer); 132 133 if (i % 10000 == 0) { 134 System.out.println("Sent " + i + " messages"); 135 } 136 } 137 138 long t1 = System.currentTimeMillis(); 139 140 System.out.println("Sent messages delay : " + (t1 - t0)); 141 142 Thread.sleep(100000); 143 144 client.connector.dispose(true); 145 } 146}