Created
October 29, 2020 06:58
-
-
Save markusthoemmes/7c59e98a59abe64763757a0b8c1f796c to your computer and use it in GitHub Desktop.
TCP vs Unix connection with no keepalive benchmark
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
func BenchmarkConnection(b *testing.B) { | |
tests := []struct { | |
proto string | |
addr string | |
}{{ | |
proto: "unix", | |
addr: unixSocketPath, | |
}, { | |
proto: "tcp", | |
addr: ":8888", | |
}} | |
for _, test := range tests { | |
b.Run(test.proto, func(b *testing.B) { | |
l, err := net.Listen(test.proto, test.addr) | |
if err != nil { | |
b.Fatal(err) | |
} | |
defer l.Close() | |
go func() { | |
http.Serve(l, http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {})) | |
}() | |
transport := http.DefaultTransport.(*http.Transport).Clone() | |
transport.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) { | |
return net.Dial(test.proto, test.addr) | |
} | |
transport.DisableKeepAlives = true | |
httpClient := &http.Client{ | |
Transport: transport, | |
} | |
for i := 0; i < b.N; i++ { | |
res, err := httpClient.Get("http://bla") | |
if err != nil { | |
b.Fatal(err) | |
} | |
defer res.Body.Close() | |
if res.StatusCode != http.StatusOK { | |
b.Fatal(res.StatusCode) | |
} | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment