tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 1 | package org.onlab.nio; |
| 2 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 3 | import com.google.common.collect.Lists; |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 4 | import org.onlab.util.Counter; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 5 | import org.slf4j.Logger; |
| 6 | import org.slf4j.LoggerFactory; |
| 7 | |
| 8 | import java.io.IOException; |
| 9 | import java.net.InetAddress; |
| 10 | import java.net.InetSocketAddress; |
| 11 | import java.net.Socket; |
| 12 | import java.net.SocketAddress; |
| 13 | import java.nio.channels.ByteChannel; |
| 14 | import java.nio.channels.ServerSocketChannel; |
| 15 | import java.nio.channels.SocketChannel; |
| 16 | import java.text.DecimalFormat; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | import java.util.concurrent.ExecutorService; |
| 20 | import java.util.concurrent.Executors; |
| 21 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 22 | import static java.lang.String.format; |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 23 | import static java.lang.System.currentTimeMillis; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 24 | import static java.lang.System.out; |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 25 | import static org.onlab.util.Tools.delay; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 26 | import static org.onlab.util.Tools.namedThreads; |
| 27 | |
| 28 | /** |
| 29 | * Auxiliary test fixture to measure speed of NIO-based channels. |
| 30 | */ |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 31 | public class IOLoopTestServer { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 32 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 33 | private static Logger log = LoggerFactory.getLogger(IOLoopTestServer.class); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 34 | |
| 35 | private static final int PRUNE_FREQUENCY = 1000; |
| 36 | |
| 37 | static final int PORT = 9876; |
| 38 | static final long TIMEOUT = 1000; |
| 39 | |
| 40 | static final boolean SO_NO_DELAY = false; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 41 | static final int SO_SEND_BUFFER_SIZE = 128 * 1024; |
| 42 | static final int SO_RCV_BUFFER_SIZE = 128 * 1024; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 43 | |
| 44 | static final DecimalFormat FORMAT = new DecimalFormat("#,##0"); |
| 45 | |
| 46 | private final AcceptorLoop aloop; |
| 47 | private final ExecutorService apool = Executors.newSingleThreadExecutor(namedThreads("accept")); |
| 48 | |
| 49 | private final List<CustomIOLoop> iloops = new ArrayList<>(); |
| 50 | private final ExecutorService ipool; |
| 51 | |
| 52 | private final int workerCount; |
| 53 | private final int msgLength; |
| 54 | private int lastWorker = -1; |
| 55 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 56 | Counter messages; |
| 57 | Counter bytes; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Main entry point to launch the server. |
| 61 | * |
| 62 | * @param args command-line arguments |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 63 | * @throws java.io.IOException if unable to crate IO loops |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 64 | */ |
| 65 | public static void main(String[] args) throws IOException { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 66 | startStandalone(args); |
| 67 | System.exit(0); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Starts a standalone IO loop test server. |
| 72 | * |
| 73 | * @param args command-line arguments |
| 74 | */ |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 75 | public static void startStandalone(String[] args) throws IOException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 76 | InetAddress ip = InetAddress.getByName(args.length > 0 ? args[0] : "127.0.0.1"); |
| 77 | int wc = args.length > 1 ? Integer.parseInt(args[1]) : 6; |
| 78 | int ml = args.length > 2 ? Integer.parseInt(args[2]) : 128; |
| 79 | |
| 80 | log.info("Setting up the server with {} workers, {} byte messages on {}... ", |
| 81 | wc, ml, ip); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 82 | IOLoopTestServer server = new IOLoopTestServer(ip, wc, ml, PORT); |
| 83 | server.start(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 84 | |
| 85 | // Start pruning clients. |
| 86 | while (true) { |
| 87 | delay(PRUNE_FREQUENCY); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 88 | server.prune(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a speed server. |
| 94 | * |
| 95 | * @param ip optional ip of the adapter where to bind |
| 96 | * @param wc worker count |
| 97 | * @param ml message length in bytes |
| 98 | * @param port listen port |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 99 | * @throws java.io.IOException if unable to create IO loops |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 100 | */ |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 101 | public IOLoopTestServer(InetAddress ip, int wc, int ml, int port) throws IOException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 102 | this.workerCount = wc; |
| 103 | this.msgLength = ml; |
| 104 | this.ipool = Executors.newFixedThreadPool(workerCount, namedThreads("io-loop")); |
| 105 | |
| 106 | this.aloop = new CustomAcceptLoop(new InetSocketAddress(ip, port)); |
| 107 | for (int i = 0; i < workerCount; i++) { |
| 108 | iloops.add(new CustomIOLoop()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Start the server IO loops and kicks off throughput tracking. |
| 114 | */ |
| 115 | public void start() { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 116 | messages = new Counter(); |
| 117 | bytes = new Counter(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 118 | |
| 119 | for (CustomIOLoop l : iloops) { |
| 120 | ipool.execute(l); |
| 121 | } |
| 122 | apool.execute(aloop); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 123 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 124 | for (CustomIOLoop l : iloops) { |
| 125 | l.awaitStart(TIMEOUT); |
| 126 | } |
| 127 | aloop.awaitStart(TIMEOUT); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Stop the server IO loops and freezes throughput tracking. |
| 132 | */ |
| 133 | public void stop() { |
| 134 | aloop.shutdown(); |
| 135 | for (CustomIOLoop l : iloops) { |
| 136 | l.shutdown(); |
| 137 | } |
| 138 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 139 | for (CustomIOLoop l : iloops) { |
| 140 | l.awaitStop(TIMEOUT); |
| 141 | } |
| 142 | aloop.awaitStop(TIMEOUT); |
| 143 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 144 | messages.freeze(); |
| 145 | bytes.freeze(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Reports on the accumulated throughput trackers. |
| 150 | */ |
| 151 | public void report() { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 152 | DecimalFormat f = new DecimalFormat("#,##0"); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 153 | out.println(format("Server: %s messages; %s bytes; %s mps; %s Mbs", |
| 154 | f.format(messages.total()), f.format(bytes.total()), |
| 155 | f.format(messages.throughput()), |
| 156 | f.format(bytes.throughput() / (1024 * msgLength)))); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Prunes the IO loops of stale message buffers. |
| 161 | */ |
| 162 | public void prune() { |
| 163 | for (CustomIOLoop l : iloops) { |
| 164 | l.pruneStaleStreams(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Get the next worker to which a client should be assigned |
| 169 | private synchronized CustomIOLoop nextWorker() { |
| 170 | lastWorker = (lastWorker + 1) % workerCount; |
| 171 | return iloops.get(lastWorker); |
| 172 | } |
| 173 | |
| 174 | // Loop for transfer of fixed-length messages |
| 175 | private class CustomIOLoop extends IOLoop<TestMessage, TestMessageStream> { |
| 176 | |
| 177 | public CustomIOLoop() throws IOException { |
| 178 | super(500); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected TestMessageStream createStream(ByteChannel channel) { |
| 183 | return new TestMessageStream(msgLength, channel, this); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | protected void removeStream(MessageStream<TestMessage> stream) { |
| 188 | super.removeStream(stream); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 189 | |
| 190 | messages.add(stream.messagesIn().total()); |
| 191 | bytes.add(stream.bytesIn().total()); |
| 192 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 193 | // out.println(format("Disconnected server; inbound %s mps, %s Mbps; outbound %s mps, %s Mbps", |
| 194 | // FORMAT.format(stream.messagesIn().throughput()), |
| 195 | // FORMAT.format(stream.bytesIn().throughput() / (1024 * msgLength)), |
| 196 | // FORMAT.format(stream.messagesOut().throughput()), |
| 197 | // FORMAT.format(stream.bytesOut().throughput() / (1024 * msgLength)))); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | protected void processMessages(List<TestMessage> messages, |
| 202 | MessageStream<TestMessage> stream) { |
| 203 | try { |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 204 | stream.write(createResponses(messages)); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 205 | } catch (IOException e) { |
| 206 | log.error("Unable to echo messages", e); |
| 207 | } |
| 208 | } |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 209 | |
| 210 | private List<TestMessage> createResponses(List<TestMessage> messages) { |
| 211 | List<TestMessage> responses = Lists.newArrayListWithCapacity(messages.size()); |
| 212 | for (TestMessage message : messages) { |
| 213 | responses.add(new TestMessage(message.length(), message.requestorTime(), |
| 214 | currentTimeMillis(), message.padding())); |
| 215 | } |
| 216 | return responses; |
| 217 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Loop for accepting client connections |
| 221 | private class CustomAcceptLoop extends AcceptorLoop { |
| 222 | |
| 223 | public CustomAcceptLoop(SocketAddress address) throws IOException { |
| 224 | super(500, address); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | protected void acceptConnection(ServerSocketChannel channel) throws IOException { |
| 229 | SocketChannel sc = channel.accept(); |
| 230 | sc.configureBlocking(false); |
| 231 | |
| 232 | Socket so = sc.socket(); |
| 233 | so.setTcpNoDelay(SO_NO_DELAY); |
| 234 | so.setReceiveBufferSize(SO_RCV_BUFFER_SIZE); |
| 235 | so.setSendBufferSize(SO_SEND_BUFFER_SIZE); |
| 236 | |
| 237 | nextWorker().acceptStream(sc); |
| 238 | log.info("Connected client"); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | } |