Java Code Examples for io.micrometer.core.instrument.MockClock#add()

The following examples show how to use io.micrometer.core.instrument.MockClock#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StepTimerTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Issue("#1814")
@Test
void meanShouldWorkIfTotalTimeNotCalled() {
    Duration stepDuration = Duration.ofMillis(10);
    MockClock clock = new MockClock();
    StepTimer timer = new StepTimer(
            mock(Meter.Id.class),
            clock,
            DistributionStatisticConfig.builder().expiry(stepDuration).bufferLength(2).build(),
            mock(PauseDetector.class),
            TimeUnit.MILLISECONDS,
            stepDuration.toMillis(),
            false
    );

    clock.add(stepDuration);
    assertThat(timer.mean(TimeUnit.MILLISECONDS)).isEqualTo(0.0);

    clock.add(Duration.ofMillis(1));
    timer.record(Duration.ofMillis(50));
    timer.record(Duration.ofMillis(100));

    clock.add(stepDuration);
    assertThat(timer.mean(TimeUnit.MILLISECONDS)).isEqualTo(75.0);
}
 
Example 2
Source File: StepDistributionSummaryTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Issue("#1814")
@Test
void meanShouldWorkIfTotalNotCalled() {
    Duration stepDuration = Duration.ofMillis(10);
    MockClock clock = new MockClock();
    StepDistributionSummary summary = new StepDistributionSummary(
            mock(Meter.Id.class),
            clock,
            DistributionStatisticConfig.builder().expiry(stepDuration).bufferLength(2).build(),
            1.0,
            stepDuration.toMillis(),
            false
    );

    clock.add(stepDuration);
    assertThat(summary.mean()).isEqualTo(0.0);

    clock.add(Duration.ofMillis(1));
    summary.record(50);
    summary.record(100);

    clock.add(stepDuration);
    assertThat(summary.mean()).isEqualTo(75.0);
}
 
Example 3
Source File: StepFunctionTimerTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void totalTimeWhenStateObjectChangedToNullShouldWorkWithChangedTimeUnit() {
    MockClock clock = new MockClock();
    StepFunctionTimer<Object> functionTimer = new StepFunctionTimer<>(
            mock(Meter.Id.class), clock, 1000L, new Object(), (o) -> 1L, (o) -> 1d, TimeUnit.SECONDS, TimeUnit.SECONDS
    );
    clock.add(Duration.ofSeconds(1));
    assertThat(functionTimer.totalTime(TimeUnit.SECONDS)).isEqualTo(1d);
    assertThat(functionTimer.totalTime(TimeUnit.MILLISECONDS)).isEqualTo(1000d);
    System.gc();
    assertThat(functionTimer.totalTime(TimeUnit.MILLISECONDS)).isEqualTo(1000d);
    assertThat(functionTimer.totalTime(TimeUnit.SECONDS)).isEqualTo(1d);
}
 
Example 4
Source File: StepFunctionTimerTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Issue("#1814")
@Test
void meanShouldWorkIfTotalNotCalled() {
    Queue<Long> counts = new LinkedList<>();
    counts.add(2L);
    counts.add(5L);
    counts.add(10L);

    Queue<Double> totalTimes = new LinkedList<>();
    totalTimes.add(150.0);
    totalTimes.add(300.0);
    totalTimes.add(1000.0);

    Duration stepDuration = Duration.ofMillis(10);
    MockClock clock = new MockClock();
    StepFunctionTimer<Object> ft = new StepFunctionTimer<>(
            mock(Meter.Id.class),
            clock,
            stepDuration.toMillis(),
            new Object(),
            (o) -> counts.poll(),
            (o) -> totalTimes.poll(),
            TimeUnit.SECONDS,
            TimeUnit.SECONDS
    );

    assertThat(ft.count()).isEqualTo(0.0);

    clock.add(stepDuration);
    assertThat(ft.mean(TimeUnit.SECONDS)).isEqualTo(300.0 / 5L);

    clock.add(stepDuration);
    assertThat(ft.mean(TimeUnit.SECONDS)).isEqualTo(700.0 / 5L);
}
 
Example 5
Source File: FluxMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollTracksOnNext() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	FluxMetricsFuseable.MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new FluxMetricsFuseable.MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, "foo", Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	clock.add(Duration.ofMillis(200));

	Integer val1 = fuseableSubscriber.poll();
	Integer val2 = fuseableSubscriber.poll();

	assertThat(val1).isEqualTo(1);
	assertThat(val2).isNull();

	//test meters
	Timer nextTimer = registry.find(METER_ON_NEXT_DELAY)
			.timer();

	assertThat(nextTimer).isNotNull();
	assertThat(nextTimer.max(TimeUnit.MILLISECONDS)).as("onNext max delay").isEqualTo(200);
}
 
Example 6
Source File: FluxMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollSyncTracksOnComplete() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	FluxMetricsFuseable.MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new FluxMetricsFuseable.MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, "foo", Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	fuseableSubscriber.requestFusion(Fuseable.SYNC);

	clock.add(Duration.ofMillis(200));
	Integer val1 = fuseableSubscriber.poll();
	clock.add(Duration.ofMillis(123));
	Integer val2 = fuseableSubscriber.poll();

	assertThat(val1).isEqualTo(1);
	assertThat(val2).isNull();

	//test meters
	Timer terminationTimer = registry.find(METER_FLOW_DURATION)
	                          .tags(Tags.of(TAG_ON_COMPLETE))
	                          .timer();

	assertThat(terminationTimer).isNotNull();
	assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323);
}
 
Example 7
Source File: FluxMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollError() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	FluxMetricsFuseable.MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new FluxMetricsFuseable.MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, "foo", Tags.empty());

	FluxPeekFuseableTest.AssertQueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.setCompleteWithError(true);
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	fuseableSubscriber.requestFusion(Fuseable.SYNC);

	clock.add(Duration.ofMillis(200));
	Integer val1 = fuseableSubscriber.poll();
	assertThat(val1).isEqualTo(1);

	clock.add(Duration.ofMillis(123));
	assertThatIllegalStateException().isThrownBy(fuseableSubscriber::poll)
	                                 .withMessage("AssertQueueSubscriber poll error");

	//test meters
	Timer terminationTimer = registry.find(METER_FLOW_DURATION)
	                          .tags(Tags.of(TAG_ON_ERROR))
	                          .timer();

	assertThat(terminationTimer).isNotNull();
	assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323);
}
 
Example 8
Source File: MonoMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollDoesntTrackOnNext() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	clock.add(Duration.ofMillis(200));

	Integer val1 = fuseableSubscriber.poll();
	Integer val2 = fuseableSubscriber.poll();

	assertThat(val1).isEqualTo(1);
	assertThat(val2).isNull();

	//test meters
	Timer nextTimer = registry.find(METER_ON_NEXT_DELAY)
			.timer();

	assertThat(nextTimer).as("no onNext delay meter for Mono").isNull();
}
 
Example 9
Source File: MonoMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollSyncTracksOnComplete() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	fuseableSubscriber.requestFusion(Fuseable.SYNC);

	clock.add(Duration.ofMillis(200));
	Integer val1 = fuseableSubscriber.poll();
	clock.add(Duration.ofMillis(123));
	Integer val2 = fuseableSubscriber.poll();

	assertThat(val1).isEqualTo(1);
	assertThat(val2).isNull();

	//test meters
	Timer terminationTimer = registry.find(METER_FLOW_DURATION)
	                          .tags(Tags.of(TAG_ON_COMPLETE))
	                          .timer();

	assertThat(terminationTimer).isNotNull();
	assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(200);
}
 
Example 10
Source File: MonoMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollError() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, Tags.empty());

	FluxPeekFuseableTest.AssertQueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.setCompleteWithError(true);
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	fuseableSubscriber.requestFusion(Fuseable.SYNC);

	clock.add(Duration.ofMillis(200));
	Integer val1 = fuseableSubscriber.poll();
	assertThat(val1).isEqualTo(1);

	clock.add(Duration.ofMillis(123));
	assertThatIllegalStateException().isThrownBy(fuseableSubscriber::poll)
	                                 .withMessage("AssertQueueSubscriber poll error");

	//test meters
	Timer terminationTimer = registry.find(METER_FLOW_DURATION)
	                          .tags(Tags.of(TAG_ON_ERROR))
	                          .timer();

	assertThat(terminationTimer).isNotNull();
	assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323);
}
 
Example 11
Source File: TimeWindowPercentileHistogramTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Test
void timeBasedSlidingWindow() {
    final DistributionStatisticConfig config =
            DistributionStatisticConfig.builder()
                    .percentiles(0.0, 0.5, 0.75, 0.9, 0.99, 0.999, 1.0)
                    .expiry(Duration.ofSeconds(4))
                    .bufferLength(4)
                    .build()
                    .merge(DistributionStatisticConfig.DEFAULT);

    MockClock clock = new MockClock();
    // Start from 0 for more comprehensive timing calculation.
    clock.add(-1, TimeUnit.NANOSECONDS);
    assertThat(clock.wallTime()).isZero();

    Histogram histogram = new TimeWindowPercentileHistogram(clock, config, false);

    histogram.recordLong(10);
    histogram.recordLong(20);
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(9.0, 11.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(19.0, 21.0);

    clock.add(900, TimeUnit.MILLISECONDS); // 900
    histogram.recordLong(30);
    histogram.recordLong(40);
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(9.0, 11.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(38.0, 42.0);

    clock.add(99, TimeUnit.MILLISECONDS); // 999
    histogram.recordLong(9);
    histogram.recordLong(60);
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(58.0, 62.0);

    clock.add(1, TimeUnit.MILLISECONDS); // 1000
    histogram.recordLong(12);
    histogram.recordLong(70);
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(68.0, 72.0);

    clock.add(1001, TimeUnit.MILLISECONDS); // 2001
    histogram.recordLong(13);
    histogram.recordLong(80);
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(75.0, 85.0);

    clock.add(1000, TimeUnit.MILLISECONDS); // 3001
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(75.0, 85.0);

    clock.add(999, TimeUnit.MILLISECONDS); // 4000
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(11.0, 13.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(75.0, 85.0);
    histogram.recordLong(1);
    histogram.recordLong(200);
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(0.0, 2.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(190.0, 210.0);

    clock.add(10000, TimeUnit.MILLISECONDS); // 14000
    assertThat(percentileValue(histogram, 0.0)).isZero();
    assertThat(percentileValue(histogram, 1.0)).isZero();
    histogram.recordLong(3);

    clock.add(3999, TimeUnit.MILLISECONDS); // 17999
    assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(2.0, 4.0);
    assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(2.0, 4.0);

    clock.add(1, TimeUnit.MILLISECONDS); // 18000
    assertThat(percentileValue(histogram, 0.0)).isZero();
    assertThat(percentileValue(histogram, 1.0)).isZero();
}
 
Example 12
Source File: AtlasMeterRegistryTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Issue("#2094")
@Test
void functionCounter() {
    AtomicLong count = new AtomicLong();

    MockClock clock = new MockClock();
    AtlasMeterRegistry registry = new AtlasMeterRegistry(new AtlasConfig() {
        @Nullable
        @Override
        public String get(String k) {
            return null;
        }

        @Override
        public Duration step() {
            return Duration.ofMinutes(1);
        }

        @Override
        public Duration lwcStep() {
            return step();
        }
    }, clock);
    FunctionCounter.builder("test", count, AtomicLong::doubleValue).register(registry);

    Supplier<Double> valueSupplier = () -> {
        AtlasRegistry r = (AtlasRegistry) registry.getSpectatorRegistry();
        PolledMeter.update(r);
        clock.add(Duration.ofMinutes(1));
        return r.measurements()
                .filter(m -> m.id().name().equals("test"))
                .findFirst()
                .map(Measurement::value)
                .orElse(Double.NaN);
    };

    count.addAndGet(60);
    assertThat(valueSupplier.get()).isEqualTo(1.0);

    count.addAndGet(120);
    assertThat(valueSupplier.get()).isEqualTo(2.0);

    count.addAndGet(90);
    assertThat(valueSupplier.get()).isEqualTo(1.5);
}