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

Jetty-based benchmark for memory usage #329

Merged
merged 9 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions benchmark/benchmark.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ apply from: "${rootDir}/gradle/java.gradle"
dependencies {
jmh group: 'io.opentelemetry', name: 'opentelemetry-api', version: '0.3.0'
jmh deps.bytebuddyagent

jmh 'javax.servlet:javax.servlet-api:4.0.1'
jmh 'com.google.http-client:google-http-client:1.19.0'
jmh 'org.eclipse.jetty:jetty-server:9.4.1.v20170120'
jmh 'org.eclipse.jetty:jetty-servlet:9.4.1.v20170120'
}

jmh {
Expand All @@ -25,6 +30,7 @@ jmh {

// profilers = ['stack:lines=5;detailLine=true;period=5;excludePackages=true']
// Use profilers to collect additional data. Supported profilers: [cl, comp, gc, stack, perf, perfnorm, perfasm, xperf, xperfasm, hs_cl, hs_comp, hs_gc, hs_rt, hs_thr]
profilers = ['io.opentelemetry.benchmark.UsedMemoryProfiler', 'gc']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool, I hadn't seen custom profiler before.


// humanOutputFile = project.file("${project.buildDir}/reports/jmh/human.txt") // human-readable output file
// operationsPerInvocation = 10 // Operations per invocation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.benchmark;

import io.opentelemetry.benchmark.classes.HttpClass;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.openjdk.jmh.annotations.*;

public class HttpBenchmark {

@State(Scope.Benchmark)
public static class BenchmarkState {
@Setup(Level.Trial)
public void doSetup() {
try {
jettyServer = new HttpClass().buildJettyServer();
jettyServer.start();
// Make sure it's actually running
while (!AbstractLifeCycle.STARTED.equals(jettyServer.getState())) {
Thread.sleep(500);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@TearDown(Level.Trial)
public void doTearDown() {
try {
jettyServer.stop();
} catch (Exception e) {
e.printStackTrace();
} finally {
jettyServer.destroy();
}
}

HttpClass http = new HttpClass();
Server jettyServer;
}

@Benchmark
public void testMakingRequest(BenchmarkState state) {
state.http.executeRequest();
}

@Fork(
jvmArgsAppend = {
"-javaagent:/path/to/opentelemetry-auto-instr-java/java-agent/build/libs/opentelemetry-auto.jar",
"-javaagent:/path/to/opentelemetry-auto-instr-java/java-agent/build/libs/opentelemetry-auto.jar",
pmm-sumo marked this conversation as resolved.
Show resolved Hide resolved
"-Dota.exporter.jar=/path/to/opentelemetry-auto-instr-java/auto-exporters/logging/build/libs/opentelemetry-auto-exporters-logging-or-other-exporter.jar"
})
public static class WithAgent extends ClassRetransformingBenchmark {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.benchmark;

import java.util.ArrayList;
import java.util.Collection;
import org.openjdk.jmh.*;
pmm-sumo marked this conversation as resolved.
Show resolved Hide resolved
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.*;
import org.openjdk.jmh.profile.*;
import org.openjdk.jmh.results.*;

public class UsedMemoryProfiler implements InternalProfiler {
private long totalHeapBefore;
private long usedHeapBefore;

@Override
public String getDescription() {
return "Used memory heap profiler";
}

@Override
public void beforeIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams) {
System.gc();
System.runFinalization();

totalHeapBefore = Runtime.getRuntime().totalMemory();
usedHeapBefore = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}

@Override
public Collection<? extends Result> afterIteration(
BenchmarkParams benchmarkParams, IterationParams iterationParams, IterationResult result) {

long totalHeap = Runtime.getRuntime().totalMemory();
long usedHeap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

Collection<ScalarResult> results = new ArrayList<>();
results.add(
new ScalarResult("·heap.total.before", totalHeapBefore, "bytes", AggregationPolicy.AVG));
pmm-sumo marked this conversation as resolved.
Show resolved Hide resolved
results.add(
new ScalarResult("·heap.used.before", usedHeapBefore, "bytes", AggregationPolicy.AVG));
results.add(new ScalarResult("·heap.total.after", totalHeap, "bytes", AggregationPolicy.AVG));
results.add(new ScalarResult("·heap.used.after", usedHeap, "bytes", AggregationPolicy.AVG));
results.add(
new ScalarResult(
"·heap.total.change", totalHeap - totalHeapBefore, "bytes", AggregationPolicy.AVG));
results.add(
new ScalarResult(
"·heap.used.change", usedHeap - usedHeapBefore, "bytes", AggregationPolicy.AVG));

return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.benchmark.classes;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.javanet.NetHttpTransport;
import java.io.IOException;
import java.net.InetSocketAddress;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class HttpClass {
private String contextPath = "/path";
private Integer port = 18888;

public Server buildJettyServer() {
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "WARN");

Server jettyServer = new Server(new InetSocketAddress("localhost", port));
ServletContextHandler servletContext = new ServletContextHandler();

servletContext.addServlet(HttpClassServlet.class, contextPath);
jettyServer.setHandler(servletContext);
return jettyServer;
}

@WebServlet
public static class HttpClassServlet extends HttpServlet {
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
}

pmm-sumo marked this conversation as resolved.
Show resolved Hide resolved
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
try {
Thread.sleep(10);
} catch (Exception e) {
}
resp.setContentType("application/json");
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().println("{ \"status\": \"ok\"}");
}

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
pmm-sumo marked this conversation as resolved.
Show resolved Hide resolved
}

private HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();

public void executeRequest() {
String url = "http://localhost:" + port + contextPath;

try {
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url));
request.setThrowExceptionOnExecuteError(false);
request.execute();
} catch (Exception ex) {
ex.printStackTrace();
pmm-sumo marked this conversation as resolved.
Show resolved Hide resolved
}
}
}