changeset 1196:5f5bfa899790

7901388: Profilers should not return -1 when the result is absent
author shade
date Wed, 15 Apr 2015 19:25:00 +0300
parents 20c5dbbe3e3d
children 03adf23f98ed
files jmh-core/src/main/java/org/openjdk/jmh/profile/ClassloaderProfiler.java jmh-core/src/main/java/org/openjdk/jmh/profile/CompilerProfiler.java
diffstat 2 files changed, 29 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/ClassloaderProfiler.java	Wed Apr 15 00:40:46 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/ClassloaderProfiler.java	Wed Apr 15 19:25:00 2015 +0300
@@ -32,14 +32,14 @@
 
 import java.lang.management.ClassLoadingMXBean;
 import java.lang.management.ManagementFactory;
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
 public class ClassloaderProfiler implements InternalProfiler {
 
-    private long loadedClasses = -1;
-    private long unloadedClasses = -1;
+    private long loadedClasses;
+    private long unloadedClasses;
 
     @Override
     public String getDescription() {
@@ -60,47 +60,43 @@
     public void beforeIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams) {
         ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
         try {
-            loadedClasses = cl.getLoadedClassCount();
+            loadedClasses = cl.getTotalLoadedClassCount();
         } catch (UnsupportedOperationException e) {
-            loadedClasses = -1;
+            // do nothing
         }
         try {
             unloadedClasses = cl.getUnloadedClassCount();
         } catch (UnsupportedOperationException e) {
-            unloadedClasses = -1;
+            // do nothing
         }
     }
 
     @Override
     public Collection<? extends Result> afterIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams, IterationResult result) {
-        long loaded;
-        long unloaded;
+        List<Result> results = new ArrayList<Result>();
+
         ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
 
-        int loadedClassCount;
         try {
-            loadedClassCount = cl.getLoadedClassCount();
-            loaded = loadedClassCount - loadedClasses;
+            long loadedClassCount = cl.getTotalLoadedClassCount();
+            long loaded = loadedClassCount - loadedClasses;
+            results.add(new ProfilerResult(Defaults.PREFIX + "classload.loaded.profiled", loaded, "classes", AggregationPolicy.SUM));
+            results.add(new ProfilerResult(Defaults.PREFIX + "classload.loaded.total", loadedClassCount, "classes", AggregationPolicy.MAX));
         } catch (UnsupportedOperationException e) {
-            loaded = -1;
-            loadedClassCount = -1;
+            // do nothing
         }
 
-        long unloadedClassCount;
         try {
-            unloadedClassCount = cl.getUnloadedClassCount();
-            unloaded = unloadedClassCount - unloadedClasses;
+            long unloadedClassCount = cl.getUnloadedClassCount();
+            long unloaded = unloadedClassCount - unloadedClasses;
+            results.add(new ProfilerResult(Defaults.PREFIX + "classload.unloaded.profiled", unloaded, "classes", AggregationPolicy.SUM));
+            results.add(new ProfilerResult(Defaults.PREFIX + "classload.unloaded.total", unloadedClassCount, "classes", AggregationPolicy.MAX));
+
         } catch (UnsupportedOperationException e) {
-            unloaded = -1;
-            unloadedClassCount = -1;
+            // do nothing
         }
 
-        return Arrays.asList(
-                new ProfilerResult(Defaults.PREFIX + "classload.loaded.profiled", loaded, "classes", AggregationPolicy.SUM),
-                new ProfilerResult(Defaults.PREFIX + "classload.unloaded.profiled", unloaded, "classes", AggregationPolicy.SUM),
-                new ProfilerResult(Defaults.PREFIX + "classload.loaded.total", loadedClassCount, "classes", AggregationPolicy.MAX),
-                new ProfilerResult(Defaults.PREFIX + "classload.unloaded.total", unloadedClassCount, "classes", AggregationPolicy.MAX)
-        );
+        return results;
     }
 
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/CompilerProfiler.java	Wed Apr 15 00:40:46 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/CompilerProfiler.java	Wed Apr 15 19:25:00 2015 +0300
@@ -34,11 +34,12 @@
 import java.lang.management.ManagementFactory;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 public class CompilerProfiler implements InternalProfiler {
 
-    private long startCompTime = -1;
+    private long startCompTime;
 
     @Override
     public String getDescription() {
@@ -67,24 +68,22 @@
         try {
             startCompTime = comp.getTotalCompilationTime();
         } catch (UnsupportedOperationException e) {
-            startCompTime = -1;
+            // do nothing
         }
     }
 
     @Override
     public Collection<? extends Result> afterIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams, IterationResult result) {
-        long compTime = -startCompTime;
         CompilationMXBean comp = ManagementFactory.getCompilationMXBean();
         try {
-            compTime += comp.getTotalCompilationTime();
+            long curTime = comp.getTotalCompilationTime();
+            return Arrays.asList(
+                new ProfilerResult(Defaults.PREFIX + "compiler.time.profiled", curTime - startCompTime, "ms", AggregationPolicy.SUM),
+                new ProfilerResult(Defaults.PREFIX + "compiler.time.total", curTime, "ms", AggregationPolicy.MAX)
+            );
         } catch (UnsupportedOperationException e) {
-            compTime = -1;
+            return Collections.emptyList();
         }
-
-        return Arrays.asList(
-                new ProfilerResult(Defaults.PREFIX + "compiler.time.profiled", compTime, "ms", AggregationPolicy.SUM),
-                new ProfilerResult(Defaults.PREFIX + "compiler.time.total", comp.getTotalCompilationTime(), "ms", AggregationPolicy.MAX)
-        );
     }
 
 }