Created
December 6, 2017 09:06
-
-
Save epickrram/48ed50072f5d42647b04e8e76199c8f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class PerfTest { | |
private final MaxPerSecondReporter reporter = new MaxPerSecondReporter(max -> { | |
System.out.printf("%s %d%n", Instant.now(), max); | |
}); | |
private void onExecutionReport() { | |
final long currentNanos = System.nanoTime(); | |
// calculate end-to-end latency | |
final long endToEndLatency = calculateEndToEndLatency(); | |
reporter.recordSample(currentNanos, endToEndLatency); | |
} | |
private static final class MaxPerSecondReporter { | |
private final LongConsumer maxValueReporter; | |
private long lastReportSecond = 0L; | |
private long maxValueInThisSecond = 0L; | |
MaxPerSecondReporter(final LongConsumer maxValueReporter) { | |
this.maxValueReporter = maxValueReporter; | |
} | |
void recordSample(final long currentNanos, final long endToEndLatency) { | |
final long currentSecond = TimeUnit.NANOSECONDS.toSeconds(currentNanos); | |
if (lastReportSecond == 0L) { | |
lastReportSecond = currentSecond; | |
} else if (lastReportSecond != currentSecond) { | |
maxValueReporter.accept(maxValueInThisSecond); | |
lastReportSecond = currentSecond; | |
maxValueInThisSecond = 0L; | |
} | |
maxValueInThisSecond = Math.max(endToEndLatency, maxValueInThisSecond); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment