Skip to content

Commit

Permalink
Fix unwanted dependency from jPowerMonitor lib to demo benchmark pr… (#…
Browse files Browse the repository at this point in the history
…96)

* Fix not wanted dependency from jPowerMonitor lib to demo benchmark project/lib

* Implemented fix from S. Schnell if total energy consumption is zero

* add more comments and use fewer num runs in order to run faster in git pipeline

---------

Co-authored-by: keilhofh <[email protected]>
  • Loading branch information
deinerj and keilhofh authored Oct 18, 2024
1 parent 67669bb commit 82a4bf3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import group.msg.jpowermonitor.agent.JPowerMonitorAgent;
import group.msg.jpowermonitor.agent.PowerMeasurementCollector;
import group.msg.jpowermonitor.agent.export.csv.CsvResultsWriter;
import group.msg.jpowermonitor.demo.StressCpuExample;
import lombok.extern.slf4j.Slf4j;

import java.text.NumberFormat;
import java.util.Locale;

import static group.msg.jpowermonitor.util.Constants.SEPARATOR;
Expand All @@ -16,30 +14,34 @@
@Slf4j
public class StatisticsWriter {
private final PowerMeasurementCollector powerMeasurementCollector;
private static long benchmarkResult;

public StatisticsWriter(PowerMeasurementCollector powerMeasurementCollector) {
this.powerMeasurementCollector = powerMeasurementCollector;
}

public void writeStatistics(CsvResultsWriter csvResultsWriter) {
if (powerMeasurementCollector == null
|| powerMeasurementCollector.getEnergyConsumptionTotalInJoule() == null
|| powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get() == null
|| powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue() == null) {
|| powerMeasurementCollector.getEnergyConsumptionTotalInJoule() == null
|| powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get() == null
|| powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue() == null) {
return;
}
String appStatistics = String.format("Application consumed %.2f joule - %.3f wh - %.6f kwh - %.3f gCO2 total",
powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue(),
convertJouleToWattHours(powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue()),
convertJouleToKiloWattHours(powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue()),
powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getCo2Value());
String benchmarkResult =
"Benchmark result efficiency factor (sum of all loop counters / energyConsumptionTotal): *** "
+ NumberFormat.getNumberInstance(Locale.GERMANY)
.format(StressCpuExample.getBenchmarkResult() / powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue().longValue())
+ " *** jPMarks";
String filesInfo = "Energy consumption per method written to '" + csvResultsWriter.getEnergyConsumptionPerMethodFileName()
+ "' and filtered methods written to '" + csvResultsWriter.getEnergyConsumptionPerFilteredMethodFileName() + "'" + "\n" + SEPARATOR;
powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue(),
convertJouleToWattHours(powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue()),
convertJouleToKiloWattHours(powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getValue()),
powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get().getCo2Value());
long totalEnergyConsumptionInJoule = powerMeasurementCollector.getEnergyConsumptionTotalInJoule().get()
.getValue().longValue();
String benchmarkResult = hasBenchmarkResult() && totalEnergyConsumptionInJoule > 0.0 ? String.format(
Locale.GERMANY,
"Benchmark result efficiency factor (sum of all loop counters / energyConsumptionTotal): *** %,d *** jPMarks",
getBenchmarkResult() / totalEnergyConsumptionInJoule) : "";
String filesInfo = "Energy consumption per method written to '"
+ csvResultsWriter.getEnergyConsumptionPerMethodFileName()
+ "' and filtered methods written to '"
+ csvResultsWriter.getEnergyConsumptionPerFilteredMethodFileName() + "'" + "\n" + SEPARATOR;

if (JPowerMonitorAgent.isSlf4jLoggerImplPresent()) {
log.info(appStatistics);
Expand All @@ -51,4 +53,16 @@ public void writeStatistics(CsvResultsWriter csvResultsWriter) {
System.out.println(filesInfo);
}
}

private static long getBenchmarkResult() {
return benchmarkResult;
}

public static void setBenchmarkResult(long benchmarkResult) {
StatisticsWriter.benchmarkResult = benchmarkResult;
}

private boolean hasBenchmarkResult() {
return benchmarkResult > 0;
}
}
11 changes: 3 additions & 8 deletions src/main/java/group/msg/jpowermonitor/demo/StressCpuExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package group.msg.jpowermonitor.demo;

import group.msg.jpowermonitor.agent.export.statistics.StatisticsWriter;
import group.msg.jpowermonitor.util.CmdLineArgs;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -28,6 +29,7 @@ public static void main(String[] args) {
runSequentialEnergyMeasurementAndBenchmarkUsingOneCpuThread();
runPercentagedEnergyMeasurementAndBenchmarkUsingOneCpuThread();
runParallelEnergyMeasurementAndBenchmarkUsingMultipleCpuThreads();
StatisticsWriter.setBenchmarkResult(getBenchmarkResult());
System.exit(0); // Important to exit properly since JavaAgent will not exit gracefully without
}

Expand Down Expand Up @@ -144,17 +146,10 @@ public static long iAm100Percent(long runUntil) {
return loopCounter;
}

/**
* @return true if this is a benchmark run
*/
public static boolean isBenchmarkRun() {
return sequentialLoopCounter > 0 || percentagedLoopCounter > 0 || parallelLoopCounter > 0;
}

/**
* @return sum of all loop counters
*/
public static long getBenchmarkResult() {
private static long getBenchmarkResult() {
return sequentialLoopCounter + percentagedLoopCounter + parallelLoopCounter;
}

Expand Down
21 changes: 19 additions & 2 deletions src/test/java/com/msg/myapplication/ReplaceInStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
@ExtendWith({JPowerMonitorExtension.class})
@Slf4j
public class ReplaceInStringTest {
private static final int NUM_RUNS = 300_000;
// private static final int NUM_RUNS = 1;
private static final int NUM_RUNS = 100_000;
//private static final int NUM_RUNS = 10; // ==> use this in case the big xml is tested...

// A carriage return means moving the cursor to the beginning of the line. The code is \r.
// A line feed means moving one line forward. The code is \n.
private static final Pattern PATTERN = Pattern.compile("\\R"); // same as "(\r)*\n"
Expand Down Expand Up @@ -99,6 +100,12 @@ void testReplaceUsingIndexOfAndStringReplace(String file) throws IOException {
log.info("testReplaceUsingIndexOfAndStringReplace: {} ms", System.currentTimeMillis() - start);
}

// -------------------------------------------------------------------------------------------------
//
// Implementation of "business logic" follows:
//
// -------------------------------------------------------------------------------------------------

/**
* Replaces "linefeed" or "carriage return+linefeed" with the string "\n" (the characters 'backslash' and 'n').
*
Expand All @@ -107,8 +114,18 @@ void testReplaceUsingIndexOfAndStringReplace(String file) throws IOException {
*/
private static String replaceUsingRegex(String input) {
return PATTERN.matcher(input).replaceAll("\\\\n");

// In Java, backslashes in strings and regex must be escaped as \\.
// In regex, a backslash is represented as \\.
// If replaceAll() is used to replace \n, this is written as \\\\n in Java, to escape the backslash and interpret the regular expression.
}

/**
* Replaces "linefeed" or "carriage return+linefeed" with the string "\n" (the characters 'backslash' and 'n').
*
* @param input the input that contains carriage return/linefeed.
* @return String with "\n" instead of carriage return/linefeed.
*/
public static String replaceUsingForwardSearchAndChars(String input) {
char[] chars = input.toCharArray();
StringBuilder result = new StringBuilder();
Expand Down

0 comments on commit 82a4bf3

Please sign in to comment.