Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions analysis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.0.12-SNAPSHOT</version>
</parent>

<artifactId>power-server-analysis</artifactId>
<name>power-server : analysis</name>
<description>Classes that support analysis of recorded data</description>

<dependencies>
<dependency>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-measure</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<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>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>net.laprun.sustainability</groupId>
<artifactId>build-tools</artifactId>
<version>0.0.12-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.laprun.sustainability.power.analysis;

import org.apache.commons.math3.util.FastMath;

import net.laprun.sustainability.power.measure.PowerMeasure;

public enum Compute {
;

public static double standardDeviation(PowerMeasure measure, int componentIndex) {
return measure.getMeasuresFor(componentIndex).map(values -> {
final var samples = values.length;
if (samples <= 1) {
return 0.0;
}
final double mean = average(measure, componentIndex);
double geometricDeviationTotal = 0.0;
for (double value : values) {
double deviation = value - mean;
geometricDeviationTotal += (deviation * deviation);
}
return FastMath.sqrt(geometricDeviationTotal / (samples - 1));
}).orElse(0.0);
}

public static double average(PowerMeasure measure, int componentIndex) {
return measure.getMeasuresFor(componentIndex).map(values -> {
double sum = 0.0;
for (double value : values) {
sum += value;
}
return sum / values.length;
}).orElse(0.0);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.laprun.sustainability.power.measure;
package net.laprun.sustainability.power.analysis;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

@SuppressWarnings("unused")
public class DescriptiveStatisticsAnalyzer implements Analyzer {
public class DescriptiveStatisticsComponentProcessor implements ComponentProcessor {
private final DescriptiveStatistics statistics;

public DescriptiveStatisticsAnalyzer() {
public DescriptiveStatisticsComponentProcessor() {
statistics = new DescriptiveStatistics();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package net.laprun.sustainability.power.measure;
package net.laprun.sustainability.power.analysis;

import org.HdrHistogram.IntCountsHistogram;

@SuppressWarnings("unused")
public class HdrHistogramAnalyzer implements Analyzer {
public class HdrHistogramComponentProcessor implements ComponentProcessor {
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 histogram;

public HdrHistogramAnalyzer() {
public HdrHistogramComponentProcessor() {
histogram = new IntCountsHistogram(HIGHEST_TRACKABLE_VALUE, NUMBER_OF_SIGNIFICANT_VALUE_DIGITS);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.laprun.sustainability.power.analysis;

public class MeanComponentProcessor implements ComponentProcessor {
private double mean;
private int count;

@Override
public void recordComponentValue(double value, long timestamp) {
final var previousSize = count;
count++;
mean = mean == 0 ? value : (previousSize * mean + value) / count;
}
}
110 changes: 110 additions & 0 deletions analysis/src/test/java/ComputeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Map;
import java.util.Random;
import java.util.random.RandomGenerator;

import org.junit.jupiter.api.Test;

import net.laprun.sustainability.power.SensorMetadata;
import net.laprun.sustainability.power.analysis.Compute;
import net.laprun.sustainability.power.measure.OngoingPowerMeasure;

public class ComputeTest {
private final static SensorMetadata metadata = new SensorMetadata(Map.of(), null, new int[0]) {

@Override
public int componentCardinality() {
return 3;
}
};

@Test
void standardDeviationShouldWork() {
final var random = Random.from(RandomGenerator.getDefault());
final var m1c1 = random.nextDouble();
final var m1c2 = random.nextDouble();
final var m1c3 = 0.0;
final var m2c1 = random.nextDouble();
final var m2c2 = random.nextDouble();
final var m2c3 = 0.0;
final var m3c1 = random.nextDouble();
final var m3c2 = random.nextDouble();
final var m3c3 = 0.0;

final var measure = new OngoingPowerMeasure(metadata);

final var components = new double[metadata.componentCardinality()];
components[0] = m1c1;
components[1] = m1c2;
components[2] = m1c3;
measure.recordMeasure(components);

components[0] = m2c1;
components[1] = m2c2;
components[2] = m2c3;
measure.recordMeasure(components);

components[0] = m3c1;
components[1] = m3c2;
components[2] = m3c3;
measure.recordMeasure(components);

final var c1Avg = Compute.average(measure, 0);
final var c2Avg = Compute.average(measure, 1);
final var stdVarForC1 = Math
.sqrt((Math.pow(m1c1 - c1Avg, 2) + Math.pow(m2c1 - c1Avg, 2) + Math.pow(m3c1 - c1Avg, 2)) / (3 - 1));
final var stdVarForC2 = Math
.sqrt((Math.pow(m1c2 - c2Avg, 2) + Math.pow(m2c2 - c2Avg, 2) + Math.pow(m3c2 - c2Avg, 2)) / (3 - 1));

assertEquals(stdVarForC1, Compute.standardDeviation(measure, 0), 0.0001,
"Standard Deviation did not match the expected value");
assertEquals(stdVarForC2, Compute.standardDeviation(measure, 1), 0.0001,
"Standard Deviation did not match the expected value");
assertEquals(0, Compute.standardDeviation(measure, 2), 0.0001,
"Standard Deviation did not match the expected value");
}

@Test
void averageShouldWork() {
final var random = Random.from(RandomGenerator.getDefault());
final var m1c1 = random.nextDouble();
final var m1c2 = random.nextDouble();
final var m1c3 = 0.0;
final var m2c1 = random.nextDouble();
final var m2c2 = random.nextDouble();
final var m2c3 = 0.0;
final var m3c1 = random.nextDouble();
final var m3c2 = random.nextDouble();
final var m3c3 = 0.0;

final var measure = new OngoingPowerMeasure(metadata);

final var components = new double[metadata.componentCardinality()];
components[0] = m1c1;
components[1] = m1c2;
components[2] = m1c3;
measure.recordMeasure(components);

components[0] = m2c1;
components[1] = m2c2;
components[2] = m2c3;
measure.recordMeasure(components);

components[0] = m3c1;
components[1] = m3c2;
components[2] = m3c3;
measure.recordMeasure(components);

final var c1Avg = Compute.average(measure, 0);
final var c2Avg = Compute.average(measure, 1);
final var c3Avg = Compute.average(measure, 2);

assertEquals((m1c1 + m2c1 + m3c1) / 3, c1Avg, 0.0001,
"Average did not match the expected value");
assertEquals((m1c2 + m2c2 + m3c2) / 3, c2Avg, 0.0001,
"Average did not match the expected value");
assertEquals(0, c3Avg, 0.0001,
"Average did not match the expected value");
}
}
12 changes: 4 additions & 8 deletions measure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
<version>2.2.2</version>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.laprun.sustainability.power.analysis;

public interface Analyzers {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.laprun.sustainability.power.analysis;

public interface ComponentProcessor {
default void recordComponentValue(double value, long timestamp) {
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.laprun.sustainability.power.measure;

public enum Compute {
;

public static double sumOfComponents(double[] recorded) {
var componentSum = 0.0;
for (double value : recorded) {
componentSum += value;
}
return componentSum;
}

public static double sumOfSelectedComponents(double[] recorded, int... indices) {
if (indices == null || indices.length == 0) {
return sumOfComponents(recorded);
}
var componentSum = 0.0;
for (int index : indices) {
componentSum += recorded[index];
}
return componentSum;
}
}
Loading