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 | |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 85 | // Start pruning clients and keep going until their number goes to 0. |
| 86 | int remaining = -1; |
| 87 | while (remaining == -1 || remaining > 0) { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 88 | delay(PRUNE_FREQUENCY); |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 89 | int r = server.prune(); |
| 90 | remaining = remaining == -1 && r == 0 ? remaining : r; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 91 | } |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 92 | server.stop(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a speed server. |
| 97 | * |
| 98 | * @param ip optional ip of the adapter where to bind |
| 99 | * @param wc worker count |
| 100 | * @param ml message length in bytes |
| 101 | * @param port listen port |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 102 | * @throws java.io.IOException if unable to create IO loops |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 103 | */ |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 104 | public IOLoopTestServer(InetAddress ip, int wc, int ml, int port) throws IOException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 105 | this.workerCount = wc; |
| 106 | this.msgLength = ml; |
| 107 | this.ipool = Executors.newFixedThreadPool(workerCount, namedThreads("io-loop")); |
| 108 | |
| 109 | this.aloop = new CustomAcceptLoop(new InetSocketAddress(ip, port)); |
| 110 | for (int i = 0; i < workerCount; i++) { |
| 111 | iloops.add(new CustomIOLoop()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Start the server IO loops and kicks off throughput tracking. |
| 117 | */ |
| 118 | public void start() { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 119 | messages = new Counter(); |
| 120 | bytes = new Counter(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 121 | |
| 122 | for (CustomIOLoop l : iloops) { |
| 123 | ipool.execute(l); |
| 124 | } |
| 125 | apool.execute(aloop); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 126 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 127 | for (CustomIOLoop l : iloops) { |
| 128 | l.awaitStart(TIMEOUT); |
| 129 | } |
| 130 | aloop.awaitStart(TIMEOUT); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Stop the server IO loops and freezes throughput tracking. |
| 135 | */ |
| 136 | public void stop() { |
| 137 | aloop.shutdown(); |
| 138 | for (CustomIOLoop l : iloops) { |
| 139 | l.shutdown(); |
| 140 | } |
| 141 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 142 | for (CustomIOLoop l : iloops) { |
| 143 | l.awaitStop(TIMEOUT); |
| 144 | } |
| 145 | aloop.awaitStop(TIMEOUT); |
| 146 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 147 | messages.freeze(); |
| 148 | bytes.freeze(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Reports on the accumulated throughput trackers. |
| 153 | */ |
| 154 | public void report() { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 155 | DecimalFormat f = new DecimalFormat("#,##0"); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 156 | out.println(format("Server: %s messages; %s bytes; %s mps; %s Mbs", |
| 157 | f.format(messages.total()), f.format(bytes.total()), |
| 158 | f.format(messages.throughput()), |
| 159 | f.format(bytes.throughput() / (1024 * msgLength)))); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Prunes the IO loops of stale message buffers. |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 164 | * |
| 165 | * @return number of remaining IO loops among all workers. |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 166 | */ |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 167 | public int prune() { |
| 168 | int count = 0; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 169 | for (CustomIOLoop l : iloops) { |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 170 | count += l.pruneStaleStreams(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 171 | } |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 172 | return count; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Get the next worker to which a client should be assigned |
| 176 | private synchronized CustomIOLoop nextWorker() { |
| 177 | lastWorker = (lastWorker + 1) % workerCount; |
| 178 | return iloops.get(lastWorker); |
| 179 | } |
| 180 | |
| 181 | // Loop for transfer of fixed-length messages |
| 182 | private class CustomIOLoop extends IOLoop<TestMessage, TestMessageStream> { |
| 183 | |
| 184 | public CustomIOLoop() throws IOException { |
| 185 | super(500); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | protected TestMessageStream createStream(ByteChannel channel) { |
| 190 | return new TestMessageStream(msgLength, channel, this); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | protected void removeStream(MessageStream<TestMessage> stream) { |
| 195 | super.removeStream(stream); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 196 | |
| 197 | messages.add(stream.messagesIn().total()); |
| 198 | bytes.add(stream.bytesIn().total()); |
| 199 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 200 | // out.println(format("Disconnected server; inbound %s mps, %s Mbps; outbound %s mps, %s Mbps", |
| 201 | // FORMAT.format(stream.messagesIn().throughput()), |
| 202 | // FORMAT.format(stream.bytesIn().throughput() / (1024 * msgLength)), |
| 203 | // FORMAT.format(stream.messagesOut().throughput()), |
| 204 | // FORMAT.format(stream.bytesOut().throughput() / (1024 * msgLength)))); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | protected void processMessages(List<TestMessage> messages, |
| 209 | MessageStream<TestMessage> stream) { |
| 210 | try { |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 211 | stream.write(createResponses(messages)); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 212 | } catch (IOException e) { |
| 213 | log.error("Unable to echo messages", e); |
| 214 | } |
| 215 | } |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 216 | |
| 217 | private List<TestMessage> createResponses(List<TestMessage> messages) { |
| 218 | List<TestMessage> responses = Lists.newArrayListWithCapacity(messages.size()); |
| 219 | for (TestMessage message : messages) { |
| 220 | responses.add(new TestMessage(message.length(), message.requestorTime(), |
tom | 5f4df2d | 2014-09-26 12:19:51 -0700 | [diff] [blame^] | 221 | System.nanoTime(), message.padding())); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 222 | } |
| 223 | return responses; |
| 224 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Loop for accepting client connections |
| 228 | private class CustomAcceptLoop extends AcceptorLoop { |
| 229 | |
| 230 | public CustomAcceptLoop(SocketAddress address) throws IOException { |
| 231 | super(500, address); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | protected void acceptConnection(ServerSocketChannel channel) throws IOException { |
| 236 | SocketChannel sc = channel.accept(); |
| 237 | sc.configureBlocking(false); |
| 238 | |
| 239 | Socket so = sc.socket(); |
| 240 | so.setTcpNoDelay(SO_NO_DELAY); |
| 241 | so.setReceiveBufferSize(SO_RCV_BUFFER_SIZE); |
| 242 | so.setSendBufferSize(SO_SEND_BUFFER_SIZE); |
| 243 | |
| 244 | nextWorker().acceptStream(sc); |
| 245 | log.info("Connected client"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | } |