-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstatus_recorder_bench_test.go
More file actions
54 lines (48 loc) · 1.29 KB
/
status_recorder_bench_test.go
File metadata and controls
54 lines (48 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package core
import (
"net/http"
"sync"
"testing"
)
// noopResponseWriter is a minimal ResponseWriter that discards output,
// isolating statusRecorder overhead from buffer growth.
type noopResponseWriter struct{}
func (noopResponseWriter) Header() http.Header { return http.Header{} }
func (noopResponseWriter) Write(b []byte) (int, error) { return len(b), nil }
func (noopResponseWriter) WriteHeader(int) {}
// sink prevents compiler from optimizing away allocations.
var sink http.ResponseWriter
func BenchmarkStatusRecorder_Direct(b *testing.B) {
w := noopResponseWriter{}
body := []byte("hello")
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
sink = rec // force heap escape
rec.WriteHeader(http.StatusOK)
rec.Write(body)
}
}
func BenchmarkStatusRecorder_Pool(b *testing.B) {
pool := sync.Pool{
New: func() any {
return &statusRecorder{}
},
}
w := noopResponseWriter{}
body := []byte("hello")
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
rec := pool.Get().(*statusRecorder)
rec.ResponseWriter = w
rec.status = http.StatusOK
rec.wroteHeader = false
sink = rec // force heap escape
rec.WriteHeader(http.StatusOK)
rec.Write(body)
rec.ResponseWriter = nil
pool.Put(rec)
}
}