github.com/gopacket/gopacket@v1.1.0/benchmark_test.go (about) 1 // Copyright 2012, Google, Inc. All rights reserved. 2 // 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 5 // tree. 6 7 package gopacket 8 9 import ( 10 "runtime" 11 "testing" 12 ) 13 14 // A few benchmarks for figuring out exactly how fast some underlying Go 15 // things are. 16 17 type testError struct{} 18 19 func (t *testError) Error() string { return "abc" } 20 21 func BenchmarkTypeAssertion(b *testing.B) { 22 var e error = &testError{} 23 for i := 0; i < b.N; i++ { 24 _, _ = e.(*testError) 25 } 26 } 27 28 func BenchmarkMapLookup(b *testing.B) { 29 m := map[LayerType]bool{ 30 LayerTypePayload: true, 31 } 32 for i := 0; i < b.N; i++ { 33 _ = m[LayerTypePayload] 34 } 35 } 36 37 func BenchmarkNilMapLookup(b *testing.B) { 38 var m map[LayerType]bool 39 for i := 0; i < b.N; i++ { 40 _ = m[LayerTypePayload] 41 } 42 } 43 44 func BenchmarkNilMapLookupWithNilCheck(b *testing.B) { 45 var m map[LayerType]bool 46 for i := 0; i < b.N; i++ { 47 if m != nil { 48 _ = m[LayerTypePayload] 49 } 50 } 51 } 52 53 func BenchmarkArrayLookup(b *testing.B) { 54 m := make([]bool, 100) 55 for i := 0; i < b.N; i++ { 56 _ = m[LayerTypePayload] 57 } 58 } 59 60 var testError1 = &testError{} 61 var testError2 error = testError1 62 63 func BenchmarkTypeToInterface1(b *testing.B) { 64 var e error 65 for i := 0; i < b.N; i++ { 66 e = testError1 67 } 68 // Have to do someting with 'e' or the compiler complains about an unused 69 // variable. 70 testError2 = e 71 } 72 func BenchmarkTypeToInterface2(b *testing.B) { 73 var e error 74 for i := 0; i < b.N; i++ { 75 e = testError2 76 } 77 // Have to do someting with 'e' or the compiler complains about an unused 78 // variable. 79 testError2 = e 80 } 81 82 var decodeOpts DecodeOptions 83 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) 89 } 90 } 91 func BenchmarkPassDecodeOptionsByPointer(b *testing.B) { 92 for i := 0; i < b.N; i++ { 93 decodeOptsByPointer(&decodeOpts) 94 } 95 } 96 97 func BenchmarkLockOSThread(b *testing.B) { 98 for i := 0; i < b.N; i++ { 99 runtime.LockOSThread() 100 } 101 } 102 func BenchmarkUnlockOSThread(b *testing.B) { 103 for i := 0; i < b.N; i++ { 104 runtime.UnlockOSThread() 105 } 106 } 107 func lockUnlock() { 108 runtime.LockOSThread() 109 runtime.UnlockOSThread() 110 } 111 func lockDeferUnlock() { 112 runtime.LockOSThread() 113 defer runtime.UnlockOSThread() 114 } 115 func BenchmarkLockUnlockOSThread(b *testing.B) { 116 for i := 0; i < b.N; i++ { 117 lockUnlock() 118 } 119 } 120 func BenchmarkLockDeferUnlockOSThread(b *testing.B) { 121 for i := 0; i < b.N; i++ { 122 lockDeferUnlock() 123 } 124 } 125 126 func BenchmarkUnbufferedChannel(b *testing.B) { 127 ca := make(chan bool) 128 cb := make(chan bool) 129 defer close(ca) 130 go func() { 131 defer close(cb) 132 for range ca { 133 cb <- true 134 } 135 }() 136 for i := 0; i < b.N; i++ { 137 ca <- true 138 <-cb 139 } 140 } 141 func BenchmarkSmallBufferedChannel(b *testing.B) { 142 ca := make(chan bool, 1) 143 cb := make(chan bool, 1) 144 defer close(ca) 145 go func() { 146 defer close(cb) 147 for range ca { 148 cb <- true 149 } 150 }() 151 for i := 0; i < b.N; i++ { 152 ca <- true 153 <-cb 154 } 155 } 156 func BenchmarkLargeBufferedChannel(b *testing.B) { 157 ca := make(chan bool, 1000) 158 cb := make(chan bool, 1000) 159 defer close(ca) 160 go func() { 161 defer close(cb) 162 for range ca { 163 cb <- true 164 } 165 }() 166 for i := 0; i < b.N; i++ { 167 ca <- true 168 <-cb 169 } 170 } 171 func BenchmarkEndpointFastHashShort(b *testing.B) { 172 e := Endpoint{typ: 1, len: 2} 173 for i := 0; i < b.N; i++ { 174 e.FastHash() 175 } 176 } 177 func BenchmarkEndpointFastHashLong(b *testing.B) { 178 e := Endpoint{typ: 1, len: 16} 179 for i := 0; i < b.N; i++ { 180 e.FastHash() 181 } 182 } 183 func BenchmarkFlowFastHashShort(b *testing.B) { 184 e := Flow{typ: 1, slen: 2, dlen: 2} 185 for i := 0; i < b.N; i++ { 186 e.FastHash() 187 } 188 } 189 func BenchmarkFlowFastHashLong(b *testing.B) { 190 e := Flow{typ: 1, slen: 16, dlen: 16} 191 for i := 0; i < b.N; i++ { 192 e.FastHash() 193 } 194 }