use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.
the class TimerTest method recordWithRunnable.
@Test
@DisplayName("record a runnable task")
default void recordWithRunnable(MeterRegistry registry) {
Timer t = registry.timer("myTimer");
try {
t.record(() -> clock(registry).add(10, TimeUnit.NANOSECONDS));
clock(registry).add(step());
} finally {
assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS), 1.0e-12));
}
}
use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.
the class TimerTest method recordMax.
@Test
default void recordMax(MeterRegistry registry) {
Timer timer = registry.timer("my.timer");
timer.record(10, TimeUnit.MILLISECONDS);
timer.record(1, TimeUnit.SECONDS);
// for Atlas, which is step rather than ring-buffer based
clock(registry).add(step());
assertThat(timer.max(TimeUnit.SECONDS)).isEqualTo(1);
assertThat(timer.max(TimeUnit.MILLISECONDS)).isEqualTo(1000);
// noinspection ConstantConditions
clock(registry).add(Duration.ofMillis(step().toMillis() * DistributionStatisticConfig.DEFAULT.getBufferLength()));
assertThat(timer.max(TimeUnit.SECONDS)).isEqualTo(0);
}
use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.
the class TimerTest method recordDuration.
@Test
@DisplayName("record durations")
default void recordDuration(MeterRegistry registry) {
Timer t = registry.timer("myTimer");
t.record(Duration.ofMillis(42));
clock(registry).add(step());
assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12));
}
use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.
the class TimerTest method recordWithSample.
@Test
@DisplayName("record with stateful Sample instance")
default void recordWithSample(MeterRegistry registry) {
Timer timer = registry.timer("myTimer");
Timer.Sample sample = Timer.start(registry);
clock(registry).add(10, TimeUnit.NANOSECONDS);
sample.stop(timer);
clock(registry).add(step());
assertAll(() -> assertEquals(1L, timer.count()), () -> assertEquals(10, timer.totalTime(TimeUnit.NANOSECONDS), 1.0e-12));
}
use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.
the class FunctionTimerSample method main.
// For Atlas: http://localhost:7101/api/v1/graph?q=name,ftimer,:eq,:dist-avg,name,timer,:eq,:dist-avg,1,:axis&s=e-5m&l=0
public static void main(String[] args) {
MeterRegistry registry = SampleConfig.myMonitoringSystem();
Timer timer = Timer.builder("timer").publishPercentiles(0.5, 0.95).register(registry);
Object placeholder = new Object();
AtomicLong totalTimeNanos = new AtomicLong(0);
AtomicLong totalCount = new AtomicLong(0);
FunctionTimer.builder("ftimer", placeholder, p -> totalCount.get(), p -> totalTimeNanos.get(), TimeUnit.NANOSECONDS).register(registry);
RandomEngine r = new MersenneTwister64(0);
Normal incomingRequests = new Normal(0, 1, r);
Normal duration = new Normal(250, 50, r);
AtomicInteger latencyForThisSecond = new AtomicInteger(duration.nextInt());
Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond.set(duration.nextInt())).subscribe();
// the potential for an "incoming request" every 10 ms
Flux.interval(Duration.ofMillis(10)).doOnEach(d -> {
if (incomingRequests.nextDouble() + 0.4 > 0) {
// pretend the request took some amount of time, such that the time is
// distributed normally with a mean of 250ms
timer.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS);
totalCount.incrementAndGet();
totalTimeNanos.addAndGet((long) TimeUtils.millisToUnit(latencyForThisSecond.get(), TimeUnit.NANOSECONDS));
}
}).blockLast();
}
Aggregations