Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Mar 29, 2022
1 parent add3652 commit 777d34a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
In case of type mismatch, they don't panic right away but return an invalid zero-initialized
instance for consistency with other OneOf field accessors (#5034)
- Update OTLP to v0.15.0 (#5064)
- OTLP HTTP receiver will use HTTP/2 over TLS if client supports it (#5190)

### 🧰 Bug fixes 🧰

Expand Down
112 changes: 109 additions & 3 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TestHttpReception(t *testing.T) {
tlsServerCreds *configtls.TLSServerSetting
tlsClientCreds *configtls.TLSClientSetting
hasError bool
forceHttp1 bool
forceHTTP1 bool
}{
{
name: "noTLS",
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestHttpReception(t *testing.T) {
},
ServerName: "localhost",
},
forceHttp1: true,
forceHTTP1: true,
},
{
name: "NoServerCertificates",
Expand Down Expand Up @@ -533,7 +533,7 @@ func TestHttpReception(t *testing.T) {
Endpoint: prefix + ln.Addr().String(),
TLSSetting: *tt.tlsClientCreds,
}
if tt.forceHttp1 {
if tt.forceHTTP1 {
expectedProto = "HTTP/1.1"
hcs.CustomRoundTripper = func(rt http.RoundTripper) (http.RoundTripper, error) {
rt.(*http.Transport).ForceAttemptHTTP2 = false
Expand Down Expand Up @@ -947,3 +947,109 @@ type mockHost struct {
func (nh *mockHost) GetExtensions() map[config.ComponentID]component.Extension {
return nh.ext
}

func BenchmarkHttpRequest(b *testing.B) {
tests := []struct {
name string
forceHTTP1 bool
clientPerThread bool
}{
{
name: "HTTP/2.0, shared client (like load balancer)",
forceHTTP1: false,
clientPerThread: false,
},
{
name: "HTTP/1.1, shared client (like load balancer)",
forceHTTP1: true,
clientPerThread: false,
},
{
name: "HTTP/2.0, client per thread (like single app)",
forceHTTP1: false,
clientPerThread: true,
},
{
name: "HTTP/1.1, client per thread (like single app)",
forceHTTP1: true,
clientPerThread: true,
},
}

tlsServerCreds := &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CAFile: filepath.Join("testdata", "ca.crt"),
CertFile: filepath.Join("testdata", "server.crt"),
KeyFile: filepath.Join("testdata", "server.key"),
},
}
tlsClientCreds := &configtls.TLSClientSetting{
TLSSetting: configtls.TLSSetting{
CAFile: filepath.Join("testdata", "ca.crt"),
},
ServerName: "localhost",
}

hss := &HTTPServerSettings{
Endpoint: "localhost:0",
TLSSetting: tlsServerCreds,
}

s, err := hss.ToServer(
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, errWrite := fmt.Fprint(w, "test")
require.NoError(b, errWrite)
}))
require.NoError(b, err)
ln, err := hss.ToListener()
require.NoError(b, err)

go func() {
_ = s.Serve(ln)
}()
defer func() {
_ = s.Close()
}()

// Wait for the servers to start
<-time.After(10 * time.Millisecond)

for _, bb := range tests {
hcs := &HTTPClientSettings{
Endpoint: "https://" + ln.Addr().String(),
TLSSetting: *tlsClientCreds,
}
if bb.forceHTTP1 {
hcs.CustomRoundTripper = func(rt http.RoundTripper) (http.RoundTripper, error) {
rt.(*http.Transport).ForceAttemptHTTP2 = false
return rt, nil
}
}
b.Run(bb.name, func(b *testing.B) {
var c *http.Client
if !bb.clientPerThread {
c, err = hcs.ToClient(map[config.ComponentID]component.Extension{}, component.TelemetrySettings{})
require.NoError(b, err)
}
b.RunParallel(func(pb *testing.PB) {
if c == nil {
c, err = hcs.ToClient(map[config.ComponentID]component.Extension{}, component.TelemetrySettings{})
require.NoError(b, err)
}
for pb.Next() {
resp, errResp := c.Get(hcs.Endpoint)
require.NoError(b, errResp)
body, errRead := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
require.NoError(b, errRead)
require.Equal(b, "test", string(body))
}
c.CloseIdleConnections()
})
// Wait for connections to close before closing server to prevent log spam
<-time.After(10 * time.Millisecond)
})
}
}

0 comments on commit 777d34a

Please sign in to comment.