SlidingTimeWindowMetrics implements Metrics { @Override public synchronized Snapshot record(long duration, TimeUnit durationUnit, Outcome outcome) { totalAggregation.record(duration, durationUnit, outcome); moveWindowToCurrentEpochSecond(getLatestPartialAggregation()) .record(duration, durationUnit, outcome); return new SnapshotImpl(totalAggregation); } SlidingTimeWindowMetrics(int timeWindowSizeInSeconds, Clock clock); @Override synchronized Snapshot record(long duration, TimeUnit durationUnit, Outcome outcome); synchronized Snapshot getSnapshot();  }
@Test public void testRecordSuccess() { MockClock clock = MockClock.at(2019, 8, 4, 12, 0, 0, ZoneId.of("UTC")); Metrics metrics = new SlidingTimeWindowMetrics(5, clock); Snapshot snapshot = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(snapshot.getTotalNumberOfCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfFailedCalls()).isEqualTo(0); assertThat(snapshot.getTotalNumberOfSlowCalls()).isEqualTo(0); assertThat(snapshot.getNumberOfSlowSuccessfulCalls()).isEqualTo(0); assertThat(snapshot.getNumberOfSlowFailedCalls()).isEqualTo(0); assertThat(snapshot.getTotalDuration().toMillis()).isEqualTo(100); assertThat(snapshot.getAverageDuration().toMillis()).isEqualTo(100); assertThat(snapshot.getFailureRate()).isEqualTo(0); }
@Test public void testRecordError() { MockClock clock = MockClock.at(2019, 8, 4, 12, 0, 0, ZoneId.of("UTC")); Metrics metrics = new SlidingTimeWindowMetrics(5, clock); Snapshot snapshot = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.ERROR); assertThat(snapshot.getTotalNumberOfCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfSuccessfulCalls()).isEqualTo(0); assertThat(snapshot.getNumberOfFailedCalls()).isEqualTo(1); assertThat(snapshot.getTotalNumberOfSlowCalls()).isEqualTo(0); assertThat(snapshot.getNumberOfSlowSuccessfulCalls()).isEqualTo(0); assertThat(snapshot.getNumberOfSlowFailedCalls()).isEqualTo(0); assertThat(snapshot.getTotalDuration().toMillis()).isEqualTo(100); assertThat(snapshot.getAverageDuration().toMillis()).isEqualTo(100); assertThat(snapshot.getFailureRate()).isEqualTo(100); }
@Test public void testRecordSlowSuccess() { MockClock clock = MockClock.at(2019, 8, 4, 12, 0, 0, ZoneId.of("UTC")); Metrics metrics = new SlidingTimeWindowMetrics(5, clock); Snapshot snapshot = metrics .record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SLOW_SUCCESS); assertThat(snapshot.getTotalNumberOfCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfFailedCalls()).isEqualTo(0); assertThat(snapshot.getTotalNumberOfSlowCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfSlowSuccessfulCalls()).isEqualTo(1); assertThat(snapshot.getNumberOfSlowFailedCalls()).isEqualTo(0); assertThat(snapshot.getTotalDuration().toMillis()).isEqualTo(100); assertThat(snapshot.getAverageDuration().toMillis()).isEqualTo(100); assertThat(snapshot.getFailureRate()).isEqualTo(0); }