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();
}
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();
}
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);
}
}
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;
}
}
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;
}
Aggregations