| 1 | package org.jtoolkit.essence.test; |
| 2 | |
| 3 | import org.jetbrains.annotations.Nullable; |
| 4 | import org.jtoolkit.essence.concurrency.Threads; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.List; |
| 8 | import java.util.concurrent.*; |
| 9 | import java.util.concurrent.atomic.AtomicBoolean; |
| 10 | import java.util.concurrent.atomic.AtomicInteger; |
| 11 | |
| 12 | public class Benchmark { |
| 13 | public static final int UNSET = -1; |
| 14 | |
| 15 | private final BenchmarkCase benchmarkCase; |
| 16 | private final List<Double> rates = new ArrayList<Double>(); |
| 17 | |
| 18 | private int runtimeMS = 2500; |
| 19 | private int numberOfThreads = UNSET; |
| 20 | private int numberOfJobs = UNSET; |
| 21 | private int numberOfSamples = 1; |
| 22 | private boolean displaySample = false; |
| 23 | |
| 24 | public Benchmark(BenchmarkCase benchmarkCase) { |
| 25 | this.benchmarkCase = benchmarkCase; |
| 26 | } |
| 27 | |
| 28 | public Benchmark(final Runnable runnable) { |
| 29 | benchmarkCase = new BenchmarkCase() { |
| 30 | public void doTest(int run, AtomicBoolean running, AtomicInteger runCount) throws Exception { |
| 31 | while (running.get()) { |
| 32 | runnable.run(); |
| 33 | runCount.incrementAndGet(); |
| 34 | } |
| 35 | } |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | public boolean isDisplaySample() { |
| 40 | return displaySample; |
| 41 | } |
| 42 | |
| 43 | public void setDisplaySample(boolean displaySample) { |
| 44 | this.displaySample = displaySample; |
| 45 | } |
| 46 | |
| 47 | public boolean isToRun() { |
| 48 | return rates.isEmpty(); |
| 49 | } |
| 50 | |
| 51 | public int getNumberOfSamples() { |
| 52 | return numberOfSamples; |
| 53 | } |
| 54 | |
| 55 | public void setNumberOfSamples(int numberOfSamples) { |
| 56 | this.numberOfSamples = numberOfSamples; |
| 57 | } |
| 58 | |
| 59 | public int getNumberOfJobs() { |
| 60 | return numberOfJobs; |
| 61 | } |
| 62 | |
| 63 | public void setNumberOfJobs(int numberOfJobs) { |
| 64 | this.numberOfJobs = numberOfJobs; |
| 65 | } |
| 66 | |
| 67 | public int getNumberOfThreads() { |
| 68 | return numberOfThreads; |
| 69 | } |
| 70 | |
| 71 | public void setNumberOfThreads(int numberOfThreads) { |
| 72 | this.numberOfThreads = numberOfThreads; |
| 73 | } |
| 74 | |
| 75 | public int getRuntimeMS() { |
| 76 | return runtimeMS; |
| 77 | } |
| 78 | |
| 79 | public void setRuntimeMS(int runtimeMS) { |
| 80 | this.runtimeMS = runtimeMS; |
| 81 | } |
| 82 | |
| 83 | public void run() { |
| 84 | int nThreads = numberOfThreads >= 1 ? numberOfThreads : 1; |
| 85 | int jobs = numberOfJobs < nThreads ? nThreads : numberOfJobs; |
| 86 | |
| 87 | ScheduledExecutorService timeout = Executors.newSingleThreadScheduledExecutor(); |
| 88 | ExecutorService executor = Executors.newFixedThreadPool(nThreads); |
| 89 | |
| 90 | // use the first sample to warm up. |
| 91 | int numberOfSamples = this.numberOfSamples > 1 ? this.numberOfSamples + 1 : 1; |
| 92 | int runtimeMS = (this.runtimeMS >= 1 ? this.runtimeMS : 2500) * nThreads / jobs / numberOfSamples; |
| 93 | try { |
| 94 | for (int s = 0; s < numberOfSamples; s++) { |
| 95 | double rate = runTests(timeout, executor, jobs, runtimeMS); |
| 96 | if (rate > 0) |
| 97 | rates.add(rate); |
| 98 | } |
| 99 | } catch (RuntimeException e) { |
| 100 | throw e; |
| 101 | } catch (ExecutionException e) { |
| 102 | throwAssertionError(e.getCause()); |
| 103 | } catch (Exception e) { |
| 104 | throwAssertionError(e); |
| 105 | } finally { |
| 106 | Threads.shutdown(timeout); |
| 107 | Threads.shutdown(executor); |
| 108 | |
| 109 | if (rates.size() > 1) |
| 110 | rates.remove(0); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private static void throwAssertionError(Throwable e) { |
| 115 | AssertionError error = new AssertionError("Exception thrown running test"); |
| 116 | error.initCause(e); |
| 117 | throw error; |
| 118 | } |
| 119 | |
| 120 | public double getAverageRate() { |
| 121 | if (isToRun()) |
| 122 | run(); |
| 123 | double total = 0; |
| 124 | for (double rate : rates) |
| 125 | total += rate; |
| 126 | return total / rates.size(); |
| 127 | } |
| 128 | |
| 129 | public double getAverageLatency() { |
| 130 | if (isToRun()) |
| 131 | run(); |
| 132 | double total = 0; |
| 133 | for (double rate : rates) |
| 134 | total += 1 / rate; |
| 135 | return total / rates.size(); |
| 136 | } |
| 137 | |
| 138 | public double getStdDev() { |
| 139 | if (isToRun()) |
| 140 | run(); |
| 141 | double total = 0; |
| 142 | for (double rate : rates) |
| 143 | total += rate; |
| 144 | double average = total / rates.size(); |
| 145 | double totalDiffSqu = 0; |
| 146 | for (double rate : rates) { |
| 147 | double diff = rate - average; |
| 148 | totalDiffSqu += diff * diff; |
| 149 | } |
| 150 | return Math.sqrt(totalDiffSqu) / rates.size(); |
| 151 | } |
| 152 | |
| 153 | private double runTests(ScheduledExecutorService timeout, ExecutorService executor, int jobs, final int runtimeMS) throws InterruptedException, ExecutionException, TimeoutException { |
| 154 | final AtomicBoolean running = new AtomicBoolean(true); |
| 155 | final AtomicInteger runCount = new AtomicInteger(); |
| 156 | final AtomicInteger finishedCount = new AtomicInteger(); |
| 157 | final Future[] futures = new Future[jobs]; |
| 158 | |
| 159 | final long start = System.nanoTime(); |
| 160 | |
| 161 | for (int j = 0; j < jobs; j++) { |
| 162 | final int j2 = j; |
| 163 | futures[j] = executor.submit(new Callable<Void>() { |
| 164 | @Nullable public Void call() throws Exception { |
| 165 | try { |
| 166 | benchmarkCase.doTest(j2, running, runCount); |
| 167 | } finally { |
| 168 | finishedCount.incrementAndGet(); |
| 169 | } |
| 170 | return null; |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | // add a timeout. |
| 176 | Future timeoutFuture = timeout.schedule(new Runnable() { |
| 177 | public void run() { |
| 178 | // System.out.println("Finishing run time " + runtimeMS); |
| 179 | running.set(false); |
| 180 | } |
| 181 | }, runtimeMS, TimeUnit.MILLISECONDS); |
| 182 | |
| 183 | Future maxTimeoutFuture = timeout.schedule(new Runnable() { |
| 184 | public void run() { |
| 185 | for (Future future : futures) |
| 186 | future.cancel(true); |
| 187 | } |
| 188 | }, runtimeMS, TimeUnit.MILLISECONDS); |
| 189 | |
| 190 | // wait for tests to finish, or wait the timeout. |
| 191 | while (!timeoutFuture.isDone() && finishedCount.get() < jobs) |
| 192 | try { |
| 193 | timeoutFuture.get(100, TimeUnit.MILLISECONDS); |
| 194 | } catch (TimeoutException ignored) { |
| 195 | // ignored. |
| 196 | } |
| 197 | // System.out.println("Finished tests"); |
| 198 | running.set(false); |
| 199 | // noinspection CallToThreadYield |
| 200 | Thread.yield(); |
| 201 | |
| 202 | if (timeoutFuture != null) timeoutFuture.cancel(true); |
| 203 | maxTimeoutFuture.cancel(true); |
| 204 | // check for exceptions thrown. |
| 205 | for (Future future : futures) |
| 206 | try { |
| 207 | if (!future.isCancelled() && !future.isDone()) { |
| 208 | future.cancel(true); |
| 209 | future.get(runtimeMS, TimeUnit.MILLISECONDS); |
| 210 | } |
| 211 | } catch (CancellationException ignored) { |
| 212 | // ignored |
| 213 | } |
| 214 | |
| 215 | long time = System.nanoTime() - start; |
| 216 | return runCount.get() * 1.0e9 / time; |
| 217 | } |
| 218 | } |