Skip to content

Commit 127f6a1

Browse files
committed
Updated helper code for profiler attachment
1 parent 3a30942 commit 127f6a1

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

src/test/java/benchmark/BenchmarkUtils.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package benchmark;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
35
import java.io.InputStream;
6+
import java.io.InputStreamReader;
47
import java.net.URL;
58
import java.nio.charset.Charset;
9+
import java.time.LocalDateTime;
10+
import java.time.format.DateTimeFormatter;
611
import java.util.concurrent.Callable;
712

813
public class BenchmarkUtils {
@@ -30,4 +35,50 @@ static <T> T asRTE(Callable<T> callable) {
3035
}
3136
}
3237

38+
public static void runInToolingForSomeTimeThenExit(Runnable setup, Runnable r, Runnable tearDown) {
39+
int runForMillis = getRunForMillis();
40+
if (runForMillis <= 0) {
41+
System.out.print("'runForMillis' environment var is not set - continuing \n");
42+
return;
43+
}
44+
System.out.printf("Running initial code in some tooling - runForMillis=%d \n", runForMillis);
45+
System.out.print("Get your tooling in order and press enter...");
46+
readLine();
47+
System.out.print("Lets go...\n");
48+
setup.run();
49+
50+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
51+
long now, then = System.currentTimeMillis();
52+
do {
53+
now = System.currentTimeMillis();
54+
long msLeft = runForMillis - (now - then);
55+
System.out.printf("\t%s Running in loop... %s ms left\n", dtf.format(LocalDateTime.now()), msLeft);
56+
r.run();
57+
now = System.currentTimeMillis();
58+
} while ((now - then) < runForMillis);
59+
60+
tearDown.run();
61+
62+
System.out.printf("This ran for %d millis. Exiting...\n", System.currentTimeMillis() - then);
63+
System.exit(0);
64+
}
65+
66+
private static void readLine() {
67+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
68+
try {
69+
br.readLine();
70+
} catch (IOException e) {
71+
throw new RuntimeException(e);
72+
}
73+
}
74+
75+
private static int getRunForMillis() {
76+
String runFor = System.getenv("runForMillis");
77+
try {
78+
return Integer.parseInt(runFor);
79+
} catch (NumberFormatException e) {
80+
return -1;
81+
}
82+
}
83+
3384
}

src/test/java/benchmark/ComplexQueryBenchmark.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -104,50 +104,20 @@ public static void main(String[] args) throws Exception {
104104

105105
@SuppressWarnings({"ConstantValue", "LoopConditionNotUpdatedInsideLoop"})
106106
private static void runAtStartup() {
107-
// set this to true if you want to hook in profiler say to a forever running JVM
108-
int runForMillis = getRunForMillis();
109107

110-
if (runForMillis <= 0) {
111-
return;
112-
}
113-
System.out.printf("Running initial code before starting the benchmark - runForMillis=%d \n", runForMillis);
114-
System.out.print("Get your profiler in order and press enter... \n");
115-
readLine();
116-
System.out.print("Lets go...\n");
117-
118-
long now, then = System.currentTimeMillis();
119108
ComplexQueryBenchmark complexQueryBenchmark = new ComplexQueryBenchmark();
120-
complexQueryBenchmark.setUp();
121-
do {
122-
System.out.printf("Running queries for %d millis....\n", System.currentTimeMillis() - then);
123-
complexQueryBenchmark.howManyItems = 100;
124-
complexQueryBenchmark.runManyQueriesToCompletion();
125-
now = System.currentTimeMillis();
126-
} while ((now - then) < runForMillis);
127-
complexQueryBenchmark.tearDown();
128-
129-
System.out.printf("This took %d millis\n", System.currentTimeMillis() - then);
130-
System.exit(0);
109+
complexQueryBenchmark.howManyQueries = 5;
110+
complexQueryBenchmark.howManyItems = 10;
131111

132-
}
112+
BenchmarkUtils.runInToolingForSomeTimeThenExit(
113+
complexQueryBenchmark::setUp,
114+
complexQueryBenchmark::runManyQueriesToCompletion,
115+
complexQueryBenchmark::tearDown
133116

134-
private static void readLine() {
135-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
136-
try {
137-
br.readLine();
138-
} catch (IOException e) {
139-
throw new RuntimeException(e);
140-
}
117+
);
141118
}
142119

143-
private static int getRunForMillis() {
144-
String runFor = System.getenv("runForMillis");
145-
try {
146-
return Integer.parseInt(runFor);
147-
} catch (NumberFormatException e) {
148-
return -1;
149-
}
150-
}
120+
151121

152122
@SuppressWarnings("UnnecessaryLocalVariable")
153123
private Void runManyQueriesToCompletion() {

0 commit comments

Comments
 (0)