Files
godoxy-yusing/internal/net/gphttp/reverseproxy/reverse_proxy_benchmark_test.go
yusing f31b1b5ed3 refactor(misc): enhance performance on bytes pool, entrypoint, access log and route context handling
- Introduced benchmark tests for Entrypoint and ReverseProxy to evaluate performance.
- Updated Entrypoint's ServeHTTP method to improve route context management.
- Added new test file for entrypoint benchmarks and refined existing tests for route handling.
2025-09-14 00:03:27 +08:00

50 lines
1.1 KiB
Go

package reverseproxy
import (
"io"
"net/http"
"net/url"
"strings"
"testing"
nettypes "github.com/yusing/go-proxy/internal/net/types"
)
type noopTransport struct{}
func (t noopTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("Hello, world!")),
Request: req,
ContentLength: int64(len("Hello, world!")),
Header: http.Header{},
}, nil
}
type noopResponseWriter struct{}
func (w noopResponseWriter) Header() http.Header {
return http.Header{}
}
func (w noopResponseWriter) Write(b []byte) (int, error) {
return len(b), nil
}
func (w noopResponseWriter) WriteHeader(statusCode int) {
}
func BenchmarkReverseProxy(b *testing.B) {
var w noopResponseWriter
var req = http.Request{
Method: "GET",
URL: &url.URL{Scheme: "http", Host: "test"},
Body: io.NopCloser(strings.NewReader("Hello, world!")),
}
proxy := NewReverseProxy("test", nettypes.MustParseURL("http://localhost:8080"), noopTransport{})
for b.Loop() {
proxy.ServeHTTP(w, &req)
}
}