@Override public void run() { synchronized (parent) { final MetricsCollectorImpl mc = new MetricsCollectorImpl(); final MetricsRecordBuilder rb = mc.addRecord("RatesRoller"); /** * snapshot all metrics regardless of being changed or not, in case no * ops since last snapshot, we will get 0. */ parent.innerMetrics.snapshot(rb, true); Preconditions.checkState(mc.getRecords().size() == 1, "There must be only one record and it's named with 'RatesRoller'"); parent.currentSnapshot = parent.innerMetrics.getGlobalMetrics(); parent.rollOverAvgs(); } parent.setChanged(); } }
private static void snapshotMutableRatesWithAggregation( MutableRatesWithAggregation rates, long[] opCount, double[] opTotalTime) { MetricsRecordBuilder rb = mockMetricsRecordBuilder(); rates.snapshot(rb, true); for (int i = 0; i < opCount.length; i++) { long prevOpCount = opCount[i]; long newOpCount = getLongCounter("Metric" + i + "NumOps", rb); opCount[i] = newOpCount; double avgTime = getDoubleGauge("Metric" + i + "AvgTime", rb); opTotalTime[i] += avgTime * (newOpCount - prevOpCount); } }
@Test public void testMutableRatesWithAggregationInit() { MetricsRecordBuilder rb = mockMetricsRecordBuilder(); MutableRatesWithAggregation rates = new MutableRatesWithAggregation(); rates.init(TestProtocol.class); rates.snapshot(rb, false); assertCounter("FooNumOps", 0L, rb); assertGauge("FooAvgTime", 0.0, rb); assertCounter("BarNumOps", 0L, rb); assertGauge("BarAvgTime", 0.0, rb); }
@Test public void testMutableRatesWithAggregationSingleThread() { MutableRatesWithAggregation rates = new MutableRatesWithAggregation(); rates.add("foo", 1); rates.add("bar", 5); MetricsRecordBuilder rb = mockMetricsRecordBuilder(); rates.snapshot(rb, false); assertCounter("FooNumOps", 1L, rb); assertGauge("FooAvgTime", 1.0, rb); assertCounter("BarNumOps", 1L, rb); assertGauge("BarAvgTime", 5.0, rb); rates.add("foo", 1); rates.add("foo", 3); rates.add("bar", 6); rb = mockMetricsRecordBuilder(); rates.snapshot(rb, false); assertCounter("FooNumOps", 3L, rb); assertGauge("FooAvgTime", 2.0, rb); assertCounter("BarNumOps", 2L, rb); assertGauge("BarAvgTime", 6.0, rb); }
rates.snapshot(rb, true);