Skip to content

Commit

Permalink
[Perf] Include running average in per-second output (#18742)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder authored Jan 22, 2021
1 parent abc63f1 commit a043bdd
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@
* Represents the main program class which reflectively runs and manages the performance tests.
*/
public class PerfStressProgram {
private static final int NANOSECONDS_PER_SECOND = 1_000_000_000;

private static int[] completedOperations;
private static long[] lastCompletionNanoTimes;

private static int getCompletedOperations() {
return IntStream.of(completedOperations).sum();
}

private static double getOperationsPerSecond() {
return IntStream.range(0, completedOperations.length)
.mapToDouble(i -> completedOperations[i] / (((double) lastCompletionNanoTimes[i]) / NANOSECONDS_PER_SECOND))
.sum();
}

/**
* Runs the performance tests passed to be executed.
*
Expand Down Expand Up @@ -177,11 +189,13 @@ public static void runTests(PerfStressTest<?>[] tests, boolean sync, int paralle

int[] lastCompleted = new int[] { 0 };
Disposable progressStatus = printStatus(
"=== " + title + " ===" + System.lineSeparator() + "Current\t\tTotal", () -> {
int totalCompleted = IntStream.of(completedOperations).sum();
"=== " + title + " ===" + System.lineSeparator() + "Current\t\tTotal\t\tAverage", () -> {
int totalCompleted = getCompletedOperations();
int currentCompleted = totalCompleted - lastCompleted[0];
double averageCompleted = getOperationsPerSecond();

lastCompleted[0] = totalCompleted;
return currentCompleted + "\t\t" + totalCompleted;
return String.format("%d\t\t%d\t\t%.2f", currentCompleted, totalCompleted, averageCompleted);
}, true, true);

if (sync) {
Expand All @@ -206,14 +220,12 @@ public static void runTests(PerfStressTest<?>[] tests, boolean sync, int paralle

System.out.println("=== Results ===");

int totalOperations = IntStream.of(completedOperations).sum();
double operationsPerSecond = IntStream.range(0, parallel)
.mapToDouble(i -> completedOperations[i] / (((double) lastCompletionNanoTimes[i]) / 1000000000))
.sum();
int totalOperations = getCompletedOperations();
double operationsPerSecond = getOperationsPerSecond();
double secondsPerOperation = 1 / operationsPerSecond;
double weightedAverageSeconds = totalOperations / operationsPerSecond;

System.out.printf("Completed %d operations in a weighted-average of %.2fs (%.2f ops/s, %.3f s/op)%n",
System.out.printf("Completed %,d operations in a weighted-average of %,.2fs (%,.2f ops/s, %,.3f s/op)%n",
totalOperations, weightedAverageSeconds, operationsPerSecond, secondsPerOperation);
System.out.println();
}
Expand Down

0 comments on commit a043bdd

Please sign in to comment.