Skip to content

Commit

Permalink
add otelhttp Handler.ServeHTTP and Transport.RoundTrip benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed May 29, 2024
1 parent 1e8c7ca commit 3df618f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
41 changes: 41 additions & 0 deletions instrumentation/net/http/otelhttp/test/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -528,3 +529,43 @@ func TestWithRouteTag(t *testing.T) {
}
}
}

func BenchmarkHandlerServeHTTP(b *testing.B) {
tp := sdktrace.NewTracerProvider()
mp := sdkmetric.NewMeterProvider()

r, err := http.NewRequest(http.MethodGet, "http://localhost/", nil)
require.NoError(b, err)

for _, bb := range []struct {
name string
handler http.Handler
}{
{
name: "without the otelhttp handler",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
}),
},
{
name: "with the otelhttp handler",
handler: otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
}),
"test_handler",
otelhttp.WithTracerProvider(tp),
otelhttp.WithMeterProvider(mp),
),
},
} {
b.Run(bb.name, func(b *testing.B) {
rr := httptest.NewRecorder()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bb.handler.ServeHTTP(rr, r)
}
})
}
}
56 changes: 49 additions & 7 deletions instrumentation/net/http/otelhttp/test/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package test
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -16,14 +17,14 @@ import (
"strings"
"testing"

"go.opentelemetry.io/otel/sdk/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"

Expand Down Expand Up @@ -244,8 +245,8 @@ func TestTransportMetrics(t *testing.T) {
responseBody := []byte("Hello, world!")

t.Run("make http request and read entire response at once", func(t *testing.T) {
reader := metric.NewManualReader()
meterProvider := metric.NewMeterProvider(metric.WithReader(reader))
reader := sdkmetric.NewManualReader()
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -302,8 +303,8 @@ func TestTransportMetrics(t *testing.T) {
})

t.Run("make http request and buffer response", func(t *testing.T) {
reader := metric.NewManualReader()
meterProvider := metric.NewMeterProvider(metric.WithReader(reader))
reader := sdkmetric.NewManualReader()
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -370,8 +371,8 @@ func TestTransportMetrics(t *testing.T) {
})

t.Run("make http request and close body before reading completely", func(t *testing.T) {
reader := metric.NewManualReader()
meterProvider := metric.NewMeterProvider(metric.WithReader(reader))
reader := sdkmetric.NewManualReader()
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -476,3 +477,44 @@ func assertClientScopeMetrics(t *testing.T, sm metricdata.ScopeMetrics, attrs at
}
metricdatatest.AssertEqual(t, want, sm.Metrics[2], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
}

func BenchmarkTransportRoundTrip(b *testing.B) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
}))
defer ts.Close()

tp := sdktrace.NewTracerProvider()
mp := sdkmetric.NewMeterProvider()

r, err := http.NewRequest(http.MethodGet, ts.URL, nil)
require.NoError(b, err)

for _, bb := range []struct {
name string
transport http.RoundTripper
}{
{
name: "without the otelhttp transport",
transport: http.DefaultTransport,
},
{
name: "with the otelhttp transport",
transport: otelhttp.NewTransport(
http.DefaultTransport,
otelhttp.WithTracerProvider(tp),
otelhttp.WithMeterProvider(mp),
),
},
} {
b.Run(bb.name, func(b *testing.B) {
c := http.Client{Transport: bb.transport}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Do(r)
}
})
}
}

0 comments on commit 3df618f

Please sign in to comment.