30 lines
944 B
Java
30 lines
944 B
Java
package com.ankurm.protocols;
|
|
|
|
import java.util.Arrays;
|
|
|
|
/** Latency bookkeeping, kept in one place so the three protocols are measured identically. */
|
|
final class Bench {
|
|
|
|
private final long[] nanos;
|
|
private int i;
|
|
|
|
Bench(int n) {
|
|
this.nanos = new long[n];
|
|
}
|
|
|
|
void record(long ns) {
|
|
nanos[i++] = ns;
|
|
}
|
|
|
|
/** p50, p99 and mean in microseconds, plus calls per second derived from the mean. */
|
|
String summary(String label) {
|
|
long[] sorted = Arrays.copyOf(nanos, i);
|
|
Arrays.sort(sorted);
|
|
double p50 = sorted[(int) (sorted.length * 0.50)] / 1000.0;
|
|
double p99 = sorted[(int) (sorted.length * 0.99)] / 1000.0;
|
|
double mean = Arrays.stream(sorted).average().orElse(0) / 1000.0;
|
|
return String.format("%-12s n=%-6d p50=%8.1f us p99=%9.1f us mean=%8.1f us ~%,.0f calls/s",
|
|
label, sorted.length, p50, p99, mean, 1_000_000.0 / mean);
|
|
}
|
|
}
|