Skip to content

Instantly share code, notes, and snippets.

@toefel18
Created October 1, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toefel18/9def55a8c3c53c79a4488c29c66f31e5 to your computer and use it in GitHub Desktop.
Save toefel18/9def55a8c3c53c79a4488c29c66f31e5 to your computer and use it in GitHub Desktop.
public static void main(String[] args) throws InterruptedException {
benchmark(10, 20000);
benchmark(100, 20000);
benchmark(1000, 20000);
benchmark(10, 200000);
benchmark(100, 200000);
}
private static void benchmark(final int threads, final int itemsPerThread) throws InterruptedException {
long millis = System.currentTimeMillis();
subject = StatisticsFactory.createThreadsafeStatistics();
final CountDownLatch latch = new CountDownLatch(threads);
for (int i =0 ; i < threads; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Stopwatch stopwatch = subject.startStopwatch();
for (int k =0 ; k < itemsPerThread; k++) {
subject.addOccurrence("concurrency.counter");
subject.addSample("concurrency.sample", k);
}
subject.recordElapsedTime("concurrency.duration", stopwatch);
latch.countDown();
}
}).start();
}
latch.await();
Snapshot snapshot = subject.getSnapshot();
assertThat(snapshot.getDurations().get("concurrency.duration").getSampleCount()).isEqualTo(threads);
assertThat(snapshot.getSamples().get("concurrency.sample").getSampleCount()).isEqualTo(itemsPerThread * threads);
assertThat(snapshot.getOccurrences().get("concurrency.counter")).isEqualTo(itemsPerThread * threads);
System.out.println(threads + " threads with " + itemsPerThread + " items takes " + (System.currentTimeMillis() - millis));
}
//Results:
//10 threads with 20000 items takes 89 millis
//100 threads with 20000 items takes 265 millis
//1000 threads with 20000 items takes 2888 millis
//10 threads with 200000 items takes 311 millis
//100 threads with 200000 items takes 3067 millis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment