Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for running benchmarks with WebClient #142

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/jenkins/benchmark/jmh/JmhJenkinsRule.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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;
}
}
1 change: 1 addition & 0 deletions src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void testJmhBenchmarks() throws Exception {
ChainedOptionsBuilder optionsBuilder =
new OptionsBuilder()
.forks(1)
.threads(1)
.warmupIterations(0)
.measurementIterations(1)
.measurementBatchSize(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -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("");
}
}