forked from lingrino/go-fault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
81 lines (64 loc) · 1.99 KB
/
benchmark_test.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package fault_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/github/go-fault"
)
// benchmarkRequest simulates a request with the provided Fault injected.
func benchmarkRequest(b *testing.B, f *fault.Fault) *httptest.ResponseRecorder {
b.Helper()
// benchmarkHandler is the main handler that runs on our request.
var benchmarkHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "OK", http.StatusOK)
})
// If we instead use httptest.NewRequest here our benchmark times will approximately double.
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
rr := httptest.NewRecorder()
if f != nil {
finalHandler := f.Handler(benchmarkHandler)
finalHandler.ServeHTTP(rr, req)
} else {
benchmarkHandler.ServeHTTP(rr, req)
}
return rr
}
// runBenchmark benchmarks the provided Fault.
func runBenchmark(b *testing.B, f *fault.Fault) {
var rr *httptest.ResponseRecorder
for n := 0; n < b.N; n++ {
rr = benchmarkRequest(b, f)
}
_ = rr
}
// BenchmarkNoFault is our control using no Fault.
func BenchmarkNoFault(b *testing.B) {
runBenchmark(b, nil)
}
// BenchmarkFaultDisabled benchmarks a disabled Fault.
func BenchmarkFaultDisabled(b *testing.B) {
i, _ := fault.NewErrorInjector(http.StatusInternalServerError)
f, _ := fault.NewFault(i,
fault.WithEnabled(false),
)
runBenchmark(b, f)
}
// BenchmarkFaultErrorZeroPercent benchmarks an enabled Fault with 0% participation.
func BenchmarkFaultErrorZeroPercent(b *testing.B) {
i, _ := fault.NewErrorInjector(http.StatusInternalServerError)
f, _ := fault.NewFault(i,
fault.WithEnabled(true),
fault.WithParticipation(0.0),
)
runBenchmark(b, f)
}
// BenchmarkFaultError100Percent benchmarks an enabled Fault with 100% participation.
func BenchmarkFaultError100Percent(b *testing.B) {
i, _ := fault.NewErrorInjector(http.StatusInternalServerError)
f, _ := fault.NewFault(i,
fault.WithEnabled(true),
fault.WithParticipation(1.0),
)
runBenchmark(b, f)
}