Search in sources :

Example 6 with Normal

use of cern.jet.random.Normal 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)

Example 7 with Normal

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

the class LongTaskTimerSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    LongTaskTimer timer = registry.more().longTaskTimer("longTaskTimer");
    RandomEngine r = new MersenneTwister64(0);
    Normal incomingRequests = new Normal(0, 1, r);
    Normal duration = new Normal(30, 50, r);
    AtomicInteger latencyForThisSecond = new AtomicInteger(duration.nextInt());
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond.set(duration.nextInt())).subscribe();
    final Map<LongTaskTimer.Sample, CountDownLatch> tasks = new ConcurrentHashMap<>();
    // the potential for an "incoming request" every 10 ms
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> {
        if (incomingRequests.nextDouble() + 0.4 > 0 && tasks.isEmpty()) {
            int taskDur;
            while ((taskDur = duration.nextInt()) < 0) ;
            synchronized (tasks) {
                tasks.put(timer.start(), new CountDownLatch(taskDur));
            }
        }
        synchronized (tasks) {
            for (Map.Entry<LongTaskTimer.Sample, CountDownLatch> e : tasks.entrySet()) {
                e.getValue().countDown();
                if (e.getValue().getCount() == 0) {
                    e.getKey().stop();
                    tasks.remove(e.getKey());
                }
            }
        }
    }).blockLast();
}
Also used : LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) 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) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Normal(cern.jet.random.Normal) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 8 with Normal

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

the class SimulatedEndpointInstrumentation method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    Timer e1Success = Timer.builder("http.server.requests").tags("uri", "/api/bar").tags("response", "200").publishPercentiles(0.5, 0.95).register(registry);
    Timer e2Success = Timer.builder("http.server.requests").tags("uri", "/api/foo").tags("response", "200").publishPercentiles(0.5, 0.95).register(registry);
    Timer e1Fail = Timer.builder("http.server.requests").tags("uri", "/api/bar").tags("response", "500").publishPercentiles(0.5, 0.95).register(registry);
    Timer e2Fail = Timer.builder("http.server.requests").tags("uri", "/api/foo").tags("response", "500").publishPercentiles(0.5, 0.95).register(registry);
    RandomEngine r = new MersenneTwister64(0);
    Normal incomingRequests = new Normal(0, 1, r);
    Normal successOrFail = new Normal(0, 1, r);
    Normal duration = new Normal(250, 50, r);
    Normal duration2 = new Normal(250, 50, r);
    AtomicInteger latencyForThisSecond = new AtomicInteger(duration.nextInt());
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond.set(duration.nextInt())).subscribe();
    AtomicInteger latencyForThisSecond2 = new AtomicInteger(duration2.nextInt());
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond2.set(duration2.nextInt())).subscribe();
    // the potential for an "incoming request" every 10 ms
    Flux.interval(Duration.ofMillis(10)).doOnEach(d -> {
        // are we going to receive a request for /api/foo?
        if (incomingRequests.nextDouble() + 0.4 > 0) {
            if (successOrFail.nextDouble() + 0.8 > 0) {
                // pretend the request took some amount of time, such that the time is
                // distributed normally with a mean of 250ms
                e1Success.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS);
            } else {
                e1Fail.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS);
            }
        }
    }).subscribe();
    // the potential for an "incoming request" every 1 ms
    Flux.interval(Duration.ofMillis(1)).doOnEach(d -> {
        // are we going to receive a request for /api/bar?
        if (incomingRequests.nextDouble() + 0.4 > 0) {
            if (successOrFail.nextDouble() + 0.8 > 0) {
                // pretend the request took some amount of time, such that the time is
                // distributed normally with a mean of 250ms
                e2Success.record(latencyForThisSecond2.get(), TimeUnit.MILLISECONDS);
            } else {
                e2Fail.record(latencyForThisSecond2.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) 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 9 with Normal

use of cern.jet.random.Normal in project processdash by dtuma.

the class LognormalConfidenceInterval method generateBootstrapSamples.

private double[] generateBootstrapSamples() {
    RandomEngine u = new MersenneTwister();
    Normal normal = new Normal(0, 1, u);
    ChiSquare chisquare = new ChiSquare(numSamples - 1, u);
    int bootstrapSize = Settings.getInt("logCI.bootstrapSize", 2000);
    double[] samples = new double[bootstrapSize];
    for (int i = bootstrapSize; i-- > 0; ) samples[i] = generateBootstrapSample(normal, chisquare, numSamples, logstd);
    Arrays.sort(samples);
    return samples;
}
Also used : ChiSquare(cern.jet.random.ChiSquare) RandomEngine(cern.jet.random.engine.RandomEngine) MersenneTwister(cern.jet.random.engine.MersenneTwister) Normal(cern.jet.random.Normal)

Example 10 with Normal

use of cern.jet.random.Normal in project Gemma by PavlidisLab.

the class ComBat method plot.

/**
 * Make diagnostic plots.
 * FIXME: As in the original ComBat, this only graphs the first batch's statistics. In principle we can (and perhaps
 * should) examine these plots for all the batches.
 *
 * @param filePrefix file prefix
 */
public void plot(String filePrefix) {
    if (this.gammaHat == null)
        throw new IllegalArgumentException("You must call 'run' first");
    /*
         * View the distribution of gammaHat, which we assume will have a normal distribution
         */
    DoubleMatrix1D ghr = gammaHat.viewRow(0);
    int NUM_HIST_BINS = 100;
    Histogram gammaHatHist = new Histogram("GammaHat", NUM_HIST_BINS, ghr);
    XYSeries ghplot = gammaHatHist.plot();
    Normal rn = new Normal(this.gammaBar.get(0), Math.sqrt(this.t2.get(0)), new MersenneTwister());
    Histogram ghtheoryT = new Histogram("Gamma", NUM_HIST_BINS, gammaHatHist.min(), gammaHatHist.max());
    for (int i = 0; i < 10000; i++) {
        double n = rn.nextDouble();
        ghtheoryT.fill(n);
    }
    XYSeries ghtheory = ghtheoryT.plot();
    File tmpfile;
    try {
        tmpfile = File.createTempFile(filePrefix + ".gammahat.histogram.", ".png");
        ComBat.log.info(tmpfile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try (OutputStream os = new FileOutputStream(tmpfile)) {
        this.writePlot(os, ghplot, ghtheory);
        /*
             * View the distribution of deltaHat, which we assume has an inverse gamma distribution
             */
        DoubleMatrix1D dhr = deltaHat.viewRow(0);
        Histogram deltaHatHist = new Histogram("DeltaHat", NUM_HIST_BINS, dhr);
        XYSeries dhplot = deltaHatHist.plot();
        Gamma g = new Gamma(aPrior.get(0), bPrior.get(0), new MersenneTwister());
        Histogram deltaHatT = new Histogram("Delta", NUM_HIST_BINS, deltaHatHist.min(), deltaHatHist.max());
        for (int i = 0; i < 10000; i++) {
            double invg = 1.0 / g.nextDouble();
            deltaHatT.fill(invg);
        }
        XYSeries dhtheory = deltaHatT.plot();
        tmpfile = File.createTempFile(filePrefix + ".deltahat.histogram.", ".png");
        ComBat.log.info(tmpfile);
        try (OutputStream os2 = new FileOutputStream(tmpfile)) {
            this.writePlot(os2, dhplot, dhtheory);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) Histogram(ubic.basecode.math.distribution.Histogram) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Normal(cern.jet.random.Normal) Gamma(cern.jet.random.Gamma) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) FileOutputStream(java.io.FileOutputStream) MersenneTwister(cern.jet.random.engine.MersenneTwister) File(java.io.File)

Aggregations

Normal (cern.jet.random.Normal)13 RandomEngine (cern.jet.random.engine.RandomEngine)8 MersenneTwister64 (cern.jet.random.engine.MersenneTwister64)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 MersenneTwister (cern.jet.random.engine.MersenneTwister)4 Timer (io.micrometer.core.instrument.Timer)4 TimeUnit (java.util.concurrent.TimeUnit)4 FunctionTimer (io.micrometer.core.instrument.FunctionTimer)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)1 ChiSquare (cern.jet.random.ChiSquare)1 Gamma (cern.jet.random.Gamma)1 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