forked from google/gopacket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
194 lines (178 loc) · 3.73 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2012, Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package gopacket
import (
"runtime"
"testing"
)
// A few benchmarks for figuring out exactly how fast some underlying Go
// things are.
type testError struct{}
func (t *testError) Error() string { return "abc" }
func BenchmarkTypeAssertion(b *testing.B) {
var e error = &testError{}
for i := 0; i < b.N; i++ {
_, _ = e.(*testError)
}
}
func BenchmarkMapLookup(b *testing.B) {
m := map[LayerType]bool{
LayerTypePayload: true,
}
for i := 0; i < b.N; i++ {
_ = m[LayerTypePayload]
}
}
func BenchmarkNilMapLookup(b *testing.B) {
var m map[LayerType]bool
for i := 0; i < b.N; i++ {
_ = m[LayerTypePayload]
}
}
func BenchmarkNilMapLookupWithNilCheck(b *testing.B) {
var m map[LayerType]bool
for i := 0; i < b.N; i++ {
if m != nil {
_ = m[LayerTypePayload]
}
}
}
func BenchmarkArrayLookup(b *testing.B) {
m := make([]bool, 100)
for i := 0; i < b.N; i++ {
_ = m[LayerTypePayload]
}
}
var testError1 = &testError{}
var testError2 error = testError1
func BenchmarkTypeToInterface1(b *testing.B) {
var e error
for i := 0; i < b.N; i++ {
e = testError1
}
// Have to do someting with 'e' or the compiler complains about an unused
// variable.
testError2 = e
}
func BenchmarkTypeToInterface2(b *testing.B) {
var e error
for i := 0; i < b.N; i++ {
e = testError2
}
// Have to do someting with 'e' or the compiler complains about an unused
// variable.
testError2 = e
}
var decodeOpts DecodeOptions
func decodeOptsByValue(_ DecodeOptions) {}
func decodeOptsByPointer(_ *DecodeOptions) {}
func BenchmarkPassDecodeOptionsByValue(b *testing.B) {
for i := 0; i < b.N; i++ {
decodeOptsByValue(decodeOpts)
}
}
func BenchmarkPassDecodeOptionsByPointer(b *testing.B) {
for i := 0; i < b.N; i++ {
decodeOptsByPointer(&decodeOpts)
}
}
func BenchmarkLockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
runtime.LockOSThread()
}
}
func BenchmarkUnlockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
runtime.UnlockOSThread()
}
}
func lockUnlock() {
runtime.LockOSThread()
runtime.UnlockOSThread()
}
func lockDeferUnlock() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
func BenchmarkLockUnlockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
lockUnlock()
}
}
func BenchmarkLockDeferUnlockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
lockDeferUnlock()
}
}
func BenchmarkUnbufferedChannel(b *testing.B) {
ca := make(chan bool)
cb := make(chan bool)
defer close(ca)
go func() {
defer close(cb)
for _ = range ca {
cb <- true
}
}()
for i := 0; i < b.N; i++ {
ca <- true
<-cb
}
}
func BenchmarkSmallBufferedChannel(b *testing.B) {
ca := make(chan bool, 1)
cb := make(chan bool, 1)
defer close(ca)
go func() {
defer close(cb)
for _ = range ca {
cb <- true
}
}()
for i := 0; i < b.N; i++ {
ca <- true
<-cb
}
}
func BenchmarkLargeBufferedChannel(b *testing.B) {
ca := make(chan bool, 1000)
cb := make(chan bool, 1000)
defer close(ca)
go func() {
defer close(cb)
for _ = range ca {
cb <- true
}
}()
for i := 0; i < b.N; i++ {
ca <- true
<-cb
}
}
func BenchmarkEndpointFastHashShort(b *testing.B) {
e := Endpoint{typ: 1, len: 2}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}
func BenchmarkEndpointFastHashLong(b *testing.B) {
e := Endpoint{typ: 1, len: 16}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}
func BenchmarkFlowFastHashShort(b *testing.B) {
e := Flow{typ: 1, slen: 2, dlen: 2}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}
func BenchmarkFlowFastHashLong(b *testing.B) {
e := Flow{typ: 1, slen: 16, dlen: 16}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}