1 // Copyright 2012, Google, Inc. All rights reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
14 // A few benchmarks for figuring out exactly how fast some underlying Go
17 type testError struct{}
19 func (t *testError) Error() string { return "abc" }
21 func BenchmarkTypeAssertion(b *testing.B) {
22 var e error = &testError{}
23 for i := 0; i < b.N; i++ {
28 func BenchmarkMapLookup(b *testing.B) {
29 m := map[LayerType]bool{
30 LayerTypePayload: true,
32 for i := 0; i < b.N; i++ {
33 _ = m[LayerTypePayload]
37 func BenchmarkNilMapLookup(b *testing.B) {
38 var m map[LayerType]bool
39 for i := 0; i < b.N; i++ {
40 _ = m[LayerTypePayload]
44 func BenchmarkNilMapLookupWithNilCheck(b *testing.B) {
45 var m map[LayerType]bool
46 for i := 0; i < b.N; i++ {
48 _ = m[LayerTypePayload]
53 func BenchmarkArrayLookup(b *testing.B) {
54 m := make([]bool, 100)
55 for i := 0; i < b.N; i++ {
56 _ = m[LayerTypePayload]
60 var testError1 = &testError{}
61 var testError2 error = testError1
63 func BenchmarkTypeToInterface1(b *testing.B) {
65 for i := 0; i < b.N; i++ {
68 // Have to do someting with 'e' or the compiler complains about an unused
72 func BenchmarkTypeToInterface2(b *testing.B) {
74 for i := 0; i < b.N; i++ {
77 // Have to do someting with 'e' or the compiler complains about an unused
82 var decodeOpts DecodeOptions
84 func decodeOptsByValue(_ DecodeOptions) {}
85 func decodeOptsByPointer(_ *DecodeOptions) {}
86 func BenchmarkPassDecodeOptionsByValue(b *testing.B) {
87 for i := 0; i < b.N; i++ {
88 decodeOptsByValue(decodeOpts)
91 func BenchmarkPassDecodeOptionsByPointer(b *testing.B) {
92 for i := 0; i < b.N; i++ {
93 decodeOptsByPointer(&decodeOpts)
97 func BenchmarkLockOSThread(b *testing.B) {
98 for i := 0; i < b.N; i++ {
99 runtime.LockOSThread()
102 func BenchmarkUnlockOSThread(b *testing.B) {
103 for i := 0; i < b.N; i++ {
104 runtime.UnlockOSThread()
108 runtime.LockOSThread()
109 runtime.UnlockOSThread()
111 func lockDeferUnlock() {
112 runtime.LockOSThread()
113 defer runtime.UnlockOSThread()
115 func BenchmarkLockUnlockOSThread(b *testing.B) {
116 for i := 0; i < b.N; i++ {
120 func BenchmarkLockDeferUnlockOSThread(b *testing.B) {
121 for i := 0; i < b.N; i++ {
126 func BenchmarkUnbufferedChannel(b *testing.B) {
127 ca := make(chan bool)
128 cb := make(chan bool)
136 for i := 0; i < b.N; i++ {
141 func BenchmarkSmallBufferedChannel(b *testing.B) {
142 ca := make(chan bool, 1)
143 cb := make(chan bool, 1)
151 for i := 0; i < b.N; i++ {
156 func BenchmarkLargeBufferedChannel(b *testing.B) {
157 ca := make(chan bool, 1000)
158 cb := make(chan bool, 1000)
166 for i := 0; i < b.N; i++ {
171 func BenchmarkEndpointFastHashShort(b *testing.B) {
172 e := Endpoint{typ: 1, len: 2}
173 for i := 0; i < b.N; i++ {
177 func BenchmarkEndpointFastHashLong(b *testing.B) {
178 e := Endpoint{typ: 1, len: 16}
179 for i := 0; i < b.N; i++ {
183 func BenchmarkFlowFastHashShort(b *testing.B) {
184 e := Flow{typ: 1, slen: 2, dlen: 2}
185 for i := 0; i < b.N; i++ {
189 func BenchmarkFlowFastHashLong(b *testing.B) {
190 e := Flow{typ: 1, slen: 16, dlen: 16}
191 for i := 0; i < b.N; i++ {