@Override public long windowSize(MetricConfig config, long now) { stat.purgeObsoleteSamples(config, now); long elapsed = now - stat.oldest(now).lastWindowMs; return elapsed < config.timeWindowMs() ? config.timeWindowMs() : elapsed; } }
public boolean isComplete(long timeMs, MetricConfig config) { return timeMs - lastWindowMs >= config.timeWindowMs() || eventCount >= config.eventWindow(); } }
protected void purgeObsoleteSamples(MetricConfig config, long now) { long expireAge = config.samples() * config.timeWindowMs(); for (Sample sample : samples) { if (now - sample.lastWindowMs >= expireAge) sample.reset(now); } }
public long windowSize(MetricConfig config, long now) { // purge old samples before we compute the window size stat.purgeObsoleteSamples(config, now); /* * Here we check the total amount of time elapsed since the oldest non-obsolete window. * This give the total windowSize of the batch which is the time used for Rate computation. * However, there is an issue if we do not have sufficient data for e.g. if only 1 second has elapsed in a 30 second * window, the measured rate will be very high. * Hence we assume that the elapsed time is always N-1 complete windows plus whatever fraction of the final window is complete. * * Note that we could simply count the amount of time elapsed in the current window and add n-1 windows to get the total time, * but this approach does not account for sleeps. SampledStat only creates samples whenever record is called, * if no record is called for a period of time that time is not accounted for in windowSize and produces incorrect results. */ long totalElapsedTimeMs = now - stat.oldest(now).lastWindowMs; // Check how many full windows of data we have currently retained int numFullWindows = (int) (totalElapsedTimeMs / config.timeWindowMs()); int minFullWindows = config.samples() - 1; // If the available windows are less than the minimum required, add the difference to the totalElapsedTime if (numFullWindows < minFullWindows) totalElapsedTimeMs += (minFullWindows - numFullWindows) * config.timeWindowMs(); return totalElapsedTimeMs; }
s.record(100); sum += 100; time.sleep(cfg.timeWindowMs()); assertEquals(sum, (Double) totalMetric.metricValue(), EPS); time.sleep(cfg.timeWindowMs() / 2); double elapsedSecs = (cfg.timeWindowMs() * (cfg.samples() - 1) + cfg.timeWindowMs() / 2) / 1000.0; time.sleep(cfg.timeWindowMs() * cfg.samples()); assertEquals(0, (Double) rateMetric.metricValue(), EPS); assertEquals(0, (Double) countRateMetric.metricValue(), EPS);
double elapsedSecs = (config.timeWindowMs() * (config.samples() - 1)) / 1000.0; assertEquals(String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs), count / elapsedSecs, metricValueFunc.apply(metrics.metrics().get(metrics.metricName("test.occurences", "grp1"))), EPS);
public boolean isComplete(long timeMs, MetricConfig config) { return timeMs - lastWindowMs >= config.timeWindowMs() || eventCount >= config.eventWindow(); } }
protected void purgeObsoleteSamples(MetricConfig config, long now) { long expireAge = config.samples() * config.timeWindowMs(); for (int i = 0; i < samples.size(); i++) { Sample sample = this.samples.get(i); if (now - sample.lastWindowMs >= expireAge) sample.reset(now); } }