forked from ttacon/uri
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
147 lines (134 loc) · 3.6 KB
/
benchmark_test.go
File metadata and controls
147 lines (134 loc) · 3.6 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
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
package uri
import (
"fmt"
"io"
"net/url"
"testing"
)
type payload struct {
name string
comment string
tests []string
}
func payloads() []payload {
return []payload{
{
name: "simple payload",
comment: "a payload with vanilla URIs, no IPs, no escaped content",
tests: []string{
"foo://example.com:8042/over/there?name=ferret#nose",
"mailto://user@domain.com",
"ssh://user@git.openstack.org:29418/openstack/keystone.git",
"https://willo.io/#yolo",
},
},
{
name: "mixed payload",
comment: "a payload with vanilla URIs, with 20% containing escaped content",
tests: []string{
"foo://example.com:8042/over/there?name=ferret#nose",
"mailto://user@domain.com",
"ssh://user@git.openstack.org:29418/openstack/keystone.git",
"https://willo.io/#yolo",
"http://httpbin.org/get?utf8=%e2%98%83",
},
},
{
name: "payload with IPs",
comment: "a payload with ~ 30% with an IP host specification (v4 & v6)",
tests: []string{
"foo://example.com:8042/over/there?name=ferret#nose",
"http://httpbin.org/get?utf8=%e2%98%83",
"mailto://user@domain.com",
"ssh://user@git.openstack.org:29418/openstack/keystone.git",
"https://willo.io/#yolo",
"https://user:passwd@[FF02:30:0:0:0:0:0:5%25en1]:8080/a?query=value#fragment",
"https://user:passwd@127.0.0.1:8080/a?query=value#fragment",
},
},
}
}
func Benchmark_Parse(b *testing.B) {
// URI.Parse() and net/url.URL.Parse() side by side on different payloads
for _, payload := range payloads() {
b.Run("with URI "+payload.name, benchParseWithPayload(payload.tests))
b.Run("with URL "+payload.name, benchParseURLStdLib(payload.tests))
}
}
func benchParseWithPayload(payload []string) func(*testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var v URI
for i := 0; i < b.N; i++ {
v, _ = Parse(payload[i%len(payload)])
}
fmt.Fprintln(io.Discard, v)
}
}
func benchParseURLStdLib(payload []string) func(*testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var u *url.URL
for i := 0; i < b.N; i++ {
u, _ = url.Parse(payload[i%len(payload)])
}
fmt.Fprintln(io.Discard, u)
}
}
func Benchmark_String(b *testing.B) {
var ip ipType
tests := []*uri{
{
"foo", "//example.com:8042/over/there", "name=ferret", "nose",
authorityInfo{ip, "//", "", "example.com", "8042", "/over/there", nil},
nil,
},
{
"http", "//httpbin.org/get", "utf8=\xe2\x98\x83", "",
authorityInfo{ip, "//", "", "httpbin.org", "", "/get", nil},
nil,
},
{
"mailto", "user@domain.com", "", "",
authorityInfo{ip, "//", "user", "domain.com", "", "", nil},
nil,
},
{
"ssh", "//user@git.openstack.org:29418/openstack/keystone.git", "", "",
authorityInfo{ip, "//", "user", "git.openstack.org", "29418", "/openstack/keystone.git", nil},
nil,
},
{
"https", "//willo.io/", "", "yolo",
authorityInfo{ip, "//", "", "willo.io", "", "/", nil},
nil,
},
}
b.ReportAllocs()
b.ResetTimer()
var s string
for i := 0; i < b.N; i++ {
s = tests[i%5].String()
}
fmt.Fprintln(io.Discard, s)
}
func Benchmark_DNSSchemes(b *testing.B) {
var found bool
schemes := schemesWithDNS()
for i := 0; i < 100; i++ {
schemes = append(schemes, "nowhere")
}
// NOTE: at this moment, an attempt to use a map[uint32]struct{] jointly with a hash
// performs about 3-4x slower (FNV1a, CRC) than the plan switch.
b.Run("with switch", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
scheme := schemes[i%len(schemes)]
found = UsesDNSHostValidation(scheme)
}
fmt.Fprintln(io.Discard, found)
})
}