Skip to content

Commit 41dd955

Browse files
committed
fix: remove misleading initial window constructor parameter
Contrary to what the name implied, this doesn't set an initial size for measure's underlying array, it fixed that size, which led to errors if we actually wanted to record more measures.
1 parent be1bf1c commit 41dd955

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

measure/src/main/java/net/laprun/sustainability/power/measure/DescriptiveStatisticsComponentMeasure.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
public class DescriptiveStatisticsComponentMeasure implements ComponentMeasure {
66
private final DescriptiveStatistics statistics;
77

8-
public DescriptiveStatisticsComponentMeasure(int initialWindow) {
9-
statistics = new DescriptiveStatistics(initialWindow);
8+
public DescriptiveStatisticsComponentMeasure() {
9+
statistics = new DescriptiveStatistics();
1010
}
1111

1212
@Override
@@ -15,11 +15,7 @@ public void recordComponentValue(double value) {
1515
}
1616

1717
@Override
18-
public double[] getComponentRawValues() {
18+
public double[] getComponentValues() {
1919
return statistics.getValues();
2020
}
21-
22-
public static Factory<DescriptiveStatisticsComponentMeasure> factory(int initialWindow) {
23-
return () -> new DescriptiveStatisticsComponentMeasure(initialWindow);
24-
}
2521
}

measure/src/test/java/net/laprun/sustainability/power/measure/OngoingPowerMeasureTest.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package net.laprun.sustainability.power.measure;
22

3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45

56
import java.util.List;
67
import java.util.Map;
8+
import java.util.stream.Stream;
79

810
import org.junit.jupiter.params.ParameterizedTest;
911
import org.junit.jupiter.params.provider.MethodSource;
@@ -20,7 +22,7 @@ public int componentCardinality() {
2022
};
2123

2224
static List<ComponentMeasure.Factory<?>> factories() {
23-
return List.of(DescriptiveStatisticsComponentMeasure.factory(2), HdrHistogramComponentMeasure::new);
25+
return List.of(DescriptiveStatisticsComponentMeasure::new, HdrHistogramComponentMeasure::new);
2426
}
2527

2628
@ParameterizedTest
@@ -29,9 +31,15 @@ void testStatistics(ComponentMeasure.Factory<?> factory) {
2931
final var m1c1 = 10.0;
3032
final var m1c2 = 12.0;
3133
final var m1c3 = 0.0;
34+
final var m1total = m1c1 + m1c2 + m1c3;
3235
final var m2c1 = 8.0;
3336
final var m2c2 = 17.0;
3437
final var m2c3 = 0.0;
38+
final var m2total = m2c1 + m2c2 + m2c3;
39+
final var m3c1 = 5.0;
40+
final var m3c2 = 5.0;
41+
final var m3c3 = 0.0;
42+
final var m3total = m3c1 + m3c2 + m3c3;
3543

3644
final var measure = new OngoingPowerMeasure(metadata, factory);
3745

@@ -46,19 +54,28 @@ void testStatistics(ComponentMeasure.Factory<?> factory) {
4654
components[2] = m2c3;
4755
measure.recordMeasure(components);
4856

49-
assertEquals(m1c1 + m1c2 + m2c1 + m2c2 + m1c3 + m2c3, measure.total());
50-
assertEquals((m1c1 + m1c2 + m2c1 + m2c2 + m1c3 + m2c3) / 2, measure.average());
51-
assertEquals(Math.min(m1c1 + m1c2 + m1c3, m2c1 + m2c2 + m2c3), measure.minMeasuredTotal());
52-
assertEquals(Math.max(m1c1 + m1c2 + m1c3, m2c1 + m2c2 + m2c3), measure.maxMeasuredTotal());
57+
components[0] = m3c1;
58+
components[1] = m3c2;
59+
components[2] = m3c3;
60+
measure.recordMeasure(components);
61+
62+
// assertArrayEquals(new double[] {m1c1, m2c1}, measure.getMeasuresFor(0));
63+
// assertArrayEquals(new double[] {m1c2, m2c2}, measure.getMeasuresFor(1));
64+
// assertArrayEquals(new double[] {m1c3, m2c3}, measure.getMeasuresFor(2));
65+
66+
assertEquals(m1c1 + m1c2 + m1c3 + m2c1 + m2c2 + m2c3 + m3c1 + m3c2 + m3c3, measure.total());
67+
assertEquals((m1c1 + m1c2 + m1c3 + m2c1 + m2c2 + m2c3 + m3c1 + m3c2 + m3c3) / 3, measure.average());
68+
assertEquals(Stream.of(m1total, m2total, m3total).min(Double::compareTo).orElseThrow(), measure.minMeasuredTotal());
69+
assertEquals(Stream.of(m1total, m2total, m3total).max(Double::compareTo).orElseThrow(), measure.maxMeasuredTotal());
5370
final var c1Avg = measure.averagesPerComponent()[0];
5471
final var c2Avg = measure.averagesPerComponent()[1];
5572
final var c3Avg = measure.averagesPerComponent()[2];
56-
assertEquals((m1c1 + m2c1) / 2, c1Avg);
57-
assertEquals((m1c2 + m2c2) / 2, c2Avg);
73+
assertEquals((m1c1 + m2c1 + m3c1) / 3, c1Avg);
74+
assertEquals((m1c2 + m2c2 + m3c2) / 3, c2Avg);
5875
assertEquals(0, c3Avg);
5976

60-
final var stdVarForC1 = Math.sqrt((Math.pow(m1c1 - c1Avg, 2) + Math.pow(m2c1 - c1Avg, 2)) / (2 - 1));
61-
final var stdVarForC2 = Math.sqrt((Math.pow(m1c2 - c2Avg, 2) + Math.pow(m2c2 - c2Avg, 2)) / (2 - 1));
77+
final var stdVarForC1 = Math.sqrt((Math.pow(m1c1 - c1Avg, 2) + Math.pow(m2c1 - c1Avg, 2) + Math.pow(m3c1 - c1Avg, 2)) / (3 - 1));
78+
final var stdVarForC2 = Math.sqrt((Math.pow(m1c2 - c2Avg, 2) + Math.pow(m2c2 - c2Avg, 2) + Math.pow(m3c2 - c2Avg, 2)) / (3 - 1));
6279

6380
assertEquals(stdVarForC1, measure.standardDeviations().perComponent()[0], 0.0001,
6481
"Standard Deviation did not match the expected value");

0 commit comments

Comments
 (0)