-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
route_test.go
66 lines (52 loc) 路 1.35 KB
/
route_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
package mux
import (
"net/http"
"regexp"
"sync"
"testing"
)
var testNewRouterMu sync.Mutex
var testHandler = http.NotFoundHandler()
func BenchmarkNewRouter(b *testing.B) {
testNewRouterMu.Lock()
defer testNewRouterMu.Unlock()
// Set the RegexpCompileFunc to the default regexp.Compile.
RegexpCompileFunc = regexp.Compile
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testNewRouter(b, testHandler)
}
}
func BenchmarkNewRouterRegexpFunc(b *testing.B) {
testNewRouterMu.Lock()
defer testNewRouterMu.Unlock()
// We preallocate the size to 8.
cache := make(map[string]*regexp.Regexp, 8)
// Override the RegexpCompileFunc to reuse compiled expressions
// from the `cache` map. Real world caches should have eviction
// policies or some sort of approach to limit memory use.
RegexpCompileFunc = func(expr string) (*regexp.Regexp, error) {
if regex, ok := cache[expr]; ok {
return regex, nil
}
regex, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
cache[expr] = regex
return regex, nil
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testNewRouter(b, testHandler)
}
}
func testNewRouter(_ testing.TB, handler http.Handler) {
r := NewRouter()
// A route with a route variable:
r.Handle("/metrics/{type}", handler)
r.Queries("orgID", "{orgID:[0-9]*?}")
r.Host("{subdomain}.domain.com")
}