LogThrottlingHelper { public LogAction record(double... values) { return record(DEFAULT_RECORDER_NAME, timer.monotonicNow(), values); } LogThrottlingHelper(long minLogPeriodMs);  LogThrottlingHelper(long minLogPeriodMs, String primaryRecorderName); @VisibleForTesting  LogThrottlingHelper(long minLogPeriodMs, String primaryRecorderName,
      Timer timer); LogAction record(double... values); LogAction record(String recorderName, long currentTimeMs,
      double... values); static final LogAction DO_NOT_LOG; }
@Test public void testBasicLogging() { assertTrue(helper.record().shouldLog()); for (int i = 0; i < 5; i++) { timer.advance(LOG_PERIOD / 10); assertFalse(helper.record().shouldLog()); } timer.advance(LOG_PERIOD); assertTrue(helper.record().shouldLog()); }
@Test public void testLoggingWithValue() { assertTrue(helper.record(1).shouldLog()); for (int i = 0; i < 4; i++) { timer.advance(LOG_PERIOD / 5); assertFalse(helper.record(i % 2 == 0 ? 0 : 1).shouldLog()); } timer.advance(LOG_PERIOD); LogAction action = helper.record(0.5); assertTrue(action.shouldLog()); assertEquals(5, action.getCount()); assertEquals(0.5, action.getStats(0).getMean(), 0.01); assertEquals(1.0, action.getStats(0).getMax(), 0.01); assertEquals(0.0, action.getStats(0).getMin(), 0.01); }
@Test public void testLoggingWithMultipleValues() { assertTrue(helper.record(1).shouldLog()); for (int i = 0; i < 4; i++) { timer.advance(LOG_PERIOD / 5); int base = i % 2 == 0 ? 0 : 1; assertFalse(helper.record(base, base * 2).shouldLog()); } timer.advance(LOG_PERIOD); LogAction action = helper.record(0.5, 1.0); assertTrue(action.shouldLog()); assertEquals(5, action.getCount()); for (int i = 1; i <= 2; i++) { assertEquals(0.5 * i, action.getStats(i - 1).getMean(), 0.01); assertEquals(1.0 * i, action.getStats(i - 1).getMax(), 0.01); assertEquals(0.0, action.getStats(i - 1).getMin(), 0.01); } }
@Test(expected = IllegalArgumentException.class) public void testLoggingWithInconsistentValues() { assertTrue(helper.record(1, 2).shouldLog()); helper.record(1, 2); helper.record(1, 2, 3); }
@Test public void testNamedLoggersWithoutSpecifiedPrimary() { assertTrue(helper.record("foo", 0).shouldLog()); assertTrue(helper.record("bar", 0).shouldLog()); assertFalse(helper.record("foo", LOG_PERIOD / 2).shouldLog()); assertFalse(helper.record("bar", LOG_PERIOD / 2).shouldLog()); assertTrue(helper.record("foo", LOG_PERIOD).shouldLog()); assertTrue(helper.record("bar", LOG_PERIOD).shouldLog()); assertFalse(helper.record("foo", (LOG_PERIOD * 3) / 2).shouldLog()); assertFalse(helper.record("bar", (LOG_PERIOD * 3) / 2).shouldLog()); assertFalse(helper.record("bar", LOG_PERIOD * 2).shouldLog()); assertTrue(helper.record("foo", LOG_PERIOD * 2).shouldLog()); assertTrue(helper.record("bar", LOG_PERIOD * 2).shouldLog()); }
@Test public void testPrimaryAndDependentLoggers() { helper = new LogThrottlingHelper(LOG_PERIOD, "foo", timer); assertTrue(helper.record("foo", 0).shouldLog()); assertTrue(helper.record("bar", 0).shouldLog()); assertFalse(helper.record("bar", 0).shouldLog()); assertFalse(helper.record("foo", 0).shouldLog()); assertFalse(helper.record("foo", LOG_PERIOD / 2).shouldLog()); assertFalse(helper.record("bar", LOG_PERIOD / 2).shouldLog()); assertTrue(helper.record("foo", LOG_PERIOD).shouldLog()); assertTrue(helper.record("bar", LOG_PERIOD).shouldLog()); assertFalse(helper.record("bar", LOG_PERIOD * 2).shouldLog()); assertTrue(helper.record("foo", LOG_PERIOD * 2).shouldLog()); assertTrue(helper.record("bar", 0).shouldLog()); }
@Test public void testMultipleLoggersWithValues() { helper = new LogThrottlingHelper(LOG_PERIOD, "foo", timer); assertTrue(helper.record("foo", 0).shouldLog()); assertTrue(helper.record("bar", 0, 2).shouldLog()); assertTrue(helper.record("baz", 0, 3, 3).shouldLog()); assertFalse(helper.record("bar", LOG_PERIOD, 2).shouldLog()); assertFalse(helper.record("baz", LOG_PERIOD, 3, 3).shouldLog()); LogAction foo = helper.record("foo", LOG_PERIOD); LogAction bar = helper.record("bar", LOG_PERIOD, 2); LogAction baz = helper.record("baz", LOG_PERIOD, 3, 3); assertTrue(foo.shouldLog()); assertTrue(bar.shouldLog()); assertTrue(baz.shouldLog()); assertEquals(1, foo.getCount()); assertEquals(2, bar.getCount()); assertEquals(2, baz.getCount()); assertEquals(2.0, bar.getStats(0).getMean(), 0.01); assertEquals(3.0, baz.getStats(0).getMean(), 0.01); assertEquals(3.0, baz.getStats(1).getMean(), 0.01); }