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