ClientSocketChannelFactory channelFactory
= new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors
.newCachedThreadPool());
OrderedSafeExecutor executor = new OrderedSafeExecutor(1);
ClientConfiguration conf = new ClientConfiguration();
BookieClient bc = new BookieClient(conf, channelFactory, executor);
LatencyCallback lc = new LatencyCallback();
ThroughputCallback tc = new ThroughputCallback();
int warmUpCount = 999;
long ledger = getValidLedgerId(servers);
for(long entry = 0; entry < warmUpCount; entry++) {
ChannelBuffer toSend = ChannelBuffers.buffer(size);
toSend.resetReaderIndex();
toSend.resetWriterIndex();
toSend.writeLong(ledger);
toSend.writeLong(entry);
toSend.writerIndex(toSend.capacity());
bc.addEntry(new InetSocketAddress(addr, port), ledger, new byte[20],
entry, toSend, tc, null, BookieProtocol.FLAG_NONE);
}
LOG.info("Waiting for warmup");
tc.waitFor(warmUpCount);
ledger = getValidLedgerId(servers);
LOG.info("Benchmarking latency");
int entryCount = 5000;
long startTime = System.nanoTime();
for(long entry = 0; entry < entryCount; entry++) {
ChannelBuffer toSend = ChannelBuffers.buffer(size);
toSend.resetReaderIndex();
toSend.resetWriterIndex();
toSend.writeLong(ledger);
toSend.writeLong(entry);
toSend.writerIndex(toSend.capacity());
lc.resetComplete();
bc.addEntry(new InetSocketAddress(addr, port), ledger, new byte[20],
entry, toSend, lc, null, BookieProtocol.FLAG_NONE);
lc.waitForComplete();
}
long endTime = System.nanoTime();
LOG.info("Latency: " + (((double)(endTime-startTime))/((double)entryCount))/1000000.0);
entryCount = 50000;
ledger = getValidLedgerId(servers);
LOG.info("Benchmarking throughput");
startTime = System.currentTimeMillis();
tc = new ThroughputCallback();
for(long entry = 0; entry < entryCount; entry++) {
ChannelBuffer toSend = ChannelBuffers.buffer(size);
toSend.resetReaderIndex();
toSend.resetWriterIndex();
toSend.writeLong(ledger);
toSend.writeLong(entry);
toSend.writerIndex(toSend.capacity());
bc.addEntry(new InetSocketAddress(addr, port), ledger, new byte[20],
entry, toSend, tc, null, BookieProtocol.FLAG_NONE);
}
tc.waitFor(entryCount);
endTime = System.currentTimeMillis();
LOG.info("Throughput: " + ((long)entryCount)*1000/(endTime-startTime));
bc.close();
channelFactory.releaseExternalResources();
executor.shutdown();
}