Skip to content

Commit

Permalink
Merge pull request #3491 from graphql-java/tweaked-profiling-code-helper
Browse files Browse the repository at this point in the history
Updated helper code for profiler attachment
  • Loading branch information
bbakerman committed Mar 2, 2024
2 parents 3a30942 + 127f6a1 commit fbca82e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
51 changes: 51 additions & 0 deletions src/test/java/benchmark/BenchmarkUtils.java
@@ -1,8 +1,13 @@
package benchmark;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Callable;

public class BenchmarkUtils {
Expand Down Expand Up @@ -30,4 +35,50 @@ static <T> T asRTE(Callable<T> callable) {
}
}

public static void runInToolingForSomeTimeThenExit(Runnable setup, Runnable r, Runnable tearDown) {
int runForMillis = getRunForMillis();
if (runForMillis <= 0) {
System.out.print("'runForMillis' environment var is not set - continuing \n");
return;
}
System.out.printf("Running initial code in some tooling - runForMillis=%d \n", runForMillis);
System.out.print("Get your tooling in order and press enter...");
readLine();
System.out.print("Lets go...\n");
setup.run();

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
long now, then = System.currentTimeMillis();
do {
now = System.currentTimeMillis();
long msLeft = runForMillis - (now - then);
System.out.printf("\t%s Running in loop... %s ms left\n", dtf.format(LocalDateTime.now()), msLeft);
r.run();
now = System.currentTimeMillis();
} while ((now - then) < runForMillis);

tearDown.run();

System.out.printf("This ran for %d millis. Exiting...\n", System.currentTimeMillis() - then);
System.exit(0);
}

private static void readLine() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static int getRunForMillis() {
String runFor = System.getenv("runForMillis");
try {
return Integer.parseInt(runFor);
} catch (NumberFormatException e) {
return -1;
}
}

}
46 changes: 8 additions & 38 deletions src/test/java/benchmark/ComplexQueryBenchmark.java
Expand Up @@ -104,50 +104,20 @@ public static void main(String[] args) throws Exception {

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

if (runForMillis <= 0) {
return;
}
System.out.printf("Running initial code before starting the benchmark - runForMillis=%d \n", runForMillis);
System.out.print("Get your profiler in order and press enter... \n");
readLine();
System.out.print("Lets go...\n");

long now, then = System.currentTimeMillis();
ComplexQueryBenchmark complexQueryBenchmark = new ComplexQueryBenchmark();
complexQueryBenchmark.setUp();
do {
System.out.printf("Running queries for %d millis....\n", System.currentTimeMillis() - then);
complexQueryBenchmark.howManyItems = 100;
complexQueryBenchmark.runManyQueriesToCompletion();
now = System.currentTimeMillis();
} while ((now - then) < runForMillis);
complexQueryBenchmark.tearDown();

System.out.printf("This took %d millis\n", System.currentTimeMillis() - then);
System.exit(0);
complexQueryBenchmark.howManyQueries = 5;
complexQueryBenchmark.howManyItems = 10;

}
BenchmarkUtils.runInToolingForSomeTimeThenExit(
complexQueryBenchmark::setUp,
complexQueryBenchmark::runManyQueriesToCompletion,
complexQueryBenchmark::tearDown

private static void readLine() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
);
}

private static int getRunForMillis() {
String runFor = System.getenv("runForMillis");
try {
return Integer.parseInt(runFor);
} catch (NumberFormatException e) {
return -1;
}
}


@SuppressWarnings("UnnecessaryLocalVariable")
private Void runManyQueriesToCompletion() {
Expand Down

0 comments on commit fbca82e

Please sign in to comment.