Skip to content

Commit 2ef1b7b

Browse files
committed
Now allows the benchmark to go into profiling mode
1 parent 744b107 commit 2ef1b7b

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

src/test/java/benchmark/ComplexQueryBenchmark.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import org.openjdk.jmh.runner.options.Options;
3030
import org.openjdk.jmh.runner.options.OptionsBuilder;
3131

32+
import java.io.BufferedReader;
33+
import java.io.IOException;
34+
import java.io.InputStreamReader;
3235
import java.util.List;
3336
import java.util.concurrent.CompletableFuture;
3437
import java.util.concurrent.ExecutorService;
@@ -102,21 +105,47 @@ public static void main(String[] args) throws Exception {
102105
@SuppressWarnings({"ConstantValue", "LoopConditionNotUpdatedInsideLoop"})
103106
private static void runAtStartup() {
104107
// 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();
106109

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");
109118
ComplexQueryBenchmark complexQueryBenchmark = new ComplexQueryBenchmark();
110119
complexQueryBenchmark.setUp();
111120
do {
112-
System.out.print("Running queries....\n");
121+
System.out.printf("Running queries for %d millis....\n", System.currentTimeMillis() - then);
113122
complexQueryBenchmark.howManyItems = 100;
114123
complexQueryBenchmark.runManyQueriesToCompletion();
115-
} while (forever);
124+
now = System.currentTimeMillis();
125+
} while ((now - then) < runForMillis);
116126
complexQueryBenchmark.tearDown();
117127

118128
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+
}
119141

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+
}
120149
}
121150

122151
@SuppressWarnings("UnnecessaryLocalVariable")
@@ -200,6 +229,7 @@ private void sleep(Integer howLong) {
200229
}
201230

202231
AtomicInteger logCount = new AtomicInteger();
232+
203233
private void logEvery(int every, String s) {
204234
int count = logCount.getAndIncrement();
205235
if (count == 0 || count % every == 0) {

0 commit comments

Comments
 (0)