Search in sources :

Example 1 with MeasurementAccumulator

use of org.spf4j.perf.MeasurementAccumulator in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorder method getThenReset.

@Override
public long[] getThenReset() {
    MeasurementAccumulator result = null;
    synchronized (threadLocalRecorders) {
        Iterator<Map.Entry<Thread, MeasurementAccumulator>> iterator = threadLocalRecorders.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Thread, MeasurementAccumulator> entry = iterator.next();
            Thread t = entry.getKey();
            if (!t.isAlive()) {
                iterator.remove();
            }
            MeasurementAccumulator measurements = entry.getValue().reset();
            if (result == null) {
                result = measurements;
            } else {
                if (measurements != null) {
                    result = result.aggregate(measurements);
                }
            }
        }
    }
    return (result == null) ? null : result.get();
}
Also used : MeasurementAccumulator(org.spf4j.perf.MeasurementAccumulator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MeasurementAccumulator

use of org.spf4j.perf.MeasurementAccumulator in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorderSource method getMeasurementsAsString.

@JmxExport(description = "measurements as csv")
public String getMeasurementsAsString() {
    StringWriter sw = new StringWriter(128);
    Map<Object, MeasurementAccumulator> entitiesMeasurements = getEntitiesMeasurements();
    MeasurementsInfo info = this.processorTemplate.getInfo();
    try {
        Csv.writeCsvRow2(sw, "Measured", (Object[]) info.getMeasurementNames());
        Csv.writeCsvRow2(sw, "string", (Object[]) info.getMeasurementUnits());
        for (Map.Entry<Object, MeasurementAccumulator> entry : entitiesMeasurements.entrySet()) {
            Csv.writeCsvElement(entry.getKey().toString(), sw);
            sw.write(',');
            final long[] measurements = entry.getValue().get();
            if (measurements != null) {
                Csv.writeCsvRow(sw, measurements);
            }
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
    return sw.toString();
}
Also used : MeasurementAccumulator(org.spf4j.perf.MeasurementAccumulator) StringWriter(java.io.StringWriter) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MeasurementsInfo(org.spf4j.perf.MeasurementsInfo) HashMap(java.util.HashMap) Map(java.util.Map) TObjectLongMap(gnu.trove.map.TObjectLongMap) TObjectLongHashMap(gnu.trove.map.hash.TObjectLongHashMap) JmxExport(org.spf4j.jmx.JmxExport)

Example 3 with MeasurementAccumulator

use of org.spf4j.perf.MeasurementAccumulator in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorderSource method getMeasurements.

public CompositeDataSupport getMeasurements() {
    Map<Object, MeasurementAccumulator> entitiesMeasurements = getEntitiesMeasurements();
    MeasurementsInfo info = this.processorTemplate.getInfo();
    int nrStuff = entitiesMeasurements.size();
    String[] names = new String[nrStuff];
    String[] descriptions = new String[nrStuff];
    OpenType<?>[] types = new OpenType[nrStuff];
    Object[] values = new Object[nrStuff];
    int i = 0;
    for (Map.Entry<Object, MeasurementAccumulator> entry : entitiesMeasurements.entrySet()) {
        MeasurementAccumulator acc = entry.getValue();
        MeasurementsInfo eInfo = acc.getInfo();
        String cattrName = eInfo.getMeasuredEntity().toString();
        names[i] = cattrName;
        String cattrDesc = eInfo.getDescription();
        if (cattrDesc.isEmpty()) {
            cattrDesc = cattrName;
        }
        descriptions[i] = cattrDesc;
        types[i] = eInfo.toCompositeType();
        values[i] = acc.getCompositeData();
        i++;
    }
    try {
        String name = info.getMeasuredEntity().toString();
        String description = info.getDescription();
        if (description.isEmpty()) {
            description = name;
        }
        CompositeType setType = new CompositeType(name, description, names, descriptions, types);
        return new CompositeDataSupport(setType, names, values);
    } catch (OpenDataException ex) {
        throw new IllegalArgumentException("Not composite data compatible " + this, ex);
    }
}
Also used : MeasurementAccumulator(org.spf4j.perf.MeasurementAccumulator) OpenType(javax.management.openmbean.OpenType) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) MeasurementsInfo(org.spf4j.perf.MeasurementsInfo) OpenDataException(javax.management.openmbean.OpenDataException) HashMap(java.util.HashMap) Map(java.util.Map) TObjectLongMap(gnu.trove.map.TObjectLongMap) TObjectLongHashMap(gnu.trove.map.hash.TObjectLongHashMap) CompositeType(javax.management.openmbean.CompositeType)

Example 4 with MeasurementAccumulator

use of org.spf4j.perf.MeasurementAccumulator in project spf4j by zolyfarkas.

the class CountingAccumulator method reset.

@Override
@Nullable
public synchronized MeasurementAccumulator reset() {
    if (counter == 0) {
        return null;
    } else {
        MeasurementAccumulator result = this.createClone();
        counter = 0;
        total = 0;
        return result;
    }
}
Also used : MeasurementAccumulator(org.spf4j.perf.MeasurementAccumulator) Nullable(javax.annotation.Nullable)

Example 5 with MeasurementAccumulator

use of org.spf4j.perf.MeasurementAccumulator in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorderSource method getEntitiesMeasurements.

@Override
public Map<Object, MeasurementAccumulator> getEntitiesMeasurements() {
    Map<Object, MeasurementAccumulator> result = new HashMap<>();
    synchronized (measurementProcessorMap) {
        Iterator<Map.Entry<Thread, Map<Object, MeasurementAccumulator>>> iterator = measurementProcessorMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Thread, Map<Object, MeasurementAccumulator>> entry = iterator.next();
            Map<Object, MeasurementAccumulator> measurements = entry.getValue();
            synchronized (measurements) {
                for (Map.Entry<Object, MeasurementAccumulator> lentry : measurements.entrySet()) {
                    Object what = lentry.getKey();
                    MeasurementAccumulator existingMeasurement = result.get(what);
                    if (existingMeasurement == null) {
                        existingMeasurement = lentry.getValue().createClone();
                    } else {
                        existingMeasurement = existingMeasurement.aggregate(lentry.getValue().createClone());
                    }
                    result.put(what, existingMeasurement);
                }
            }
        }
    }
    return result;
}
Also used : MeasurementAccumulator(org.spf4j.perf.MeasurementAccumulator) HashMap(java.util.HashMap) TObjectLongHashMap(gnu.trove.map.hash.TObjectLongHashMap) HashMap(java.util.HashMap) Map(java.util.Map) TObjectLongMap(gnu.trove.map.TObjectLongMap) TObjectLongHashMap(gnu.trove.map.hash.TObjectLongHashMap)

Aggregations

MeasurementAccumulator (org.spf4j.perf.MeasurementAccumulator)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 TObjectLongMap (gnu.trove.map.TObjectLongMap)4 TObjectLongHashMap (gnu.trove.map.hash.TObjectLongHashMap)4 MeasurementsInfo (org.spf4j.perf.MeasurementsInfo)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 CompositeType (javax.management.openmbean.CompositeType)1 OpenDataException (javax.management.openmbean.OpenDataException)1 OpenType (javax.management.openmbean.OpenType)1 JmxExport (org.spf4j.jmx.JmxExport)1