mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-31 02:55:17 -04:00
Proxy performance (#946)
* proxy: add benchmark Signed-off-by: Tw <tw19881113@gmail.com> * replacer: prepare lazily update issue#939 benchmark old ns/op new ns/op delta BenchmarkProxy-4 83865 72544 -13.50% Signed-off-by: Tw <tw19881113@gmail.com> * proxy: use buffer pool to avoid temporary allocation Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
@@ -761,3 +761,37 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) error { return nil }
|
||||
func (c *fakeConn) Close() error { return nil }
|
||||
func (c *fakeConn) Read(b []byte) (int, error) { return c.readBuf.Read(b) }
|
||||
func (c *fakeConn) Write(b []byte) (int, error) { return c.writeBuf.Write(b) }
|
||||
|
||||
func BenchmarkProxy(b *testing.B) {
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Hello, client"))
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
upstream := newFakeUpstream(backend.URL, false)
|
||||
upstream.host.UpstreamHeaders = http.Header{
|
||||
"Hostname": {"{hostname}"},
|
||||
"Host": {"{host}"},
|
||||
"X-Real-IP": {"{remote}"},
|
||||
"X-Forwarded-Proto": {"{scheme}"},
|
||||
}
|
||||
// set up proxy
|
||||
p := &Proxy{
|
||||
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
|
||||
Upstreams: []Upstream{upstream},
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
b.StopTimer()
|
||||
// create request and response recorder
|
||||
r, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
b.StartTimer()
|
||||
p.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user