*/
final UnsafeOperator operator = new UnsafeOperator();
final CyclicBarrier startLatch = new CyclicBarrier(concurrency+1);
final CyclicBarrier endLatch = new CyclicBarrier(concurrency+1);
final TimingAccumulator timer = new TimingAccumulator();
for(int i=0;i<concurrency;i++){
testService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
startLatch.await();
} catch (InterruptedException e) {
fail(String.format("%d - %s",Thread.currentThread().getId(),e.getMessage()));
} catch (BrokenBarrierException e) {
fail(String.format("%d - %s",Thread.currentThread().getId(),e.getMessage()));
}
ZooKeeper zk = newZooKeeper();
try{
ZkSessionManager zksm = new BaseZkSessionManager(zk);
Lock guard = new ReentrantZkLock2(baseLockPath,zksm);
long totalStart = System.currentTimeMillis();
for(int i=0;i<MAX_LOCK_ATTEMPTS;i++){
timer.addTiming(guardedWork(guard,operator));
}
long totalEnd = System.currentTimeMillis();
long totalTime = totalEnd-totalStart;
float totalTimeSec = totalTime/1000f;
int ops = MAX_LOCK_ATTEMPTS;
float opsPerSec = ops/totalTimeSec;
System.out.printf("%d - total time taken: %d ms \t total Operations performed: %d \t Ops/sec: %f%n",Thread.currentThread().getId(),totalTime,ops,opsPerSec);
}finally{
zk.close();
}
//enter the stop latch
try {
endLatch.await();
} catch (InterruptedException e) {
fail(String.format("%d - %s",Thread.currentThread().getId(),e.getMessage()));
} catch (BrokenBarrierException e) {
fail(String.format("%d - %s",Thread.currentThread().getId(),e.getMessage()));
}
return null;
}
});
}
//start the test
startLatch.await();
//enter the end latch to wait on things finishing
endLatch.await();
int numberOfOperations = MAX_LOCK_ATTEMPTS*concurrency;
assertEquals("Incorrect incrementing!",numberOfOperations,operator.getValue());
timer.printResults();
}