File: bench_test.go

package info (click to toggle)
golang-github-zenazn-goji 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 464 kB
  • sloc: makefile: 3
file content (166 lines) | stat: -rw-r--r-- 3,292 bytes parent folder | download | duplicates (3)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// +build go1.3

package web

import (
	"crypto/rand"
	"encoding/base64"
	mrand "math/rand"
	"net/http"
	"testing"
)

/*
The core benchmarks here are based on cypriss's mux benchmarks, which can be
found here:
https://github.com/cypriss/golang-mux-benchmark

They happen to play very well into Goji's router's strengths.
*/

type nilRouter struct{}

var helloWorld = []byte("Hello world!\n")

func (_ nilRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Write(helloWorld)
}

type nilResponse struct{}

func (_ nilResponse) Write(buf []byte) (int, error) {
	return len(buf), nil
}
func (_ nilResponse) Header() http.Header {
	return nil
}
func (_ nilResponse) WriteHeader(code int) {
}

func trivialMiddleware(h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}

var w nilResponse

func addRoutes(m *Mux, prefix string) {
	m.Get(prefix, nilRouter{})
	m.Post(prefix, nilRouter{})
	m.Get(prefix+"/:id", nilRouter{})
	m.Put(prefix+"/:id", nilRouter{})
	m.Delete(prefix+"/:id", nilRouter{})
}

func randString() string {
	var buf [6]byte
	rand.Reader.Read(buf[:])
	return base64.URLEncoding.EncodeToString(buf[:])
}

func genPrefixes(n int) []string {
	p := make([]string, n)
	for i := range p {
		p[i] = "/" + randString()
	}
	return p
}

func genRequests(prefixes []string) []*http.Request {
	rs := make([]*http.Request, 5*len(prefixes))
	for i, prefix := range prefixes {
		rs[5*i+0], _ = http.NewRequest("GET", prefix, nil)
		rs[5*i+1], _ = http.NewRequest("POST", prefix, nil)
		rs[5*i+2], _ = http.NewRequest("GET", prefix+"/foo", nil)
		rs[5*i+3], _ = http.NewRequest("PUT", prefix+"/foo", nil)
		rs[5*i+4], _ = http.NewRequest("DELETE", prefix+"/foo", nil)
	}
	return rs
}

func permuteRequests(reqs []*http.Request) []*http.Request {
	out := make([]*http.Request, len(reqs))
	perm := mrand.Perm(len(reqs))
	for i, req := range reqs {
		out[perm[i]] = req
	}
	return out
}

func benchN(b *testing.B, n int) {
	m := New()
	prefixes := genPrefixes(n)
	for _, prefix := range prefixes {
		addRoutes(m, prefix)
	}
	m.Compile()
	reqs := permuteRequests(genRequests(prefixes))

	b.ResetTimer()
	b.ReportAllocs()
	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			i++
			m.ServeHTTP(w, reqs[i%len(reqs)])
		}
	})
}

func benchM(b *testing.B, n int) {
	m := New()
	m.Get("/", nilRouter{})
	for i := 0; i < n; i++ {
		m.Use(trivialMiddleware)
	}
	r, _ := http.NewRequest("GET", "/", nil)
	m.Compile()

	b.ResetTimer()
	b.ReportAllocs()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			m.ServeHTTP(w, r)
		}
	})
}

func BenchmarkStatic(b *testing.B) {
	m := New()
	m.Get("/", nilRouter{})
	r, _ := http.NewRequest("GET", "/", nil)
	m.Compile()

	b.ResetTimer()
	b.ReportAllocs()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			m.ServeHTTP(w, r)
		}
	})
}

func BenchmarkRoute5(b *testing.B) {
	benchN(b, 1)
}
func BenchmarkRoute50(b *testing.B) {
	benchN(b, 10)
}
func BenchmarkRoute500(b *testing.B) {
	benchN(b, 100)
}
func BenchmarkRoute5000(b *testing.B) {
	benchN(b, 1000)
}

func BenchmarkMiddleware1(b *testing.B) {
	benchM(b, 1)
}
func BenchmarkMiddleware10(b *testing.B) {
	benchM(b, 10)
}
func BenchmarkMiddleware100(b *testing.B) {
	benchM(b, 100)
}