~bigbes/sr-ht-ecore

ref: b36a927213562090e100c8b43ab27ef9094b1de9 sr-ht-ecore/middleware/middleware_bench_test.go -rw-r--r-- 3.5 KiB
b36a9272 — Eugene Blikh beads: ignore the JSONL exports a day ago
                                                                                
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package middleware

import (
	"io"
	"log/slog"
	"net/http"
	"testing"
)

// nullWriter is a ResponseWriter that keeps nothing: httptest.NewRecorder grows
// a buffer per request and this measurement is of the middleware.
type nullWriter struct{ header http.Header }

func (w *nullWriter) Header() http.Header         { return w.header }
func (w *nullWriter) Write(b []byte) (int, error) { return len(b), nil }
func (w *nullWriter) WriteHeader(int)             {}

// discardLog silences the default logger for one benchmark. The panic case
// below logs a stack per iteration, and without this the run would be measuring
// the terminal as much as the middleware — and would bury the result under it.
func discardLog(b *testing.B) {
	b.Helper()

	previous := slog.Default()
	slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{
		Level: slog.LevelDebug,
	})))
	b.Cleanup(func() { slog.SetDefault(previous) })
}

// benchRequest is the request every case below serves. chi and net/http do not
// mutate it — a router that needs a context puts the new one on a copy — so one
// value is safe to reuse across iterations, and building it per iteration would
// measure httptest.NewRequest.
func benchRequest(b *testing.B) *http.Request {
	b.Helper()

	r, err := http.NewRequest(http.MethodGet, "http://bench.example.org/tokens", nil)
	if err != nil {
		b.Fatalf("building the request: %v", err)
	}
	return r
}

// BenchmarkPrivateCache is two header writes in front of every response on the
// instance — the cheapest middleware here and the one on the most paths.
func BenchmarkPrivateCache(b *testing.B) {
	h := PrivateCache(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))
	r := benchRequest(b)

	b.ReportAllocs()
	for b.Loop() {
		h.ServeHTTP(&nullWriter{header: make(http.Header, 4)}, r)
	}
}

// BenchmarkRecoverPanics measures both halves of the guard.
//
// "clean" is what every request that does not panic pays: one wrapper
// allocation, one deferred recover. It is the number that matters, because it
// is charged to the whole surface for the benefit of the rare request below.
//
// "panicking" is the rendered 500, stack capture and log record included. It is
// slow on purpose — debug.Stack() walks the goroutine — and it is here so that
// a change which makes an already bad minute worse is visible.
func BenchmarkRecoverPanics(b *testing.B) {
	discardLog(b)

	r := benchRequest(b)

	b.Run("clean", func(b *testing.B) {
		h := RecoverPanics(renderInternal)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
			w.WriteHeader(http.StatusOK)
		}))

		b.ReportAllocs()
		for b.Loop() {
			h.ServeHTTP(&nullWriter{header: make(http.Header, 4)}, r)
		}
	})

	b.Run("panicking", func(b *testing.B) {
		h := RecoverPanics(renderInternal)(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
			panic("the store is not reachable")
		}))

		b.ReportAllocs()
		for b.Loop() {
			h.ServeHTTP(&nullWriter{header: make(http.Header, 4)}, r)
		}
	})
}

// BenchmarkChain is the two middlewares as a service installs them, one inside
// the other, so that the sum is measured rather than inferred from the two
// numbers above — the wrapper allocations compose and the header writes do not.
func BenchmarkChain(b *testing.B) {
	discardLog(b)

	h := PrivateCache(RecoverPanics(renderInternal)(
		http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
			w.WriteHeader(http.StatusOK)
		})))
	r := benchRequest(b)

	b.ReportAllocs()
	for b.Loop() {
		h.ServeHTTP(&nullWriter{header: make(http.Header, 4)}, r)
	}
}