Skip to content

Instantly share code, notes, and snippets.

@epickrram
Created December 6, 2017 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epickrram/48ed50072f5d42647b04e8e76199c8f1 to your computer and use it in GitHub Desktop.
Save epickrram/48ed50072f5d42647b04e8e76199c8f1 to your computer and use it in GitHub Desktop.
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