From 90e21c67875ecc1afcd4c1734b00fcf5d1de9f34 Mon Sep 17 00:00:00 2001 From: Sanketh I Date: Wed, 17 Nov 2021 17:06:57 -0800 Subject: [PATCH] Output p99 on each line --- .../sample/common/metrics/Metric.java | 2 +- .../common/metrics/ReadableStatsMetric.java | 76 ++++++++++--------- .../sample/common/metrics/StatsTracker.java | 34 ++++----- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/yugabyte/sample/common/metrics/Metric.java b/src/main/java/com/yugabyte/sample/common/metrics/Metric.java index 26a093e..c906090 100644 --- a/src/main/java/com/yugabyte/sample/common/metrics/Metric.java +++ b/src/main/java/com/yugabyte/sample/common/metrics/Metric.java @@ -25,7 +25,7 @@ public Metric(String name) { } public synchronized void observe(Observation o) { - readableStatsMetric.accumulate(o.getCount(), o.getLatencyNanos()); + readableStatsMetric.observe(o); jsonStatsMetric.observe(o); } diff --git a/src/main/java/com/yugabyte/sample/common/metrics/ReadableStatsMetric.java b/src/main/java/com/yugabyte/sample/common/metrics/ReadableStatsMetric.java index f4ea5bf..9c0e82e 100644 --- a/src/main/java/com/yugabyte/sample/common/metrics/ReadableStatsMetric.java +++ b/src/main/java/com/yugabyte/sample/common/metrics/ReadableStatsMetric.java @@ -16,41 +16,49 @@ import org.apache.log4j.Logger; public class ReadableStatsMetric { - private static final Logger LOG = Logger.getLogger(ReadableStatsMetric.class); - String name; - private final Object lock = new Object(); - protected long curOpCount = 0; - protected long curOpLatencyNanos = 0; - protected long totalOpCount = 0; - private long lastSnapshotNanos; + private static final Logger LOG = Logger.getLogger(ReadableStatsMetric.class); + String name; + private final Object lock = new Object(); + protected long curOpCount = 0; + protected long curOpLatencyNanos = 0; + protected long totalOpCount = 0; + private long lastSnapshotNanos; + private StatsTracker quantileStats; - public ReadableStatsMetric(String name) { - this.name = name; - lastSnapshotNanos = System.nanoTime(); - } + public ReadableStatsMetric(String name) { + this.name = name; + lastSnapshotNanos = System.nanoTime(); + quantileStats = new StatsTracker(); + } - /** - * Accumulate metrics with operations processed as one batch. - * @param numOps number of ops processed as one batch - * @param batchLatencyNanos whole batch latency - */ - public synchronized void accumulate(long numOps, long batchLatencyNanos) { - curOpCount += numOps; - curOpLatencyNanos += batchLatencyNanos * numOps; - totalOpCount += numOps; - } + public synchronized void observe(Observation o) { + accumulate(o.getCount(), o.getLatencyNanos()); + quantileStats.observe(o.getLatencyMillis()); + } - public synchronized String getMetricsAndReset() { - long currNanos = System.nanoTime(); - long elapsedNanos = currNanos - lastSnapshotNanos; - LOG.debug("currentOpLatency: " + curOpLatencyNanos + ", currentOpCount: " + curOpCount); - double ops_per_sec = - (elapsedNanos == 0) ? 0 : (curOpCount * 1000000000 * 1.0 / elapsedNanos); - double latency = (curOpCount == 0) ? 0 : (curOpLatencyNanos / 1000000 * 1.0 / curOpCount); - curOpCount = 0; - curOpLatencyNanos = 0; - lastSnapshotNanos = currNanos; - return String.format("%s: %.2f ops/sec (%.2f ms/op), %d total ops", - name, ops_per_sec, latency, totalOpCount); - } + /** + * Accumulate metrics with operations processed as one batch. + * + * @param numOps number of ops processed as one batch + * @param batchLatencyNanos whole batch latency + */ + public synchronized void accumulate(long numOps, long batchLatencyNanos) { + curOpCount += numOps; + curOpLatencyNanos += batchLatencyNanos * numOps; + totalOpCount += numOps; + } + + public synchronized String getMetricsAndReset() { + long currNanos = System.nanoTime(); + long elapsedNanos = currNanos - lastSnapshotNanos; + LOG.debug("currentOpLatency: " + curOpLatencyNanos + ", currentOpCount: " + curOpCount); + double ops_per_sec = (elapsedNanos == 0) ? 0 : (curOpCount * 1000000000 * 1.0 / elapsedNanos); + double latency = (curOpCount == 0) ? 0 : (curOpLatencyNanos / 1000000 * 1.0 / curOpCount); + curOpCount = 0; + curOpLatencyNanos = 0; + lastSnapshotNanos = currNanos; + return String.format( + "%s: %.2f ops/sec (%.2f ms/op, %.2f ms p99), %d total ops", + name, ops_per_sec, latency, quantileStats.stats.getPercentile(99), totalOpCount); + } } diff --git a/src/main/java/com/yugabyte/sample/common/metrics/StatsTracker.java b/src/main/java/com/yugabyte/sample/common/metrics/StatsTracker.java index 78cec6d..2412ea2 100644 --- a/src/main/java/com/yugabyte/sample/common/metrics/StatsTracker.java +++ b/src/main/java/com/yugabyte/sample/common/metrics/StatsTracker.java @@ -4,24 +4,24 @@ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; public class StatsTracker { - private DescriptiveStatistics stats; + public DescriptiveStatistics stats; - public StatsTracker() { - this.stats = new DescriptiveStatistics(); - } + public StatsTracker() { + this.stats = new DescriptiveStatistics(); + } - public synchronized void observe(double value) { - stats.addValue(value); - } + public synchronized void observe(double value) { + stats.addValue(value); + } - public synchronized JsonObject getJson() { - JsonObject json = new JsonObject(); - json.addProperty("mean", stats.getMean()); - json.addProperty("variance", stats.getVariance()); - json.addProperty("sampleSize", stats.getN()); - json.addProperty("min", stats.getMin()); - json.addProperty("max", stats.getMax()); - json.addProperty("p99", stats.getPercentile(99)); - return json; - } + public synchronized JsonObject getJson() { + JsonObject json = new JsonObject(); + json.addProperty("mean", stats.getMean()); + json.addProperty("variance", stats.getVariance()); + json.addProperty("sampleSize", stats.getN()); + json.addProperty("min", stats.getMin()); + json.addProperty("max", stats.getMax()); + json.addProperty("p99", stats.getPercentile(99)); + return json; + } }