All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.airlift.http.client.jetty.CachedDistribution Maven / Gradle / Ivy

The newest version!
package io.airlift.http.client.jetty;

import com.google.errorprone.annotations.ThreadSafe;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import io.airlift.stats.Distribution;
import org.weakref.jmx.Managed;

import java.util.Map;
import java.util.function.Supplier;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

/*
 * This class is needed because jmxutils only fetches a nested instance object once and holds on to it forever.
 * todo remove this when https://github.com/martint/jmxutils/issues/26 is implemented
 */
@ThreadSafe
public class CachedDistribution
{
    private final Supplier distributionSupplier;

    @GuardedBy("this")
    private Distribution distribution;
    @GuardedBy("this")
    private long lastUpdate = System.nanoTime();

    public CachedDistribution(Supplier distributionSupplier)
    {
        this.distributionSupplier = distributionSupplier;
    }

    public synchronized Distribution getDistribution()
    {
        // refresh stats only once a second
        if (NANOSECONDS.toMillis(System.nanoTime() - lastUpdate) > 1000) {
            this.distribution = distributionSupplier.get();
            this.lastUpdate = System.nanoTime();
        }
        return distribution;
    }

    @Managed
    public double getCount()
    {
        return getDistribution().getCount();
    }

    @Managed
    public double getTotal()
    {
        return getDistribution().getTotal();
    }

    @Managed
    public double getP01()
    {
        return getDistribution().getP01();
    }

    @Managed
    public double getP05()
    {
        return getDistribution().getP05();
    }

    @Managed
    public double getP10()
    {
        return getDistribution().getP10();
    }

    @Managed
    public double getP25()
    {
        return getDistribution().getP25();
    }

    @Managed
    public double getP50()
    {
        return getDistribution().getP50();
    }

    @Managed
    public double getP75()
    {
        return getDistribution().getP75();
    }

    @Managed
    public double getP90()
    {
        return getDistribution().getP90();
    }

    @Managed
    public double getP95()
    {
        return getDistribution().getP95();
    }

    @Managed
    public double getP99()
    {
        return getDistribution().getP99();
    }

    @Managed
    public double getMin()
    {
        return getDistribution().getMin();
    }

    @Managed
    public double getMax()
    {
        return getDistribution().getMax();
    }

    @Managed
    public Map getPercentiles()
    {
        return getDistribution().getPercentiles();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy