Search in sources :

Example 1 with MersenneTwister64

use of cern.jet.random.engine.MersenneTwister64 in project stream-lib by addthis.

the class QDigestTest method testComprehensiveOnMixture.

@Test
public void testComprehensiveOnMixture() {
    RandomEngine r = new MersenneTwister64(0);
    Normal[] dists = new Normal[] { new Normal(100, 50, r), new Normal(150, 20, r), new Normal(500, 300, r), new Normal(10000, 10000, r), new Normal(1200, 300, r) };
    for (int numSamples : new int[] { 1, 10, 100, 1000, 10000 }) {
        long[][] samples = new long[dists.length][];
        for (int i = 0; i < dists.length; ++i) {
            samples[i] = new long[numSamples];
            for (int j = 0; j < samples[i].length; ++j) {
                samples[i][j] = (long) Math.max(0, dists[i].nextDouble());
            }
        }
        double compressionFactor = 1000;
        int logCapacity = 1;
        long max = 0;
        for (long[] s : samples) {
            for (long x : s) max = Math.max(max, x);
        }
        for (double scale = 1; scale < max; scale *= 2, logCapacity++) {
            ;
        }
        double eps = logCapacity / compressionFactor;
        QDigest[] digests = new QDigest[dists.length];
        for (int i = 0; i < digests.length; ++i) {
            digests[i] = new QDigest(compressionFactor);
            for (long x : samples[i]) {
                digests[i].offer(x);
            }
            assertEquals(samples[i].length, digests[i].computeActualSize());
        }
        int numTotal = 0;
        for (int i = 0; i < digests.length; ++i) {
            for (double q = 0; q <= 1; q += 0.01) {
                long res = digests[i].getQuantile(q);
                double[] actualRank = actualRankOf(res, samples[i]);
                assertTrue(actualRank[0] + " .. " + actualRank[1] + " outside error bound for  " + q, q >= actualRank[0] - eps && q <= actualRank[1] + eps);
            }
            // Test the same on the union of all distributions up to i-th
            numTotal += samples[i].length;
            long[] total = new long[numTotal];
            int offset = 0;
            QDigest totalDigest = new QDigest(compressionFactor);
            long expectedSize = 0;
            for (int j = 0; j <= i; ++j) {
                System.arraycopy(samples[j], 0, total, offset, samples[j].length);
                offset += samples[j].length;
                totalDigest = QDigest.unionOf(totalDigest, digests[j]);
                expectedSize += samples[j].length;
            }
            assertEquals(expectedSize, totalDigest.computeActualSize());
            for (double q = 0; q <= 1; q += 0.01) {
                long res = totalDigest.getQuantile(q);
                double[] actualRank = actualRankOf(res, total);
                assertTrue(actualRank[0] + " .. " + actualRank[1] + " outside error bound for  " + q, q >= actualRank[0] - eps && q <= actualRank[1] + eps);
            }
        }
    }
}
Also used : MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) Test(org.junit.Test)

Example 2 with MersenneTwister64

use of cern.jet.random.engine.MersenneTwister64 in project micrometer by micrometer-metrics.

the class CounterSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    Counter counter = registry.counter("counter", "method", "actual");
    AtomicInteger n = new AtomicInteger(0);
    registry.more().counter("counter", Tags.of("method", "function"), n);
    RandomEngine r = new MersenneTwister64(0);
    Normal dist = new Normal(0, 1, r);
    Flux.interval(Duration.ofMillis(10)).doOnEach(d -> {
        if (dist.nextDouble() + 0.1 > 0) {
            counter.increment();
            n.incrementAndGet();
        }
    }).blockLast();
}
Also used : Counter(io.micrometer.core.instrument.Counter) Tags(io.micrometer.core.instrument.Tags) Flux(reactor.core.publisher.Flux) RandomEngine(cern.jet.random.engine.RandomEngine) SampleConfig(io.micrometer.core.samples.utils.SampleConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) Counter(io.micrometer.core.instrument.Counter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 3 with MersenneTwister64

use of cern.jet.random.engine.MersenneTwister64 in project micrometer by micrometer-metrics.

the class TimerMaximumThroughputSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    Timer timer = Timer.builder("timer").publishPercentileHistogram().sla(Duration.ofMillis(275), Duration.ofMillis(300), Duration.ofMillis(500)).distributionStatisticExpiry(Duration.ofSeconds(10)).distributionStatisticBufferLength(3).register(registry);
    RandomEngine r = new MersenneTwister64(0);
    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();
    Stream<Integer> infiniteStream = Stream.iterate(0, i -> (i + 1) % 1000);
    Flux.fromStream(infiniteStream).parallel(4).runOn(Schedulers.parallel()).doOnEach(d -> timer.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS)).subscribe();
    Flux.never().blockLast();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) Timer(io.micrometer.core.instrument.Timer) Stream(java.util.stream.Stream) RandomEngine(cern.jet.random.engine.RandomEngine) SampleConfig(io.micrometer.core.samples.utils.SampleConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) Schedulers(reactor.core.scheduler.Schedulers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timer(io.micrometer.core.instrument.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 4 with MersenneTwister64

use of cern.jet.random.engine.MersenneTwister64 in project micrometer by micrometer-metrics.

the class TimerSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    Timer timer = Timer.builder("timer").publishPercentileHistogram().publishPercentiles(0.5, 0.95, 0.99).sla(Duration.ofMillis(275), Duration.ofMillis(300), Duration.ofMillis(500)).distributionStatisticExpiry(Duration.ofSeconds(10)).distributionStatisticBufferLength(3).register(registry);
    FunctionTimer.builder("ftimer", timer, Timer::count, t -> t.totalTime(TimeUnit.SECONDS), TimeUnit.SECONDS).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);
        }
    }).blockLast();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) Timer(io.micrometer.core.instrument.Timer) RandomEngine(cern.jet.random.engine.RandomEngine) SampleConfig(io.micrometer.core.samples.utils.SampleConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) FunctionTimer(io.micrometer.core.instrument.FunctionTimer) Timer(io.micrometer.core.instrument.Timer) FunctionTimer(io.micrometer.core.instrument.FunctionTimer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 5 with MersenneTwister64

use of cern.jet.random.engine.MersenneTwister64 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();
}
Also used : SampleConfig(io.micrometer.core.samples.utils.SampleConfig) Normal(cern.jet.random.Normal) FunctionTimer(io.micrometer.core.instrument.FunctionTimer) TimeUtils(io.micrometer.core.instrument.util.TimeUtils) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Timer(io.micrometer.core.instrument.Timer) RandomEngine(cern.jet.random.engine.RandomEngine) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) AtomicLong(java.util.concurrent.atomic.AtomicLong) FunctionTimer(io.micrometer.core.instrument.FunctionTimer) Timer(io.micrometer.core.instrument.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Aggregations

Normal (cern.jet.random.Normal)7 MersenneTwister64 (cern.jet.random.engine.MersenneTwister64)7 RandomEngine (cern.jet.random.engine.RandomEngine)7 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)6 SampleConfig (io.micrometer.core.samples.utils.SampleConfig)6 Duration (java.time.Duration)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Flux (reactor.core.publisher.Flux)6 Timer (io.micrometer.core.instrument.Timer)4 TimeUnit (java.util.concurrent.TimeUnit)4 FunctionTimer (io.micrometer.core.instrument.FunctionTimer)2 Counter (io.micrometer.core.instrument.Counter)1 LongTaskTimer (io.micrometer.core.instrument.LongTaskTimer)1 Tags (io.micrometer.core.instrument.Tags)1 TimeUtils (io.micrometer.core.instrument.util.TimeUtils)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Stream (java.util.stream.Stream)1