diff --git a/src/main/java/jenkins/benchmark/jmh/JmhJenkinsRule.java b/src/main/java/jenkins/benchmark/jmh/JmhJenkinsRule.java new file mode 100644 index 000000000..f2fe59126 --- /dev/null +++ b/src/main/java/jenkins/benchmark/jmh/JmhJenkinsRule.java @@ -0,0 +1,44 @@ +package jenkins.benchmark.jmh; + +import com.gargoylesoftware.htmlunit.Cache; +import jenkins.model.Jenkins; +import org.jvnet.hudson.test.JenkinsRule; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Objects; + +/** + * Extension of {@link JenkinsRule} to allow it to be used from JMH benchmarks. + *
+ * This class should be instantiated only when the Jenkins instance is confirmed to exist. + * + * @since TODO + */ +public class JmhJenkinsRule extends JenkinsRule { + private final Jenkins jenkins; + + public JmhJenkinsRule() { + super(); + jenkins = Objects.requireNonNull(Jenkins.getInstanceOrNull()); + super.jenkins = null; // the jenkins is not started from JenkinsRule + } + + @Override + public URL getURL() throws MalformedURLException { + // the rootURL should not be null as it should've been set by JmhBenchmarkState + return new URL(Objects.requireNonNull(jenkins.getRootUrl())); + } + + /** + * {@inheritDoc} + */ + @Override + public WebClient createWebClient() { + WebClient webClient = super.createWebClient(); + Cache cache = new Cache(); + cache.setMaxSize(0); + webClient.setCache(cache); // benchmarks should not rely on cached content + return webClient; + } +} diff --git a/src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java b/src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java index 2c637234f..fc07f4d3c 100644 --- a/src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java +++ b/src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java @@ -26,6 +26,7 @@ public void testJmhBenchmarks() throws Exception { ChainedOptionsBuilder optionsBuilder = new OptionsBuilder() .forks(1) + .threads(1) .warmupIterations(0) .measurementIterations(1) .measurementBatchSize(1) diff --git a/src/test/java/jenkins/benchmark/jmh/samples/WebClientBenchmark.java b/src/test/java/jenkins/benchmark/jmh/samples/WebClientBenchmark.java new file mode 100644 index 000000000..a3f08367a --- /dev/null +++ b/src/test/java/jenkins/benchmark/jmh/samples/WebClientBenchmark.java @@ -0,0 +1,31 @@ +package jenkins.benchmark.jmh.samples; + +import jenkins.benchmark.jmh.JmhBenchmark; +import jenkins.benchmark.jmh.JmhBenchmarkState; +import jenkins.benchmark.jmh.JmhJenkinsRule; +import org.jvnet.hudson.test.JenkinsRule; +import org.openjdk.jmh.annotations.Benchmark; + +@JmhBenchmark +public class WebClientBenchmark { + public static class MyState extends JmhBenchmarkState { + JenkinsRule.WebClient webClient = null; + + @Override + public void setup() { + JmhJenkinsRule jenkinsRule = new JmhJenkinsRule(); + getJenkins().setSecurityRealm(jenkinsRule.createDummySecurityRealm()); + webClient = jenkinsRule.createWebClient(); + } + + @Override + public void tearDown() { + webClient.close(); + } + } + + @Benchmark + public void benchmark(MyState state) throws Exception { + state.webClient.goTo(""); + } +}