From f72f18f573393ba434c9ca03b9945e61b62e3770 Mon Sep 17 00:00:00 2001 From: Andrius Kausinis Date: Fri, 21 May 2021 14:43:00 +0300 Subject: [PATCH] Removed duplicated code. --- .../gocypher/cybench/core/utils/IOUtils.java | 56 +-- .../cybench/launcher/BenchmarkRunner.java | 31 +- .../launcher/report/ReportingService.java | 406 +++++++++--------- .../cybench/launcher/utils/JSONUtils.java | 68 --- .../src/test/java/CollectPropertiesRun.java | 145 +++---- 5 files changed, 295 insertions(+), 411 deletions(-) delete mode 100644 gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/utils/JSONUtils.java diff --git a/gocypher-cybench-client/gocypher-cybench-core/src/main/java/com/gocypher/cybench/core/utils/IOUtils.java b/gocypher-cybench-client/gocypher-cybench-core/src/main/java/com/gocypher/cybench/core/utils/IOUtils.java index 0f990d84..cda3afd2 100644 --- a/gocypher-cybench-client/gocypher-cybench-core/src/main/java/com/gocypher/cybench/core/utils/IOUtils.java +++ b/gocypher-cybench-client/gocypher-cybench-core/src/main/java/com/gocypher/cybench/core/utils/IOUtils.java @@ -54,15 +54,13 @@ public static File createFile(String name) { public static File generateBinaryFileForTests() throws Exception { createRandomBinaryFileIfNotExists(FILE_NAME_AS_SRC, fileSizeMultiplierPerChunkSize, randomFileChunkSize * fileSizeMultiplierPerChunkSize); - File f = new File(FILE_NAME_AS_SRC); - return f; + return new File(FILE_NAME_AS_SRC); } public static File generateSmallBinaryFileForTests() throws Exception { createRandomBinaryFileIfNotExists(FILE_NAME_AS_SRC_FOR_SMALL_CASES, fileSizeSmallMultiplierPerChunkSize, randomFileChunkSize * fileSizeSmallMultiplierPerChunkSize); - File f = new File(FILE_NAME_AS_SRC_FOR_SMALL_CASES); - return f; + return new File(FILE_NAME_AS_SRC_FOR_SMALL_CASES); } /* @@ -95,53 +93,29 @@ public static void removeFile(File file) { public static long copyFileUsingFileStreams(File srcFile, File targetFile, int bufferSize, boolean isSyncWrite) throws IOException { - long bytesCopied = 0L; - byte[] buffer = new byte[bufferSize]; - - try (InputStream in = new FileInputStream(srcFile)) { - try (OutputStream out = new FileOutputStream(targetFile)) { - int bytesRead; - while ((bytesRead = in.read(buffer)) != -1) { - out.write(buffer, 0, bytesRead); - if (isSyncWrite) { - out.flush(); - } - bytesCopied += bytesRead; - } - } - } - - return bytesCopied; + return copyFileUsingStreams(new FileInputStream(srcFile), new FileOutputStream(targetFile), bufferSize, + isSyncWrite); } public static long copyFileUsingBufferedStreams(File srcFile, File targetFile, int bufferSize, boolean isSyncWrite) throws IOException { - long bytesCopied = 0L; - byte[] buffer = new byte[bufferSize]; - - try (InputStream in = new BufferedInputStream(new FileInputStream(srcFile))) { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile))) { - int bytesRead; - while ((bytesRead = in.read(buffer)) != -1) { - out.write(buffer, 0, bytesRead); - if (isSyncWrite) { - out.flush(); - } - bytesCopied += bytesRead; - } - } - } - - return bytesCopied; + return copyFileUsingStreams(new BufferedInputStream(new FileInputStream(srcFile)), + new BufferedOutputStream(new FileOutputStream(targetFile)), bufferSize, isSyncWrite); } public static long copyFileUsingDirectBufferedStreams(File srcFile, File targetFile, int bufferSize, boolean isSyncWrite) throws IOException { + return copyFileUsingStreams(new BufferedInputStream(new FileInputStream(srcFile), bufferSize * 2), + new BufferedOutputStream(new FileOutputStream(targetFile), bufferSize * 2), bufferSize, isSyncWrite); + } + + private static long copyFileUsingStreams(InputStream inStr, OutputStream outStr, int bufferSize, + boolean isSyncWrite) throws IOException { long bytesCopied = 0L; byte[] buffer = new byte[bufferSize]; - try (InputStream in = new BufferedInputStream(new FileInputStream(srcFile), bufferSize * 2)) { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile), bufferSize * 2)) { + try (InputStream in = inStr) { + try (OutputStream out = outStr) { int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); @@ -182,7 +156,7 @@ public static void storeResultsToFile(String fileName, String content) { boolean exists = pFile.exists(); if (!exists) { if (!pFile.mkdir()) { - throw new IOException("Coluld not create folder=" + pFile); + throw new IOException("Could not create folder=" + pFile); } } try (Writer file = new FileWriter(fileName)) { diff --git a/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/BenchmarkRunner.java b/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/BenchmarkRunner.java index fad76e18..62fed736 100644 --- a/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/BenchmarkRunner.java +++ b/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/BenchmarkRunner.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.*; import java.util.stream.Collectors; @@ -44,6 +45,7 @@ import com.gocypher.cybench.core.annotation.CyBenchMetadataList; import com.gocypher.cybench.core.utils.IOUtils; import com.gocypher.cybench.core.utils.JMHUtils; +import com.gocypher.cybench.core.utils.JSONUtils; import com.gocypher.cybench.core.utils.SecurityUtils; import com.gocypher.cybench.launcher.environment.model.HardwareProperties; import com.gocypher.cybench.launcher.environment.model.JVMProperties; @@ -55,7 +57,6 @@ import com.gocypher.cybench.launcher.services.ConfigurationHandler; import com.gocypher.cybench.launcher.utils.ComputationUtils; import com.gocypher.cybench.launcher.utils.Constants; -import com.gocypher.cybench.launcher.utils.JSONUtils; import com.gocypher.cybench.launcher.utils.SecurityBuilder; public class BenchmarkRunner { @@ -231,7 +232,7 @@ public static void main(String[] args) throws Exception { Class aClass = Class.forName(clazz); Optional benchmarkMethod = JMHUtils.getBenchmarkMethod(method, aClass); appendMetadataFromClass(aClass, benchmarkReport); - appendMetadataFromMethod(benchmarkMethod, benchmarkReport); + appendMetadataFromAnnotated(benchmarkMethod, benchmarkReport); appendMetadataFromJavaDoc(aClass, benchmarkMethod, benchmarkReport); } catch (ClassNotFoundException e) { e.printStackTrace(); @@ -366,29 +367,14 @@ protected static void appendMetadataFromClass(Class aClass, BenchmarkReport b } for (Class anInterface : aClass.getInterfaces()) { appendMetadataFromClass(anInterface, benchmarkReport); - - } - - CyBenchMetadataList annotation = aClass.getDeclaredAnnotation(CyBenchMetadataList.class); - - if (annotation != null) { - Arrays.stream(annotation.value()).forEach(annot -> { - checkSetOldMetadataProps(annot.key(), annot.value(), benchmarkReport); - benchmarkReport.addMetadata(annot.key(), annot.value()); - // LOG.info("added metadata " + annot.key() + "=" + annot.value()); - }); - } - BenchmarkMetaData singleAnnotation = aClass.getDeclaredAnnotation(BenchmarkMetaData.class); - if (singleAnnotation != null) { - checkSetOldMetadataProps(singleAnnotation.key(), singleAnnotation.value(), benchmarkReport); - benchmarkReport.addMetadata(singleAnnotation.key(), singleAnnotation.value()); - // LOG.info("added metadata " + singleAnnotation.key() + "=" + singleAnnotation.value()); } + appendMetadataFromAnnotated(Optional.of(aClass), benchmarkReport); } - protected static void appendMetadataFromMethod(Optional benchmarkMethod, BenchmarkReport benchmarkReport) { - CyBenchMetadataList annotation = benchmarkMethod.get().getDeclaredAnnotation(CyBenchMetadataList.class); + protected static void appendMetadataFromAnnotated(Optional annotated, + BenchmarkReport benchmarkReport) { + CyBenchMetadataList annotation = annotated.get().getDeclaredAnnotation(CyBenchMetadataList.class); if (annotation != null) { Arrays.stream(annotation.value()).forEach(annot -> { checkSetOldMetadataProps(annot.key(), annot.value(), benchmarkReport); @@ -396,12 +382,11 @@ protected static void appendMetadataFromMethod(Optional benchmarkMethod, // LOG.info("added metadata " + annot.key() + "=" + annot.value()); }); } - BenchmarkMetaData singleAnnotation = benchmarkMethod.get().getDeclaredAnnotation(BenchmarkMetaData.class); + BenchmarkMetaData singleAnnotation = annotated.get().getDeclaredAnnotation(BenchmarkMetaData.class); if (singleAnnotation != null) { checkSetOldMetadataProps(singleAnnotation.key(), singleAnnotation.value(), benchmarkReport); benchmarkReport.addMetadata(singleAnnotation.key(), singleAnnotation.value()); // LOG.info("added metadata " + singleAnnotation.key() + "=" + singleAnnotation.value()); - } } diff --git a/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/report/ReportingService.java b/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/report/ReportingService.java index 106f2cc4..b7635fb6 100644 --- a/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/report/ReportingService.java +++ b/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/report/ReportingService.java @@ -25,253 +25,253 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; -import java.util.concurrent.TimeUnit; import java.util.jar.Manifest; import org.openjdk.jmh.results.RunResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.gocypher.cybench.core.utils.JSONUtils; import com.gocypher.cybench.core.utils.SecurityUtils; import com.gocypher.cybench.launcher.model.BenchmarkOverviewReport; import com.gocypher.cybench.launcher.model.BenchmarkReport; import com.gocypher.cybench.launcher.model.SecuredReport; import com.gocypher.cybench.launcher.utils.ComputationUtils; import com.gocypher.cybench.launcher.utils.Constants; -import com.gocypher.cybench.launcher.utils.JSONUtils; import com.gocypher.cybench.launcher.utils.SecurityBuilder; import com.jcabi.manifests.Manifests; public class ReportingService { - private static final Logger LOG = LoggerFactory.getLogger(ReportingService.class); - private static ReportingService instance; + private static final Logger LOG = LoggerFactory.getLogger(ReportingService.class); + private static ReportingService instance; - private ReportingService() { + private ReportingService() { - } + } - public static ReportingService getInstance() { - if (instance == null) { - instance = new ReportingService(); - } - return instance; - } + public static ReportingService getInstance() { + if (instance == null) { + instance = new ReportingService(); + } + return instance; + } - public BenchmarkOverviewReport createBenchmarkReport(Collection jmhResults, - Map> defaultBenchmarksMetadata) { - BenchmarkOverviewReport overviewReport = new BenchmarkOverviewReport(); - for (RunResult item : jmhResults) { - BenchmarkReport report = new BenchmarkReport(); - if (item.getPrimaryResult() != null) { - report.setScore(item.getPrimaryResult().getScore()); - report.setUnits(item.getPrimaryResult().getScoreUnit()); - if (item.getPrimaryResult().getStatistics() != null) { - report.setN(item.getPrimaryResult().getStatistics().getN()); - report.setMeanScore(item.getPrimaryResult().getStatistics().getMean()); - report.setMaxScore(item.getPrimaryResult().getStatistics().getMax()); - report.setMinScore(item.getPrimaryResult().getStatistics().getMin()); - if (!Double.isNaN(item.getPrimaryResult().getStatistics().getStandardDeviation())) { - report.setStdDevScore(item.getPrimaryResult().getStatistics().getStandardDeviation()); - } - } + public BenchmarkOverviewReport createBenchmarkReport(Collection jmhResults, + Map> defaultBenchmarksMetadata) { + BenchmarkOverviewReport overviewReport = new BenchmarkOverviewReport(); + for (RunResult item : jmhResults) { + BenchmarkReport report = new BenchmarkReport(); + if (item.getPrimaryResult() != null) { + report.setScore(item.getPrimaryResult().getScore()); + report.setUnits(item.getPrimaryResult().getScoreUnit()); + if (item.getPrimaryResult().getStatistics() != null) { + report.setN(item.getPrimaryResult().getStatistics().getN()); + report.setMeanScore(item.getPrimaryResult().getStatistics().getMean()); + report.setMaxScore(item.getPrimaryResult().getStatistics().getMax()); + report.setMinScore(item.getPrimaryResult().getStatistics().getMin()); + if (!Double.isNaN(item.getPrimaryResult().getStatistics().getStandardDeviation())) { + report.setStdDevScore(item.getPrimaryResult().getStatistics().getStandardDeviation()); + } + } - } - if (item.getParams() != null) { - report.setName(item.getParams().getBenchmark()); - report.setMode(item.getParams().getMode().shortLabel()); - // System.out.println("id:"+item.getParams().id()); - // System.out.println("Mode"+item.getParams().getMode().longLabel()); - } + } + if (item.getParams() != null) { + report.setName(item.getParams().getBenchmark()); + report.setMode(item.getParams().getMode().shortLabel()); + // System.out.println("id:"+item.getParams().id()); + // System.out.println("Mode"+item.getParams().getMode().longLabel()); + } Collection paramsKeys = item.getParams().getParamsKeys(); - for (String key : paramsKeys) { + for (String key : paramsKeys) { String value = item.getParams().getParam(key); LOG.info("Collected params. Key: {}, Value: {}", key, value); report.addMetadata("param" + BenchmarkReport.camelCase(key), value); } - report.setBenchForkCount(Objects.requireNonNull(item.getParams()).getForks()); - report.setBenchThreadCount(item.getParams().getThreads()); - report.setBenchWarmUpIteration(item.getParams().getWarmup().getCount()); - report.setBenchWarmUpSeconds((int) item.getParams().getWarmup().getTime().getTime()); - report.setBenchMeasurementIteration(item.getParams().getMeasurement().getCount()); - report.setBenchMeasurementSeconds((int) item.getParams().getMeasurement().getTime().getTime()); + report.setBenchThreadCount(item.getParams().getThreads()); + report.setBenchWarmUpIteration(item.getParams().getWarmup().getCount()); + report.setBenchWarmUpSeconds((int) item.getParams().getWarmup().getTime().getTime()); + report.setBenchMeasurementIteration(item.getParams().getMeasurement().getCount()); + report.setBenchMeasurementSeconds((int) item.getParams().getMeasurement().getTime().getTime()); - report.setGcCalls(getScoreFromJMHSecondaryResult(item, "·gc.count")); - report.setGcTime(getScoreFromJMHSecondaryResult(item, "·gc.time")); - report.setGcAllocationRate(getScoreFromJMHSecondaryResult(item, "·gc.alloc.rate")); - report.setGcAllocationRateNorm(getScoreFromJMHSecondaryResult(item, "·gc.alloc.rate.norm")); - report.setGcChurnPsEdenSpace(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Eden_Space")); - report.setGcChurnPsEdenSpaceNorm(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Eden_Space.norm")); - report.setGcChurnPsSurvivorSpace(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Survivor_Space")); - report.setGcChurnPsSurvivorSpaceNorm(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Survivor_Space.norm")); + report.setGcCalls(getScoreFromJMHSecondaryResult(item, "·gc.count")); + report.setGcTime(getScoreFromJMHSecondaryResult(item, "·gc.time")); + report.setGcAllocationRate(getScoreFromJMHSecondaryResult(item, "·gc.alloc.rate")); + report.setGcAllocationRateNorm(getScoreFromJMHSecondaryResult(item, "·gc.alloc.rate.norm")); + report.setGcChurnPsEdenSpace(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Eden_Space")); + report.setGcChurnPsEdenSpaceNorm(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Eden_Space.norm")); + report.setGcChurnPsSurvivorSpace(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Survivor_Space")); + report.setGcChurnPsSurvivorSpaceNorm(getScoreFromJMHSecondaryResult(item, "·gc.churn.PS_Survivor_Space.norm")); - report.setThreadsAliveCount(getScoreFromJMHSecondaryResult(item, "·threads.alive")); - report.setThreadsDaemonCount(getScoreFromJMHSecondaryResult(item, "·threads.daemon")); - report.setThreadsStartedCount(getScoreFromJMHSecondaryResult(item, "·threads.started")); + report.setThreadsAliveCount(getScoreFromJMHSecondaryResult(item, "·threads.alive")); + report.setThreadsDaemonCount(getScoreFromJMHSecondaryResult(item, "·threads.daemon")); + report.setThreadsStartedCount(getScoreFromJMHSecondaryResult(item, "·threads.started")); - report.setThreadsSafePointSyncTime(getScoreFromJMHSecondaryResult(item, "·rt.safepointSyncTime")); - report.setThreadsSafePointTime(getScoreFromJMHSecondaryResult(item, "·rt.safepointTime")); - report.setThreadsSafePointsCount(getScoreFromJMHSecondaryResult(item, "·rt.safepoints")); + report.setThreadsSafePointSyncTime(getScoreFromJMHSecondaryResult(item, "·rt.safepointSyncTime")); + report.setThreadsSafePointTime(getScoreFromJMHSecondaryResult(item, "·rt.safepointTime")); + report.setThreadsSafePointsCount(getScoreFromJMHSecondaryResult(item, "·rt.safepoints")); - report.setThreadsSyncContendedLockAttemptsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.contendedLockAttempts")); - report.setThreadsSyncMonitorFatMonitorsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.fatMonitors")); - report.setThreadsSyncMonitorFutileWakeupsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.futileWakeups")); - report.setThreadsSyncMonitorDeflations(getScoreFromJMHSecondaryResult(item, "·rt.sync.monitorDeflations")); - report.setThreadsSyncMonitorInflations(getScoreFromJMHSecondaryResult(item, "·rt.sync.monitorInflations")); - report.setThreadsSyncNotificationsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.notifications")); + report.setThreadsSyncContendedLockAttemptsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.contendedLockAttempts")); + report.setThreadsSyncMonitorFatMonitorsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.fatMonitors")); + report.setThreadsSyncMonitorFutileWakeupsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.futileWakeups")); + report.setThreadsSyncMonitorDeflations(getScoreFromJMHSecondaryResult(item, "·rt.sync.monitorDeflations")); + report.setThreadsSyncMonitorInflations(getScoreFromJMHSecondaryResult(item, "·rt.sync.monitorInflations")); + report.setThreadsSyncNotificationsCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.notifications")); - report.setThreadsSyncParksCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.parks")); + report.setThreadsSyncParksCount(getScoreFromJMHSecondaryResult(item, "·rt.sync.parks")); - report.setThreadsSafePointsInterval(getScoreFromJMHSecondaryResult(item, "·safepoints.interval")); - report.setThreadsSafePointsPause(getScoreFromJMHSecondaryResult(item, "·safepoints.pause")); - report.setThreadsSafePointsPauseAvg(getScoreFromJMHSecondaryResult(item, "·safepoints.pause.avg")); - report.setThreadsSafePointsPauseCount(getScoreFromJMHSecondaryResult(item, "·safepoints.pause.count")); - report.setThreadsSafePointsPauseTTSP(getScoreFromJMHSecondaryResult(item, "·safepoints.ttsp")); - report.setThreadsSafePointsPauseTTSPAvg(getScoreFromJMHSecondaryResult(item, "·safepoints.ttsp.avg")); - report.setThreadsSafePointsPauseTTSPCount(getScoreFromJMHSecondaryResult(item, "·safepoints.ttsp.count")); + report.setThreadsSafePointsInterval(getScoreFromJMHSecondaryResult(item, "·safepoints.interval")); + report.setThreadsSafePointsPause(getScoreFromJMHSecondaryResult(item, "·safepoints.pause")); + report.setThreadsSafePointsPauseAvg(getScoreFromJMHSecondaryResult(item, "·safepoints.pause.avg")); + report.setThreadsSafePointsPauseCount(getScoreFromJMHSecondaryResult(item, "·safepoints.pause.count")); + report.setThreadsSafePointsPauseTTSP(getScoreFromJMHSecondaryResult(item, "·safepoints.ttsp")); + report.setThreadsSafePointsPauseTTSPAvg(getScoreFromJMHSecondaryResult(item, "·safepoints.ttsp.avg")); + report.setThreadsSafePointsPauseTTSPCount(getScoreFromJMHSecondaryResult(item, "·safepoints.ttsp.count")); - report.setPerformanceProcessCpuLoad(getScoreFromJMHSecondaryResult(item,"performanceProcessCpuLoad",item.getParams().getMeasurement().getCount())); - report.setPerformanceSystemCpuLoad(getScoreFromJMHSecondaryResult(item,"performanceSystemCpuLoad",item.getParams().getMeasurement().getCount())); - report.setPerformanceProcessHeapMemoryUsed(getScoreFromJMHSecondaryResult(item,"performanceProcessHeapMemoryUsed",item.getParams().getMeasurement().getCount())); - report.setPerformanceProcessNonHeapMemoryUsed(getScoreFromJMHSecondaryResult(item,"performanceProcessNonHeapMemoryUsed",item.getParams().getMeasurement().getCount())); + report.setPerformanceProcessCpuLoad(getScoreFromJMHSecondaryResult(item, "performanceProcessCpuLoad", item.getParams().getMeasurement().getCount())); + report.setPerformanceSystemCpuLoad(getScoreFromJMHSecondaryResult(item, "performanceSystemCpuLoad", item.getParams().getMeasurement().getCount())); + report.setPerformanceProcessHeapMemoryUsed(getScoreFromJMHSecondaryResult(item, "performanceProcessHeapMemoryUsed", item.getParams().getMeasurement().getCount())); + report.setPerformanceProcessNonHeapMemoryUsed(getScoreFromJMHSecondaryResult(item, "performanceProcessNonHeapMemoryUsed", item.getParams().getMeasurement().getCount())); - String manifestData = null; - if (Manifests.exists(Constants.BENCHMARK_METADATA)) { - manifestData = Manifests.read(Constants.BENCHMARK_METADATA); - } - Map> benchmarksMetadata = ComputationUtils.parseBenchmarkMetadata(manifestData); - Map benchProps; - if (manifestData != null) { - benchProps = prepareBenchmarkProperties(report.getReportClassName(), benchmarksMetadata); - } else { - benchProps = prepareBenchmarkProperties(report.getReportClassName(), defaultBenchmarksMetadata); - } - if (benchProps.get("benchCategory") != null) { - report.setCategory(benchProps.get("benchCategory")); - } - if (benchProps.get("benchContext") != null) { - report.setContext(benchProps.get("benchContext")); - } - if (benchProps.get("benchVersion") != null) { - report.setVersion(benchProps.get("benchVersion")); - } - report.recalculateScoresToMatchNewUnits(); - overviewReport.addToBenchmarks(report); - } + String manifestData = null; + if (Manifests.exists(Constants.BENCHMARK_METADATA)) { + manifestData = Manifests.read(Constants.BENCHMARK_METADATA); + } + Map> benchmarksMetadata = ComputationUtils.parseBenchmarkMetadata(manifestData); + Map benchProps; + if (manifestData != null) { + benchProps = prepareBenchmarkProperties(report.getReportClassName(), benchmarksMetadata); + } else { + benchProps = prepareBenchmarkProperties(report.getReportClassName(), defaultBenchmarksMetadata); + } + if (benchProps.get("benchCategory") != null) { + report.setCategory(benchProps.get("benchCategory")); + } + if (benchProps.get("benchContext") != null) { + report.setContext(benchProps.get("benchContext")); + } + if (benchProps.get("benchVersion") != null) { + report.setVersion(benchProps.get("benchVersion")); + } + report.recalculateScoresToMatchNewUnits(); + overviewReport.addToBenchmarks(report); + } + + overviewReport.setTimestamp(System.currentTimeMillis()); + overviewReport.setTimestampUTC(ZonedDateTime.now(ZoneOffset.UTC).toInstant().toEpochMilli()); + // overviewReport.computeScores(); + + return overviewReport; + } - overviewReport.setTimestamp(System.currentTimeMillis()); - overviewReport.setTimestampUTC(ZonedDateTime.now(ZoneOffset.UTC).toInstant().toEpochMilli()); -// overviewReport.computeScores(); + public Double checkValueExistence(Double value) { + if (value != null && value == -1) { + return null; + } else { + return value; + } + } - return overviewReport; - } + public Map prepareBenchmarkProperties(String className, + Map> benchmarksMetadata) { + Map benchmarkProperties = new HashMap<>(); + try { + if (benchmarksMetadata.get(className) != null) { + if (benchmarksMetadata.get(className).get("category") != null) { + benchmarkProperties.put("benchCategory", benchmarksMetadata.get(className).get("category")); + } + if (benchmarksMetadata.get(className).get("context") != null) { + benchmarkProperties.put("benchContext", benchmarksMetadata.get(className).get("context")); + } + if (benchmarksMetadata.get(className).get("version") != null) { + benchmarkProperties.put("benchVersion", benchmarksMetadata.get(className).get("version")); + } + } else { + benchmarkProperties.put("benchCategory", "CUSTOM"); + benchmarkProperties.put("benchContext", "Custom"); + benchmarkProperties.put("benchVersion", "1.0.0"); + } + return benchmarkProperties; + } catch (Exception e) { + LOG.error("Error on resolving benchmarks category, context and version: class={}", className, e); + } + return benchmarkProperties; + } - public Double checkValueExistence(Double value){ - if(value != null && value == -1){ - return null; - }else{ - return value; - } - } - public Map prepareBenchmarkProperties(String className, - Map> benchmarksMetadata) { - Map benchmarkProperties = new HashMap<>(); - try { - if (benchmarksMetadata.get(className) != null) { - if (benchmarksMetadata.get(className).get("category") != null) { - benchmarkProperties.put("benchCategory", benchmarksMetadata.get(className).get("category")); - } - if (benchmarksMetadata.get(className).get("context") != null) { - benchmarkProperties.put("benchContext", benchmarksMetadata.get(className).get("context")); - } - if (benchmarksMetadata.get(className).get("version") != null) { - benchmarkProperties.put("benchVersion", benchmarksMetadata.get(className).get("version")); - } - } else { - benchmarkProperties.put("benchCategory", "CUSTOM"); - benchmarkProperties.put("benchContext", "Custom"); - benchmarkProperties.put("benchVersion", "1.0.0"); - } - return benchmarkProperties; - } catch (Exception e) { - LOG.error("Error on resolving benchmarks category, context and version: class={}", className, e); - } - return benchmarkProperties; - } + public Map prepareBenchmarkSettings(String className, + Map> benchmarksMetadata) { + Map benchmarkProperties = new HashMap<>(); + try { + if (benchmarksMetadata.get(className) != null) { + if (benchmarksMetadata.get(className).get("context") != null) { + benchmarkProperties.put("benchContext", benchmarksMetadata.get(className).get("context")); + } + if (benchmarksMetadata.get(className).get("version") != null) { + benchmarkProperties.put("benchVersion", benchmarksMetadata.get(className).get("version")); + } + } else { + benchmarkProperties.put("benchContext", "Custom"); + benchmarkProperties.put("benchVersion", "1.0.0"); + } + return benchmarkProperties; + } catch (Exception e) { + benchmarkProperties.put("benchContext", "Custom"); + benchmarkProperties.put("benchVersion", "1.0.0"); + LOG.error("Error on resolving category: class={}", className, e); + } + return benchmarkProperties; + } - public Map prepareBenchmarkSettings(String className, - Map> benchmarksMetadata) { - Map benchmarkProperties = new HashMap<>(); - try { - if (benchmarksMetadata.get(className) != null) { - if (benchmarksMetadata.get(className).get("context") != null) { - benchmarkProperties.put("benchContext", benchmarksMetadata.get(className).get("context")); - } - if (benchmarksMetadata.get(className).get("version") != null) { - benchmarkProperties.put("benchVersion", benchmarksMetadata.get(className).get("version")); - } - } else { - benchmarkProperties.put("benchContext", "Custom"); - benchmarkProperties.put("benchVersion", "1.0.0"); - } - return benchmarkProperties; - } catch (Exception e) { - benchmarkProperties.put("benchContext", "Custom"); - benchmarkProperties.put("benchVersion", "1.0.0"); - LOG.error("Error on resolving category: class={}", className, e); - } - return benchmarkProperties; - } + protected String getVersion(String fullClassName) { + try { + Class clazz = Class.forName(fullClassName); + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + Manifest manifest = new Manifest(loader.getResourceAsStream("META-INF/MANIFEST.MF")); + String benchmarkPackageString = clazz.getPackage().getName().replace(".", "-") + "-version"; + return manifest.getMainAttributes().getValue(benchmarkPackageString); + } catch (Exception e) { + LOG.info("Could not locate the benchmark version", e); + } + return null; + } - protected String getVersion(String fullClassName) { - try { - Class clazz = Class.forName(fullClassName); - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - Manifest manifest = new Manifest(loader.getResourceAsStream("META-INF/MANIFEST.MF")); - String benchmarkPackageString = clazz.getPackage().getName().replace(".", "-") + "-version"; - return manifest.getMainAttributes().getValue(benchmarkPackageString); - } catch (Exception e) { - LOG.info("Could not locate the benchmark version", e); - } - return null; - } + public String prepareReportForDelivery(SecurityBuilder securityBuilder, BenchmarkOverviewReport report) { + try { + LOG.info("Preparing report: encrypt and sign..."); + SecuredReport securedReport = createSecuredReport(report); + securityBuilder.generateSecurityHashForReport(securedReport.getReport()); + securedReport.setSignatures(securityBuilder.buildSignatures()); + String plainReport = JSONUtils.marshalToJson(securedReport); + return SecurityUtils.encryptReport(plainReport); + } finally { + LOG.info("Report prepared: encrypted and signed"); + } + } - public String prepareReportForDelivery(SecurityBuilder securityBuilder, BenchmarkOverviewReport report) { - try { - LOG.info("Preparing report: encrypt and sign..."); - SecuredReport securedReport = createSecuredReport(report); - securityBuilder.generateSecurityHashForReport(securedReport.getReport()); - securedReport.setSignatures(securityBuilder.buildSignatures()); - String plainReport = JSONUtils.marshalToJson(securedReport); - return SecurityUtils.encryptReport(plainReport); - } finally { - LOG.info("Report prepared: encrypted and signed"); - } - } + private SecuredReport createSecuredReport(BenchmarkOverviewReport report) { + SecuredReport securedReport = new SecuredReport(); + securedReport.setReport(JSONUtils.marshalToJson(report)); + return securedReport; + } - private SecuredReport createSecuredReport(BenchmarkOverviewReport report) { - SecuredReport securedReport = new SecuredReport(); - securedReport.setReport(JSONUtils.marshalToJson(report)); - return securedReport; - } + private Double getScoreFromJMHSecondaryResult(RunResult result, String key) { + if (result != null && result.getSecondaryResults() != null) { + if (result.getSecondaryResults().get(key) != null) { + return result.getSecondaryResults().get(key).getScore(); + } + } + return null; + } - private Double getScoreFromJMHSecondaryResult(RunResult result, String key) { - if (result != null && result.getSecondaryResults() != null) { - if (result.getSecondaryResults().get(key) != null) { - return result.getSecondaryResults().get(key).getScore(); - } - } - return null; - } - private Double getScoreFromJMHSecondaryResult(RunResult result, String key, int denominator) { - Double value = getScoreFromJMHSecondaryResult(result, key) ; - checkValueExistence(value); - if (value != null && denominator != 0){ - return value/denominator ; - } - return value ; - } + private Double getScoreFromJMHSecondaryResult(RunResult result, String key, int denominator) { + Double value = getScoreFromJMHSecondaryResult(result, key); + checkValueExistence(value); + if (value != null && denominator != 0) { + return value / denominator; + } + return value; + } } diff --git a/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/utils/JSONUtils.java b/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/utils/JSONUtils.java deleted file mode 100644 index d76f22c9..00000000 --- a/gocypher-cybench-client/gocypher-cybench-runner/src/main/java/com/gocypher/cybench/launcher/utils/JSONUtils.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2020, K2N.IO. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -package com.gocypher.cybench.launcher.utils; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class JSONUtils { - private static Logger LOG = LoggerFactory.getLogger(JSONUtils.class); - private static ObjectMapper mapper = new ObjectMapper(); - - public static Map parseJsonIntoMap(String jsonString) { - try { - return mapper.readValue(jsonString, HashMap.class); - } catch (Exception e) { - LOG.error("Error on parsing json into map", e); - return new HashMap<>(); - } - } - public static List parseJsonIntoList(String jsonString) { - try { - return mapper.readValue(jsonString, ArrayList.class); - } catch (Exception e) { - LOG.error("Error on parsing json into map", e); - return new ArrayList<>(); - } - } - public static String marshalToJson(Object item) { - try { - return mapper.writeValueAsString(item); - } catch (Exception e) { - LOG.error ("Error on marshaling to json",e) ; - return ""; - } - } - public static String marshalToPrettyJson(Object item) { - try { - return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(item); - } catch (Exception e) { - LOG.error ("Error on storing results",e) ; - return ""; - } - } - -} diff --git a/gocypher-cybench-client/gocypher-cybench-runner/src/test/java/CollectPropertiesRun.java b/gocypher-cybench-client/gocypher-cybench-runner/src/test/java/CollectPropertiesRun.java index 33f2cd2d..40ca985f 100644 --- a/gocypher-cybench-client/gocypher-cybench-runner/src/test/java/CollectPropertiesRun.java +++ b/gocypher-cybench-client/gocypher-cybench-runner/src/test/java/CollectPropertiesRun.java @@ -17,42 +17,45 @@ * */ +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryUsage; +import java.text.DecimalFormat; +import java.util.*; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.gocypher.cybench.launcher.environment.model.HardwareProperties; import com.gocypher.cybench.launcher.environment.model.JVMProperties; import com.gocypher.cybench.launcher.environment.services.CollectSystemInformation; import com.sun.management.GarbageCollectorMXBean; import com.sun.management.GcInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.management.MBeanServer; -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryUsage; -import java.text.DecimalFormat; -import java.util.*; public class CollectPropertiesRun { - private static final String GC_BEAN_NAME = - "java.lang:type=GarbageCollector,name=PS MarkSweep"; + private static final String GC_BEAN_NAME = "java.lang:type=GarbageCollector,name=PS MarkSweep"; private static volatile GarbageCollectorMXBean gcMBean; private static final Logger LOG = LoggerFactory.getLogger(CollectSystemInformation.class); static HardwareProperties hardwareProp = new HardwareProperties(); static JVMProperties jvmProperties = new JVMProperties(); private static final DecimalFormat df = new DecimalFormat("#.####"); - private static final String[] excludeWindowsMACs = {"virtual", "hyper-v", "npcap"}; + private static final String[] excludeWindowsMACs = { "virtual", "hyper-v", "npcap" }; - public static void main (String [] args)throws Exception{ + public static void main(String[] args) throws Exception { getGCInfo(args); getHardwarePropsInfo(args); } - private static void getGCInfo(String [] args){ + private static void getGCInfo(String[] args) { try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - Set names = mbs.queryNames(null, null); + Set names = mbs.queryNames(null, null); System.out.println(names.toString().replace(", ", System.getProperty("line.separator"))); - List gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans(); + List gcMxBeans = ManagementFactory + .getGarbageCollectorMXBeans(); for (java.lang.management.GarbageCollectorMXBean gcMxBean : gcMxBeans) { System.out.println(gcMxBean.getName()); System.out.println(gcMxBean.getObjectName()); @@ -64,39 +67,41 @@ private static void getGCInfo(String [] args){ throw new RuntimeException(exp); } } - private static void getHardwarePropsInfo(String [] args){ - LOG.info ("--- Main Started successfully ---") ; - LOG.info ("---------------------------------------------------------------------------") ; - LOG.info ("") ; + + private static void getHardwarePropsInfo(String[] args) { + LOG.info("--- Main Started successfully ---"); + LOG.info("---------------------------------------------------------------------------"); + LOG.info(""); int index; - for (String commands: args) { - switch(commands) { - case "jvmPropertiesToFile": - CollectSystemInformation.getJavaVirtualMachineProperties(); - CollectSystemInformation.outputHardwareDataObjectToFile(jvmProperties, "JVMProperties.txt"); - break; - case "hardwarePropertiesToFile": - CollectSystemInformation.getEnvironmentProperties(); - CollectSystemInformation.outputHardwareDataObjectToFile(hardwareProp, "HardwareProperties.txt"); - break; - case "jvmPropertiesToFile=": - index = Collections.singletonList(commands).indexOf("jvmPropertiesToFile=*"); - CollectSystemInformation.getJavaVirtualMachineProperties(); - CollectSystemInformation.outputHardwareDataObjectToFile(jvmProperties, args[index+1]); - break; - case "hardwarePropertiesToFile=": - index = Collections.singletonList(commands).indexOf("hardwarePropertiesToFile=*"); - CollectSystemInformation.getEnvironmentProperties(); - CollectSystemInformation.outputHardwareDataObjectToFile(hardwareProp, args[index+1]); - break; - default: - LOG.info ("No additional output selected") ; + for (String commands : args) { + switch (commands) { + case "jvmPropertiesToFile": + CollectSystemInformation.getJavaVirtualMachineProperties(); + CollectSystemInformation.outputHardwareDataObjectToFile(jvmProperties, "JVMProperties.txt"); + break; + case "hardwarePropertiesToFile": + CollectSystemInformation.getEnvironmentProperties(); + CollectSystemInformation.outputHardwareDataObjectToFile(hardwareProp, "HardwareProperties.txt"); + break; + case "jvmPropertiesToFile=": + index = Collections.singletonList(commands).indexOf("jvmPropertiesToFile=*"); + CollectSystemInformation.getJavaVirtualMachineProperties(); + CollectSystemInformation.outputHardwareDataObjectToFile(jvmProperties, args[index + 1]); + break; + case "hardwarePropertiesToFile=": + index = Collections.singletonList(commands).indexOf("hardwarePropertiesToFile=*"); + CollectSystemInformation.getEnvironmentProperties(); + CollectSystemInformation.outputHardwareDataObjectToFile(hardwareProp, args[index + 1]); + break; + default: + LOG.info("No additional output selected"); } } } public CollectPropertiesRun() { } + // initialize the GC MBean field private static void initGCMBean() { if (gcMBean == null) { @@ -107,14 +112,14 @@ private static void initGCMBean() { } } } + // get the GarbageCollectorMXBean MBean from the // platform MBean server private static GarbageCollectorMXBean getGCMBean() { try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - GarbageCollectorMXBean bean = - ManagementFactory.newPlatformMXBeanProxy(server, - GC_BEAN_NAME, GarbageCollectorMXBean.class); + GarbageCollectorMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, GC_BEAN_NAME, + GarbageCollectorMXBean.class); return bean; } catch (RuntimeException re) { throw re; @@ -125,15 +130,15 @@ private static GarbageCollectorMXBean getGCMBean() { static boolean printGCInfo() { // initialize GC MBean - List test = new ArrayList<>(); + List test = new ArrayList<>(); test.add("test1"); test.add("test1"); test.add("test1"); test.add("test1"); test.add("test1"); test = null; - System.gc (); - System.runFinalization (); + System.gc(); + System.runFinalization(); initGCMBean(); try { GcInfo gci = gcMBean.getLastGcInfo(); @@ -143,46 +148,34 @@ static boolean printGCInfo() { long endTime = gci.getEndTime(); long duration = gci.getDuration(); if (startTime == endTime) { - return false; // no gc + return false; // no gc } System.out.println("GC ID: " + id); System.out.println("Start Time: " + startTime); System.out.println("End Time: " + endTime); System.out.println("Duration: " + duration); - Map mapBefore = gci.getMemoryUsageBeforeGc(); - Map mapAfter = gci.getMemoryUsageAfterGc(); System.out.println("Before GC Memory Usage Details...."); - Set memType = mapBefore.keySet(); - Iterator it = memType.iterator(); - while (it.hasNext()) { - String type = (String) it.next(); - System.out.println(type); - MemoryUsage mu1 = (MemoryUsage) mapBefore.get(type); - System.out.print("Initial Size: " + mu1.getInit()); - System.out.print(" Used: " + mu1.getUsed()); - System.out.print(" Max: " + mu1.getMax()); - System.out.print(" Committed: " + mu1.getCommitted()); - System.out.println(" "); - } + printMemUse(gci.getMemoryUsageBeforeGc()); System.out.println("After GC Memory Usage Details...."); - memType = mapAfter.keySet(); - it = memType.iterator(); - while (it.hasNext()) { - String type = (String) it.next(); - System.out.println(type); - MemoryUsage mu2 = (MemoryUsage) mapAfter.get(type); - System.out.print("Initial Size: " + mu2.getInit()); - System.out.print(" Used: " + mu2.getUsed()); - System.out.print(" Max: " + mu2.getMax()); - System.out.print(" Committed: " + mu2.getCommitted()); - System.out.println(" "); - } + printMemUse(gci.getMemoryUsageAfterGc()); } - } catch(RuntimeException re){ + } catch (RuntimeException re) { throw re; - } catch(Exception exp){ + } catch (Exception exp) { throw new RuntimeException(exp); } return true; } + + private static void printMemUse(Map memMap) { + for (Map.Entry me : memMap.entrySet()) { + System.out.println(me.getKey()); + MemoryUsage mu = me.getValue(); + System.out.print("Initial Size: " + mu.getInit()); + System.out.print(" Used: " + mu.getUsed()); + System.out.print(" Max: " + mu.getMax()); + System.out.print(" Committed: " + mu.getCommitted()); + System.out.println(" "); + } + } } \ No newline at end of file