Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output p99 latency on each line #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
34 changes: 17 additions & 17 deletions src/main/java/com/yugabyte/sample/common/metrics/StatsTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}