Skip to content

Commit

Permalink
Use a custom output stream in marshaling jmh benchmarks (#5964)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Nov 7, 2023
1 parent 1ecc919 commit f9be682
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ public class GrpcGzipBenchmark {
}

@Benchmark
public ByteArrayOutputStream gzipCompressor() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
public TestOutputStream gzipCompressor() throws IOException {
TestOutputStream baos = new TestOutputStream();
OutputStream gzos = GZIP_CODEC.compress(baos);
METRICS_REQUEST.writeTo(gzos);
gzos.close();
return baos;
}

@Benchmark
public ByteArrayOutputStream identityCompressor() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
public TestOutputStream identityCompressor() throws IOException {
TestOutputStream baos = new TestOutputStream();
OutputStream gzos = IDENTITY_CODEC.compress(baos);
METRICS_REQUEST.writeTo(gzos);
gzos.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -117,9 +116,9 @@ public class MetricsRequestMarshalerBenchmark {
}

@Benchmark
public ByteArrayOutputStream marshaler() throws IOException {
public TestOutputStream marshaler() throws IOException {
MetricsRequestMarshaler marshaler = MetricsRequestMarshaler.create(METRICS);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
TestOutputStream bos = new TestOutputStream();
marshaler.writeBinaryTo(bos);
return bos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.exporter.internal.otlp.traces.TraceRequestMarshaler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
Expand All @@ -27,26 +26,26 @@ public class RequestMarshalBenchmarks {

@Benchmark
@Threads(1)
public ByteArrayOutputStream createCustomMarshal(RequestMarshalState state) {
public TestOutputStream createCustomMarshal(RequestMarshalState state) {
TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList);
return new ByteArrayOutputStream(requestMarshaler.getBinarySerializedSize());
return new TestOutputStream(requestMarshaler.getBinarySerializedSize());
}

@Benchmark
@Threads(1)
public ByteArrayOutputStream marshalCustom(RequestMarshalState state) throws IOException {
public TestOutputStream marshalCustom(RequestMarshalState state) throws IOException {
TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList);
ByteArrayOutputStream customOutput =
new ByteArrayOutputStream(requestMarshaler.getBinarySerializedSize());
TestOutputStream customOutput =
new TestOutputStream(requestMarshaler.getBinarySerializedSize());
requestMarshaler.writeBinaryTo(customOutput);
return customOutput;
}

@Benchmark
@Threads(1)
public ByteArrayOutputStream marshalJson(RequestMarshalState state) throws IOException {
public TestOutputStream marshalJson(RequestMarshalState state) throws IOException {
TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList);
ByteArrayOutputStream customOutput = new ByteArrayOutputStream();
TestOutputStream customOutput = new TestOutputStream();
requestMarshaler.writeJsonTo(customOutput);
return customOutput;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.internal.otlp;

import java.io.OutputStream;

class TestOutputStream extends OutputStream {
private final int size;
private int count;

TestOutputStream() {
this(-1);
}

TestOutputStream(int size) {
this.size = size;
}

@Override
public void write(int b) {
count++;
if (size > 0 && count > size) {
throw new IllegalStateException("max size exceeded");
}
}
}

0 comments on commit f9be682

Please sign in to comment.