|
29 | 29 | import org.openjdk.jmh.runner.options.Options; |
30 | 30 | import org.openjdk.jmh.runner.options.OptionsBuilder; |
31 | 31 |
|
| 32 | +import java.io.BufferedReader; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStreamReader; |
32 | 35 | import java.util.List; |
33 | 36 | import java.util.concurrent.CompletableFuture; |
34 | 37 | import java.util.concurrent.ExecutorService; |
@@ -102,21 +105,47 @@ public static void main(String[] args) throws Exception { |
102 | 105 | @SuppressWarnings({"ConstantValue", "LoopConditionNotUpdatedInsideLoop"}) |
103 | 106 | private static void runAtStartup() { |
104 | 107 | // set this to true if you want to hook in profiler say to a forever running JVM |
105 | | - boolean forever = Boolean.getBoolean("forever"); |
| 108 | + int runForMillis = getRunForMillis(); |
106 | 109 |
|
107 | | - long then = System.currentTimeMillis(); |
108 | | - System.out.printf("Running initial code before starting the benchmark in forever=%b mode \n", forever); |
| 110 | + if (runForMillis <= 0) { |
| 111 | + return; |
| 112 | + } |
| 113 | + long now, then = System.currentTimeMillis(); |
| 114 | + System.out.printf("Running initial code before starting the benchmark - runForMillis=%d \n", runForMillis); |
| 115 | + System.out.print("Get your profiler in order and press enter... \n"); |
| 116 | + readLine(); |
| 117 | + System.out.print("Lets go...\n"); |
109 | 118 | ComplexQueryBenchmark complexQueryBenchmark = new ComplexQueryBenchmark(); |
110 | 119 | complexQueryBenchmark.setUp(); |
111 | 120 | do { |
112 | | - System.out.print("Running queries....\n"); |
| 121 | + System.out.printf("Running queries for %d millis....\n", System.currentTimeMillis() - then); |
113 | 122 | complexQueryBenchmark.howManyItems = 100; |
114 | 123 | complexQueryBenchmark.runManyQueriesToCompletion(); |
115 | | - } while (forever); |
| 124 | + now = System.currentTimeMillis(); |
| 125 | + } while ((now - then) < runForMillis); |
116 | 126 | complexQueryBenchmark.tearDown(); |
117 | 127 |
|
118 | 128 | System.out.printf("This took %d millis\n", System.currentTimeMillis() - then); |
| 129 | + System.exit(0); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + private static void readLine() { |
| 134 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 135 | + try { |
| 136 | + br.readLine(); |
| 137 | + } catch (IOException e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + } |
119 | 141 |
|
| 142 | + private static int getRunForMillis() { |
| 143 | + String runFor = System.getenv("runForMillis"); |
| 144 | + try { |
| 145 | + return Integer.parseInt(runFor); |
| 146 | + } catch (NumberFormatException e) { |
| 147 | + return -1; |
| 148 | + } |
120 | 149 | } |
121 | 150 |
|
122 | 151 | @SuppressWarnings("UnnecessaryLocalVariable") |
@@ -200,6 +229,7 @@ private void sleep(Integer howLong) { |
200 | 229 | } |
201 | 230 |
|
202 | 231 | AtomicInteger logCount = new AtomicInteger(); |
| 232 | + |
203 | 233 | private void logEvery(int every, String s) { |
204 | 234 | int count = logCount.getAndIncrement(); |
205 | 235 | if (count == 0 || count % every == 0) { |
|
0 commit comments