starnet/dynamic_transport_benchmark_test.go

145 lines
3.2 KiB
Go
Raw Normal View History

package starnet
import (
"crypto/tls"
"net/http"
"net/url"
"testing"
)
func BenchmarkDynamicTransportCustomIP(b *testing.B) {
server := newIPv4Server(b, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}))
defer server.Close()
targetURL := benchmarkTargetURL(b, server.URL, "bench-custom-ip.test")
client := NewClientNoErr()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
resp, err := client.Get(targetURL, WithCustomIP([]string{"127.0.0.1"}))
if err != nil {
b.Fatalf("Get() error: %v", err)
}
_, _ = resp.Body().Bytes()
resp.Close()
}
}
func BenchmarkDynamicTransportProxyTLSCacheable(b *testing.B) {
server := newIPv4TLSServer(b, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}))
defer server.Close()
proxy := newIPv4ConnectProxyServer(b, nil)
defer proxy.Close()
targetURL := httpsURLForHost(b, server, "bench-proxy-cacheable.test")
client := NewClientNoErr()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
resp, err := client.Get(targetURL,
WithProxy(proxy.URL),
WithCustomIP([]string{"127.0.0.1"}),
WithSkipTLSVerify(true),
)
if err != nil {
b.Fatalf("Get() error: %v", err)
}
_, _ = resp.Body().Bytes()
resp.Close()
}
}
func BenchmarkDynamicTransportCustomIPTLSCacheable(b *testing.B) {
server := newIPv4TLSServer(b, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}))
defer server.Close()
targetURL := httpsURLForHost(b, server, "bench-custom-ip-cacheable.test")
client := NewClientNoErr()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
resp, err := client.Get(targetURL,
WithCustomIP([]string{"127.0.0.1"}),
WithSkipTLSVerify(true),
)
if err != nil {
b.Fatalf("Get() error: %v", err)
}
_, _ = resp.Body().Bytes()
resp.Close()
}
}
func BenchmarkDynamicTransportCustomIPUserTLSConfig(b *testing.B) {
server := newIPv4TLSServer(b, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}))
defer server.Close()
targetURL := httpsURLForHost(b, server, "bench-user-tls.test")
client := NewClientNoErr()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
resp, err := client.Get(targetURL,
WithCustomIP([]string{"127.0.0.1"}),
WithTLSConfig(&tls.Config{InsecureSkipVerify: true}),
)
if err != nil {
b.Fatalf("Get() error: %v", err)
}
_, _ = resp.Body().Bytes()
resp.Close()
}
}
func benchmarkTargetURL(tb testing.TB, rawURL, host string) string {
tb.Helper()
parsed, err := url.Parse(rawURL)
if err != nil {
tb.Fatalf("url.Parse() error: %v", err)
}
port := parsed.Port()
if port == "" {
switch parsed.Scheme {
case "https":
port = "443"
default:
port = "80"
}
}
return parsed.Scheme + "://" + host + ":" + port + pathWithQuery(parsed.Path, parsed.RawQuery)
}
func pathWithQuery(path, rawQuery string) string {
if path == "" {
path = "/"
}
if rawQuery == "" {
return path
}
return path + "?" + rawQuery
}