changeset 1022:2cfeed0767c5

7901083: User-defined Profilers are ignored on user classpath Summary: use ServiceLoader.load, add regression/integration tests.
author shade
date Tue, 04 Nov 2014 02:08:02 +0300
parents 07488524786d
children 9237036291e9
files jmh-core-it/src/test/java/org/openjdk/jmh/it/profilers/ItExternalProfiler.java jmh-core-it/src/test/java/org/openjdk/jmh/it/profilers/ItInternalProfiler.java jmh-core-it/src/test/java/org/openjdk/jmh/it/profilers/ProfilerTest.java jmh-core-it/src/test/resources/META-INF/services/org.openjdk.jmh.profile.Profiler jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java
diffstat 5 files changed, 235 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/profilers/ItExternalProfiler.java	Tue Nov 04 02:08:02 2014 +0300
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code 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 General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.it.profilers;
+
+import org.openjdk.jmh.infra.BenchmarkParams;
+import org.openjdk.jmh.profile.ExternalProfiler;
+import org.openjdk.jmh.results.Result;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+public class ItExternalProfiler implements ExternalProfiler {
+
+    @Override
+    public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public Collection<String> addJVMOptions(BenchmarkParams params) {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public void beforeTrial(BenchmarkParams benchmarkParams) {
+        // intentionally left blank
+    }
+
+    @Override
+    public Collection<? extends Result> afterTrial(BenchmarkParams benchmarkParams, File stdOut, File stdErr) {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public boolean allowPrintOut() {
+        return true;
+    }
+
+    @Override
+    public boolean allowPrintErr() {
+        return true;
+    }
+
+    @Override
+    public boolean checkSupport(List<String> msgs) {
+        return true;
+    }
+
+    @Override
+    public String label() {
+        return "itexternal";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Integration Test External Profiler";
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/profilers/ItInternalProfiler.java	Tue Nov 04 02:08:02 2014 +0300
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code 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 General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.it.profilers;
+
+import org.openjdk.jmh.infra.BenchmarkParams;
+import org.openjdk.jmh.infra.IterationParams;
+import org.openjdk.jmh.profile.InternalProfiler;
+import org.openjdk.jmh.results.Result;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+public class ItInternalProfiler implements InternalProfiler {
+
+    @Override
+    public void beforeIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams) {
+        // intentionally blank
+    }
+
+    @Override
+    public Collection<? extends Result> afterIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams) {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public boolean checkSupport(List<String> msgs) {
+        return true;
+    }
+
+    @Override
+    public String label() {
+        return "itinternal";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Integration Test Internal Profiler";
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/profilers/ProfilerTest.java	Tue Nov 04 02:08:02 2014 +0300
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code 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 General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.it.profilers;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.it.Fixtures;
+import org.openjdk.jmh.profile.Profiler;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.CommandLineOptionException;
+import org.openjdk.jmh.runner.options.CommandLineOptions;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+public class ProfilerTest {
+
+    @Benchmark
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.MILLISECONDS)
+    @Fork(1)
+    public void bench() {
+      // intentionally left blank
+    }
+
+    @Test
+    public void testInternal_API() throws RunnerException {
+        Options opts = new OptionsBuilder()
+                .include(Fixtures.getTestMask(this.getClass()))
+                .addProfiler(ItInternalProfiler.class)
+                .build();
+        new Runner(opts).run();
+    }
+
+    @Test
+    public void testExternal_API() throws RunnerException {
+        Options opts = new OptionsBuilder()
+                .include(Fixtures.getTestMask(this.getClass()))
+                .addProfiler(ItExternalProfiler.class)
+                .build();
+        new Runner(opts).run();
+    }
+
+    @Test
+    public void testInternal_CLI() throws RunnerException, CommandLineOptionException {
+        Set<Class<? extends Profiler>> ps = new CommandLineOptions("-prof", "itinternal").getProfilers();
+        Assert.assertEquals(1, ps.size());
+        Assert.assertEquals(ItInternalProfiler.class.getName(), ps.iterator().next().getName());
+    }
+
+    @Test
+    public void testExternal_CLI() throws RunnerException, CommandLineOptionException {
+        Set<Class<? extends Profiler>> ps = new CommandLineOptions("-prof", "itexternal").getProfilers();
+        Assert.assertEquals(1, ps.size());
+        Assert.assertEquals(ItExternalProfiler.class.getName(), ps.iterator().next().getName());
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/resources/META-INF/services/org.openjdk.jmh.profile.Profiler	Tue Nov 04 02:08:02 2014 +0300
@@ -0,0 +1,2 @@
+org.openjdk.jmh.it.profilers.ItInternalProfiler
+org.openjdk.jmh.it.profilers.ItExternalProfiler
\ No newline at end of file
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java	Mon Nov 03 14:59:17 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/ProfilerFactory.java	Tue Nov 04 02:08:02 2014 +0300
@@ -56,8 +56,9 @@
 
     public static List<Class<? extends Profiler>> getDiscoveredProfilers() {
         List<Class<? extends Profiler>> profs = new ArrayList<Class<? extends Profiler>>();
-        for (Profiler s : ServiceLoader.loadInstalled(Profiler.class))
+        for (Profiler s : ServiceLoader.load(Profiler.class)) {
             profs.add(s.getClass());
+        }
         return profs;
     }