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.SocketAddress; |
| 12 | import java.nio.channels.ByteChannel; |
| 13 | import java.nio.channels.SelectionKey; |
| 14 | import java.nio.channels.SocketChannel; |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 15 | import java.text.DecimalFormat; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | import java.util.concurrent.ExecutionException; |
| 19 | import java.util.concurrent.ExecutorService; |
| 20 | import java.util.concurrent.Executors; |
| 21 | import java.util.concurrent.FutureTask; |
| 22 | import java.util.concurrent.Semaphore; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.concurrent.TimeoutException; |
| 25 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 26 | import static java.lang.String.format; |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame] | 27 | import static java.lang.System.nanoTime; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 28 | import static java.lang.System.out; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 29 | import static org.onlab.nio.IOLoopTestServer.PORT; |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 30 | import static org.onlab.util.Tools.delay; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 31 | import static org.onlab.util.Tools.namedThreads; |
| 32 | |
| 33 | /** |
| 34 | * Auxiliary test fixture to measure speed of NIO-based channels. |
| 35 | */ |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 36 | public class IOLoopTestClient { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 37 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 38 | private static Logger log = LoggerFactory.getLogger(IOLoopTestClient.class); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 39 | |
| 40 | private final InetAddress ip; |
| 41 | private final int port; |
| 42 | private final int msgCount; |
| 43 | private final int msgLength; |
| 44 | |
| 45 | private final List<CustomIOLoop> iloops = new ArrayList<>(); |
| 46 | private final ExecutorService ipool; |
| 47 | private final ExecutorService wpool; |
| 48 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 49 | Counter messages; |
| 50 | Counter bytes; |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 51 | long latencyTotal = 0; |
| 52 | long latencyCount = 0; |
| 53 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Main entry point to launch the client. |
| 57 | * |
| 58 | * @param args command-line arguments |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 59 | * @throws java.io.IOException if unable to connect to server |
| 60 | * @throws InterruptedException if latch wait gets interrupted |
| 61 | * @throws java.util.concurrent.ExecutionException if wait gets interrupted |
| 62 | * @throws java.util.concurrent.TimeoutException if timeout occurred while waiting for completion |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 63 | */ |
| 64 | public static void main(String[] args) |
| 65 | throws IOException, InterruptedException, ExecutionException, TimeoutException { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 66 | startStandalone(args); |
| 67 | |
| 68 | System.exit(0); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Starts a standalone IO loop test client. |
| 73 | * |
| 74 | * @param args command-line arguments |
| 75 | */ |
| 76 | public static void startStandalone(String[] args) |
| 77 | throws IOException, InterruptedException, ExecutionException, TimeoutException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 78 | InetAddress ip = InetAddress.getByName(args.length > 0 ? args[0] : "127.0.0.1"); |
| 79 | int wc = args.length > 1 ? Integer.parseInt(args[1]) : 6; |
| 80 | int mc = args.length > 2 ? Integer.parseInt(args[2]) : 50 * 1000000; |
| 81 | int ml = args.length > 3 ? Integer.parseInt(args[3]) : 128; |
tom | 14dc4d0 | 2014-09-26 12:43:14 -0700 | [diff] [blame] | 82 | int to = args.length > 4 ? Integer.parseInt(args[4]) : 60; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 83 | |
| 84 | log.info("Setting up client with {} workers sending {} {}-byte messages to {} server... ", |
| 85 | wc, mc, ml, ip); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 86 | IOLoopTestClient client = new IOLoopTestClient(ip, wc, mc, ml, PORT); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 87 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 88 | client.start(); |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 89 | delay(500); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 90 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 91 | client.await(to); |
| 92 | client.report(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a speed client. |
| 97 | * |
| 98 | * @param ip ip address of server |
| 99 | * @param wc worker count |
| 100 | * @param mc message count to send per client |
| 101 | * @param ml message length in bytes |
| 102 | * @param port socket port |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 103 | * @throws java.io.IOException if unable to create IO loops |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 104 | */ |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 105 | public IOLoopTestClient(InetAddress ip, int wc, int mc, int ml, int port) throws IOException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 106 | this.ip = ip; |
| 107 | this.port = port; |
| 108 | this.msgCount = mc; |
| 109 | this.msgLength = ml; |
| 110 | this.wpool = Executors.newFixedThreadPool(wc, namedThreads("worker")); |
| 111 | this.ipool = Executors.newFixedThreadPool(wc, namedThreads("io-loop")); |
| 112 | |
| 113 | for (int i = 0; i < wc; i++) { |
| 114 | iloops.add(new CustomIOLoop()); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Starts the client workers. |
| 120 | * |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 121 | * @throws java.io.IOException if unable to open connection |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 122 | */ |
| 123 | public void start() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 124 | messages = new Counter(); |
| 125 | bytes = new Counter(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 126 | |
| 127 | // First start up all the IO loops |
| 128 | for (CustomIOLoop l : iloops) { |
| 129 | ipool.execute(l); |
| 130 | } |
| 131 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 132 | // Wait for all of them to get going |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 133 | for (CustomIOLoop l : iloops) { |
| 134 | l.awaitStart(1000); |
| 135 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 136 | |
| 137 | // ... and Next open all connections; one-per-loop |
| 138 | for (CustomIOLoop l : iloops) { |
| 139 | openConnection(l); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Initiates open connection request and registers the pending socket |
| 146 | * channel with the given IO loop. |
| 147 | * |
| 148 | * @param loop loop with which the channel should be registered |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 149 | * @throws java.io.IOException if the socket could not be open or connected |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 150 | */ |
| 151 | private void openConnection(CustomIOLoop loop) throws IOException { |
| 152 | SocketAddress sa = new InetSocketAddress(ip, port); |
| 153 | SocketChannel ch = SocketChannel.open(); |
| 154 | ch.configureBlocking(false); |
| 155 | loop.connectStream(ch); |
| 156 | ch.connect(sa); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Waits for the client workers to complete. |
| 162 | * |
| 163 | * @param secs timeout in seconds |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 164 | * @throws java.util.concurrent.ExecutionException if execution failed |
| 165 | * @throws InterruptedException if interrupt occurred while waiting |
| 166 | * @throws java.util.concurrent.TimeoutException if timeout occurred |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 167 | */ |
| 168 | public void await(int secs) throws InterruptedException, |
| 169 | ExecutionException, TimeoutException { |
| 170 | for (CustomIOLoop l : iloops) { |
| 171 | if (l.worker.task != null) { |
| 172 | l.worker.task.get(secs, TimeUnit.SECONDS); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 173 | latencyTotal += l.latencyTotal; |
| 174 | latencyCount += l.latencyCount; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 177 | messages.freeze(); |
| 178 | bytes.freeze(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Reports on the accumulated throughput trackers. |
| 183 | */ |
| 184 | public void report() { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 185 | DecimalFormat f = new DecimalFormat("#,##0"); |
tom | 14dc4d0 | 2014-09-26 12:43:14 -0700 | [diff] [blame] | 186 | out.println(format("Client: %s messages; %s bytes; %s mps; %s MBs; %s ns latency", |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 187 | f.format(messages.total()), f.format(bytes.total()), |
| 188 | f.format(messages.throughput()), |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 189 | f.format(bytes.throughput() / (1024 * msgLength)), |
| 190 | f.format(latencyTotal / latencyCount))); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
| 194 | // Loop for transfer of fixed-length messages |
| 195 | private class CustomIOLoop extends IOLoop<TestMessage, TestMessageStream> { |
| 196 | |
| 197 | Worker worker = new Worker(); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 198 | long latencyTotal = 0; |
| 199 | long latencyCount = 0; |
| 200 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 201 | |
| 202 | public CustomIOLoop() throws IOException { |
| 203 | super(500); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | @Override |
| 208 | protected TestMessageStream createStream(ByteChannel channel) { |
| 209 | return new TestMessageStream(msgLength, channel, this); |
| 210 | } |
| 211 | |
| 212 | @Override |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 213 | protected synchronized void removeStream(MessageStream<TestMessage> stream) { |
| 214 | super.removeStream(stream); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 215 | messages.add(stream.messagesIn().total()); |
| 216 | bytes.add(stream.bytesIn().total()); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 217 | stream.messagesOut().reset(); |
| 218 | stream.bytesOut().reset(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
| 222 | protected void processMessages(List<TestMessage> messages, |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 223 | MessageStream<TestMessage> stream) { |
| 224 | for (TestMessage message : messages) { |
| 225 | // TODO: summarize latency data better |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame] | 226 | latencyTotal += nanoTime() - message.requestorTime(); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 227 | latencyCount++; |
| 228 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 229 | worker.release(messages.size()); |
| 230 | } |
| 231 | |
| 232 | @Override |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame] | 233 | protected void connect(SelectionKey key) throws IOException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 234 | super.connect(key); |
| 235 | TestMessageStream b = (TestMessageStream) key.attachment(); |
| 236 | Worker w = ((CustomIOLoop) b.loop()).worker; |
| 237 | w.pump(b); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Auxiliary worker to connect and pump batched messages using blocking I/O. |
| 244 | */ |
| 245 | private class Worker implements Runnable { |
| 246 | |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame] | 247 | private static final int BATCH_SIZE = 50; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 248 | private static final int PERMITS = 2 * BATCH_SIZE; |
| 249 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 250 | private TestMessageStream stream; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 251 | private FutureTask<Worker> task; |
| 252 | |
| 253 | // Stuff to throttle pump |
| 254 | private final Semaphore semaphore = new Semaphore(PERMITS); |
| 255 | private int msgWritten; |
| 256 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 257 | void pump(TestMessageStream stream) { |
| 258 | this.stream = stream; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 259 | task = new FutureTask<>(this, this); |
| 260 | wpool.execute(task); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void run() { |
| 265 | try { |
| 266 | log.info("Worker started..."); |
| 267 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 268 | while (msgWritten < msgCount) { |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 269 | int size = Math.min(BATCH_SIZE, msgCount - msgWritten); |
| 270 | writeBatch(size); |
| 271 | msgWritten += size; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Now try to get all the permits back before sending poison pill |
| 275 | semaphore.acquireUninterruptibly(PERMITS); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 276 | stream.close(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 277 | |
| 278 | log.info("Worker done..."); |
| 279 | |
| 280 | } catch (IOException e) { |
| 281 | log.error("Worker unable to perform I/O", e); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 286 | private void writeBatch(int size) throws IOException { |
| 287 | // Build a batch of messages |
| 288 | List<TestMessage> batch = Lists.newArrayListWithCapacity(size); |
| 289 | for (int i = 0; i < size; i++) { |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame] | 290 | batch.add(new TestMessage(msgLength, nanoTime(), 0, stream.padding())); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 291 | } |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 292 | acquire(size); |
| 293 | stream.write(batch); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | // Release permits based on the specified number of message credits |
| 298 | private void release(int permits) { |
| 299 | semaphore.release(permits); |
| 300 | } |
| 301 | |
| 302 | // Acquire permit for a single batch |
| 303 | private void acquire(int permits) { |
| 304 | semaphore.acquireUninterruptibly(permits); |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | |
| 309 | } |