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