SlidingTimeWindowMetrics implements Metrics { public SlidingTimeWindowMetrics(int timeWindowSizeInSeconds, Clock clock) { this.clock = clock; this.timeWindowSizeInSeconds = timeWindowSizeInSeconds; this.partialAggregations = new PartialAggregation[timeWindowSizeInSeconds]; this.headIndex = 0; long epochSecond = clock.instant().getEpochSecond(); for (int i = 0; i < timeWindowSizeInSeconds; i++) { partialAggregations[i] = new PartialAggregation(epochSecond); epochSecond++; } this.totalAggregation = new TotalAggregation(); } SlidingTimeWindowMetrics(int timeWindowSizeInSeconds, Clock clock); @Override synchronized Snapshot record(long duration, TimeUnit durationUnit, Outcome outcome); synchronized Snapshot getSnapshot();  }
@Test public void testSlidingTimeWindowMetrics() { MockClock clock = MockClock.at(2019, 8, 4, 12, 0, 0, ZoneId.of("UTC")); Metrics metrics = new SlidingTimeWindowMetrics(5, clock); Snapshot result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.ERROR); assertThat(result.getTotalNumberOfCalls()).isEqualTo(1); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(0); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(100); clock.advanceByMillis(100); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(2); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(200); clock.advanceByMillis(100); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(3); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(2); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(300); clock.advanceBySeconds(1); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(4); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(3); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(400); clock.advanceBySeconds(1); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(5); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(4); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(500); clock.advanceBySeconds(1); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.ERROR); assertThat(result.getTotalNumberOfCalls()).isEqualTo(6); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(4); assertThat(result.getNumberOfFailedCalls()).isEqualTo(2); assertThat(result.getTotalDuration().toMillis()).isEqualTo(600); clock.advanceBySeconds(1); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(7); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(5); assertThat(result.getNumberOfFailedCalls()).isEqualTo(2); assertThat(result.getTotalDuration().toMillis()).isEqualTo(700); clock.advanceBySeconds(1); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(5); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(4); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(500); clock.advanceBySeconds(1); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(5); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(4); assertThat(result.getNumberOfFailedCalls()).isEqualTo(1); assertThat(result.getTotalDuration().toMillis()).isEqualTo(500); clock.advanceBySeconds(5); result = metrics.record(100, TimeUnit.MILLISECONDS, Metrics.Outcome.SUCCESS); assertThat(result.getTotalNumberOfCalls()).isEqualTo(1); assertThat(result.getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(result.getNumberOfFailedCalls()).isEqualTo(0); assertThat(result.getTotalDuration().toMillis()).isEqualTo(100); }