|
| 1 | +package perf; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | +import java.util.concurrent.CountDownLatch; |
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | +import java.util.concurrent.Executors; |
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
| 8 | + |
| 9 | +public abstract class Benchmark { |
| 10 | + |
| 11 | + private static final int SYNC_THREADS = 128; |
| 12 | + private final CountDownLatch completed = new CountDownLatch(1); |
| 13 | + private volatile Long startTime = null; |
| 14 | + private volatile Long endTime = null; |
| 15 | + private volatile int targetCount = Integer.MAX_VALUE; |
| 16 | + private final AtomicInteger counter = new AtomicInteger(); |
| 17 | + private final ExecutorService es = Executors.newFixedThreadPool(SYNC_THREADS); |
| 18 | + private final int warmupDurationSeconds; |
| 19 | + private final int numberOfRequests; |
| 20 | + |
| 21 | + public Benchmark(int warmupDurationSeconds, int numberOfRequests) { |
| 22 | + this.warmupDurationSeconds = warmupDurationSeconds; |
| 23 | + this.numberOfRequests = numberOfRequests; |
| 24 | + } |
| 25 | + |
| 26 | + public void run() { |
| 27 | + // warmup |
| 28 | + startBenchmark(); |
| 29 | + |
| 30 | + // start monitor / warmup |
| 31 | + startMonitor(); |
| 32 | + |
| 33 | + // start benchmark |
| 34 | + startMeasuring(); |
| 35 | + } |
| 36 | + |
| 37 | + private void startMonitor() { |
| 38 | + for (int i = 0; i < warmupDurationSeconds; i++) { |
| 39 | + counter.set(0); |
| 40 | + long start = new Date().getTime(); |
| 41 | + try { |
| 42 | + Thread.sleep(1_000); |
| 43 | + } catch (InterruptedException e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } |
| 46 | + |
| 47 | + long current = new Date().getTime(); |
| 48 | + long elapsed = current - start; |
| 49 | + double reqsPerSec = 1_000.0 * counter.get() / elapsed; |
| 50 | + System.out.println("reqs/s: \t" + reqsPerSec); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void startBenchmark() { |
| 55 | + start(); |
| 56 | + new Thread(() -> { |
| 57 | + try { |
| 58 | + completed.await(); |
| 59 | + // wait graceful shutdown |
| 60 | + Thread.sleep(1_000); |
| 61 | + } catch (InterruptedException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + // force shutdown |
| 65 | + es.shutdown(); |
| 66 | + shutdown(); |
| 67 | + }).start(); |
| 68 | + } |
| 69 | + |
| 70 | + private void startMeasuring() { |
| 71 | + counter.set(0); |
| 72 | + targetCount = numberOfRequests; |
| 73 | + startTime = System.currentTimeMillis(); |
| 74 | + } |
| 75 | + |
| 76 | + public long waitComplete() { |
| 77 | + try { |
| 78 | + completed.await(); |
| 79 | + } catch (InterruptedException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + return endTime - startTime; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return req/s |
| 87 | + */ |
| 88 | + public long getThroughput() { |
| 89 | + return targetCount * 1000L / (endTime - startTime); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * notify the success of #count requests |
| 94 | + * |
| 95 | + * @return whether more requests should be performed |
| 96 | + */ |
| 97 | + private boolean success() { |
| 98 | + if (endTime != null) return false; |
| 99 | + if (counter.addAndGet(1) >= targetCount) { |
| 100 | + endTime = System.currentTimeMillis(); |
| 101 | + completed.countDown(); |
| 102 | + return false; |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private void start() { |
| 108 | + for (int i = 0; i < SYNC_THREADS; i++) { |
| 109 | + es.execute(() -> { |
| 110 | + boolean more = true; |
| 111 | + while (more) { |
| 112 | + sendRequest(); |
| 113 | + more = success(); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + protected abstract void sendRequest(); |
| 120 | + |
| 121 | + protected abstract void shutdown(); |
| 122 | + |
| 123 | +} |
0 commit comments