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

org.jgroups.util.Profiler Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 33.0.2.Final
Show newest version
package org.jgroups.util;

import org.jgroups.stack.DiagnosticsHandler;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Wraps {@link AverageMinMax} and provides an impl of {@link org.jgroups.stack.DiagnosticsHandler.ProbeHandler}.
 * Note that this class in unsynchronized and needs external synchronization.
 * @author Bela Ban
 * @since  4.0
 */
public class Profiler implements DiagnosticsHandler.ProbeHandler {
    protected final AverageMinMax avg=new AverageMinMax();
    protected final String        name;
    protected final TimeUnit      unit;

    /**
     * Creates a Profiler instance which will reply to key 'name'.
     * @param name The name under which the profiler will register itself
     * @param unit The unit of measurement - only nanos, micros and millis are supported
     */
    public Profiler(String name, TimeUnit unit) {
        this.name=name;
        this.unit=unit;
    }

    public Profiler add(long time) {avg.add(time); return this;}
    public long     min()          {return avg.min();}
    public long     max()          {return avg.max();}
    public double   average()      {return avg.average();}
    public long     count()        {return avg.count();}
    public Profiler clear()        {avg.clear(); return this;}

    public String   toString()     {
        return String.format("min/avg/max %s = %d / %.2f / %d",
                             toString(unit), avg.min(), avg.average(), avg.max());
    }

    public Map handleProbe(String... keys) {
        Map map=null;
        for(String key: keys) {
            if(Objects.equals(key, name + ".reset"))
                avg.clear();
            else if(Objects.equals(key, name)) {
                if(map == null) map=new HashMap<>();
                map.put(name, String.format("cnt: min/avg/max = %d: %d / %.2f / %d %s",
                                            avg.count(), avg.min(), avg.average(), avg.max(), toString(unit)));
            }
        }
        return map;
    }

    public String[] supportedKeys() {return new String[]{name, name + ".reset"};}

    protected static String toString(TimeUnit unit) {
        switch(unit) {
            case SECONDS:      return "s";
            case MILLISECONDS: return "ms";
            case MICROSECONDS: return "us";
            case NANOSECONDS:  return "ns";
        }
        return unit.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy