Skip to content

Commit

Permalink
feat: added HdrHistogram-backed MeasureStore
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Jul 10, 2024
1 parent be015c5 commit a1031d2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
5 changes: 5 additions & 0 deletions measure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,52 @@
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

class DescriptiveStatisticsMeasureStore implements MeasureStore {
private final int totalIndex;
private final DescriptiveStatistics[] measures;
private final DescriptiveStatistics total;

public DescriptiveStatisticsMeasureStore(int componentsNumber, int initialWindow) {
total = new DescriptiveStatistics(initialWindow);
this.measures = new DescriptiveStatistics[componentsNumber];
this.measures = new DescriptiveStatistics[componentsNumber + 1];
totalIndex = componentsNumber;
for (int i = 0; i < measures.length; i++) {
measures[i] = new DescriptiveStatistics(initialWindow);
}
}

@Override
public void recordComponentValue(int component, double value) {
measures[component].addValue(value);
getMeasure(component).addValue(value);
}

private DescriptiveStatistics getMeasure(int component) {
return measures[component];
}

private DescriptiveStatistics getTotalMeasure() {
return getMeasure(totalIndex);
}

@Override
public void recordTotal(double value) {
total.addValue(value);
getTotalMeasure().addValue(value);
}

@Override
public double getMeasuredTotal() {
return total.getSum();
return getTotalMeasure().getSum();
}

@Override
public double getComponentStandardDeviation(int component) {
return measures[component].getStandardDeviation();
return getMeasure(component).getStandardDeviation();
}

@Override
public double getTotalStandardDeviation() {
return total.getStandardDeviation();
return getTotalMeasure().getStandardDeviation();
}

@Override
public double[] getComponentRawValues(int component) {
return measures[component].getValues();
return getMeasure(component).getValues();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package net.laprun.sustainability.power.measure;

import org.HdrHistogram.HistogramIterationValue;
import org.HdrHistogram.IntCountsHistogram;

public class HdrHistogramMeasureStore implements MeasureStore {
private static final int HIGHEST_TRACKABLE_VALUE = 1_000_000;
private static final int NUMBER_OF_SIGNIFICANT_VALUE_DIGITS = 4;
private static final int CONVERSION_FACTOR = 1000;
private final IntCountsHistogram[] measures;
private final int totalIndex;
private double accumulatedTotal;

public HdrHistogramMeasureStore(int componentsNumber, int initialWindow) {
totalIndex = componentsNumber;
measures = new IntCountsHistogram[componentsNumber + 1];
for (int i = 0; i < measures.length; i++) {
measures[i] = new IntCountsHistogram(HIGHEST_TRACKABLE_VALUE,
NUMBER_OF_SIGNIFICANT_VALUE_DIGITS);
}
}

private IntCountsHistogram getMeasure(int component) {
return measures[component];
}

private IntCountsHistogram getTotalMeasure() {
return getMeasure(totalIndex);
}

@Override
public void recordComponentValue(int component, double value) {
getMeasure(component).recordValue((long) (CONVERSION_FACTOR * value));
}

@Override
public void recordTotal(double value) {
getTotalMeasure().recordValue((long) (CONVERSION_FACTOR * value));
accumulatedTotal += value;
}

@Override
public double getMeasuredTotal() {
return accumulatedTotal;
}

@Override
public double getComponentStandardDeviation(int component) {
return getMeasure(component).getStdDeviation() / CONVERSION_FACTOR;
}

@Override
public double getTotalStandardDeviation() {
// not unbiased so tests will fail
return getTotalMeasure().getStdDeviation();
}

@Override
public double[] getComponentRawValues(int component) {
final var measure = getMeasure(component);
final var totalCount = measure.getTotalCount();
final var result = new double[(int) totalCount];
int index = 0;
for (HistogramIterationValue value : measure.recordedValues()) {
result[index++] = (double) value.getValueIteratedTo() / CONVERSION_FACTOR;
}
return result;
}
}

0 comments on commit a1031d2

Please sign in to comment.