-
Notifications
You must be signed in to change notification settings - Fork 860
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
19fc7a6
Jetty-based benchmark for memory usage
pmm-sumo cb9ffd6
Fix formatting
pmm-sumo 623be2a
Fix headers
pmm-sumo 561e44e
Remove unnecessary code
pmm-sumo 1312abc
Remove unnecessary code
pmm-sumo 83e023a
Remove unnecessary code
pmm-sumo 2fb6811
Formatting and code cleanup
pmm-sumo 6344a6b
Formatting fix
pmm-sumo fae9094
Merge branch 'master' into memory-churn-benchmark
trask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
benchmark/src/jmh/java/io/opentelemetry/benchmark/HttpBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.util.component.AbstractLifeCycle; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
|
||
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 (final Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@TearDown(Level.Trial) | ||
public void doTearDown() { | ||
try { | ||
jettyServer.stop(); | ||
} catch (final Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
jettyServer.destroy(); | ||
} | ||
} | ||
|
||
HttpClass http = new HttpClass(); | ||
Server jettyServer; | ||
} | ||
|
||
@Benchmark | ||
public void testMakingRequest(final BenchmarkState state) throws IOException { | ||
state.http.executeRequest(); | ||
} | ||
|
||
@Fork( | ||
jvmArgsAppend = { | ||
"-javaagent:/path/to/opentelemetry-auto-instr-java/java-agent/build/libs/opentelemetry-auto.jar", | ||
"-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 {} | ||
} |
66 changes: 66 additions & 0 deletions
66
benchmark/src/jmh/java/io/opentelemetry/benchmark/UsedMemoryProfiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.infra.BenchmarkParams; | ||
import org.openjdk.jmh.infra.IterationParams; | ||
import org.openjdk.jmh.profile.InternalProfiler; | ||
import org.openjdk.jmh.results.AggregationPolicy; | ||
import org.openjdk.jmh.results.IterationResult; | ||
import org.openjdk.jmh.results.Result; | ||
import org.openjdk.jmh.results.ScalarResult; | ||
|
||
public class UsedMemoryProfiler implements InternalProfiler { | ||
private long totalHeapBefore; | ||
private long usedHeapBefore; | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Used memory heap profiler"; | ||
} | ||
|
||
@Override | ||
public void beforeIteration( | ||
final BenchmarkParams benchmarkParams, final IterationParams iterationParams) { | ||
System.gc(); | ||
System.runFinalization(); | ||
|
||
totalHeapBefore = Runtime.getRuntime().totalMemory(); | ||
usedHeapBefore = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); | ||
} | ||
|
||
@Override | ||
public Collection<? extends Result> afterIteration( | ||
final BenchmarkParams benchmarkParams, | ||
final IterationParams iterationParams, | ||
final IterationResult result) { | ||
|
||
final long totalHeap = Runtime.getRuntime().totalMemory(); | ||
final long usedHeap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); | ||
|
||
final Collection<ScalarResult> results = new ArrayList<>(); | ||
results.add( | ||
new ScalarResult("heap.total.before", totalHeapBefore, "bytes", AggregationPolicy.AVG)); | ||
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)); | ||
|
||
return results; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
benchmark/src/jmh/java/io/opentelemetry/benchmark/classes/HttpClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.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 | ||
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\"}"); | ||
} | ||
} | ||
|
||
private HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(); | ||
|
||
public void executeRequest() throws IOException { | ||
String url = "http://localhost:" + port + contextPath; | ||
|
||
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url)); | ||
request.setThrowExceptionOnExecuteError(false); | ||
request.execute(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.