Example #1
0
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    int port;
    if (args.length > 0) {
      port = Integer.parseInt(args[0]);
    } else {
      port = 0;
    }

    DefaultEventLoop eventloop = DefaultEventLoop.createEventLoop("alone");
    eventloop.start(null, port, new Server(port));
    new Thread(eventloop).start();
  }
/**
 * Netlet Coral Block based Benchmark Test Client see: <a
 * href="http://www.coralblocks.com/index.php/2014/04/coralreactor-vs-netty-performance-comparison">http://www.coralblocks.com/index.php/2014/04/coralreactor-vs-netty-performance-comparison</a>,
 * <a
 * href="http://stackoverflow.com/questions/23839437/what-are-the-netty-alternatives-for-high-performance-networking">http://stackoverflow.com/questions/23839437/what-are-the-netty-alternatives-for-high-performance-networking</a>,
 * <a href="http://www.coralblocks.com/NettyBench.zip">http://www.coralblocks.com/NettyBench.zip</a>
 * and <a
 * href="https://groups.google.com/forum/#!topic/mechanical-sympathy/fhbyMnnxmaA">https://groups.google.com/forum/#!topic/mechanical-sympathy/fhbyMnnxmaA</a>
 *
 * <p>run: <code>mvn exec:exec -Dbenchmark=netlet.client</code>
 *
 * <p>results=Iterations: 1000000 | Avg Time: 28.386 micros | Min Time: 15.0 micros | Max Time:
 * 167.0 micros | 75% Time: 28.0 micros | 90% Time: 36.0 micros | 99% Time: 47.0 micros | 99.9%
 * Time: 76.0 micros | 99.99% Time: 94.0 micros | 99.999% Time: 115.0 micros
 */
public class BenchmarkTcpClient extends AbstractClient {
  private static final Logger logger = LoggerFactory.getLogger(BenchmarkTcpClient.class);

  private int count = 0;
  private long start;
  private boolean warmingUp = false;
  private boolean benchmarking = false;
  private long timestamp;
  private final ByteBuffer readByteBuffer = ByteBuffer.allocate(BenchmarkConfiguration.messageSize);
  private final ByteBuffer sendByteBuffer = ByteBuffer.allocate(BenchmarkConfiguration.messageSize);
  private final BenchmarkResults benchmarkResults =
      new BenchmarkResults(BenchmarkConfiguration.messageCount);
  private final DefaultEventLoop eventLoop = DefaultEventLoop.createEventLoop("EventLoop");

  private BenchmarkTcpClient(final String host, final int port)
      throws IOException, InterruptedException {
    super();
    final Thread eventLoopThread = eventLoop.start();
    eventLoop.connect(new InetSocketAddress(host, port), this);
    eventLoopThread.join();
  }

  @Override
  public ByteBuffer buffer() {
    return readByteBuffer;
  }

  @Override
  public void handleException(Exception e, EventLoop eventLoop) {
    logger.error("", e);
    this.eventLoop.stop();
  }

  @Override
  public void connected() {
    logger.info("Connected. Sending the first message.");
    start = System.currentTimeMillis();
    warmingUp = true;
    benchmarking = false;
    count = 0;
    send(-1);
  }

  @Override
  public void unregistered(SelectionKey key) {
    super.unregistered(key);
    disconnected();
  }

  @Override
  public void disconnected() {
    logger.info("Disconnected. Overall test time: {} millis", System.currentTimeMillis() - start);
    benchmarkResults.printResults(System.out);
    eventLoop.stop();
  }

  @Override
  public void read(int len) {
    if (readByteBuffer.position() != readByteBuffer.capacity()) {
      logger.error(
          "Read buffer position {} != capacity {}",
          readByteBuffer.position(),
          readByteBuffer.capacity());
      eventLoop.disconnect(this);
      return;
    }

    readByteBuffer.flip();
    long timestamp = readByteBuffer.getLong();
    if (timestamp < -2) {
      logger.error("Received bad timestamp {}", timestamp);
      eventLoop.disconnect(this);
      return;
    } else if (timestamp != this.timestamp) {
      logger.error("Received bad timestamp {}. Sent timestamp {}", timestamp, this.timestamp);
      eventLoop.disconnect(this);
      return;
    } else if (timestamp > 0) {
      benchmarkResults.addResult(System.nanoTime() - timestamp);
    }
    readByteBuffer.clear();

    send();
  }

  private void send(final long tsSent) {
    this.timestamp = tsSent;
    sendByteBuffer.putLong(tsSent);
    while (sendByteBuffer.hasRemaining()) {
      sendByteBuffer.put((byte) 'x');
    }
    sendByteBuffer.flip();
    try {
      while (!send(sendByteBuffer.array())) {
        sleep(5);
      }
      write();
    } catch (Exception e) {
      logger.error("", e);
      eventLoop.disconnect(this);
      return;
    }
    sendByteBuffer.clear();
  }

  private void send() {
    if (warmingUp) {
      if (++count == BenchmarkConfiguration.messageCount) {
        logger.info(
            "Finished warming up! Sent {} messages in {} millis",
            count,
            System.currentTimeMillis() - start);
        warmingUp = false;
        benchmarking = true;
        count = 0;
        send(System.nanoTime());
      } else {
        send(0);
      }
    } else if (benchmarking) {
      if (++count == BenchmarkConfiguration.messageCount) {
        send(-2);
        logger.info("Finished sending messages! Sent {} messages.", count);
        eventLoop.disconnect(this);
      } else {
        send(System.nanoTime());
      }
    }
  }

  public static void main(String[] args) {
    String host = args[0];
    int port = Integer.parseInt(args[1]);
    try {
      new BenchmarkTcpClient(host, port);
    } catch (IOException e) {
      logger.error("", e);
    } catch (InterruptedException e) {
      logger.error("", e);
    }
  }
}