gitee.com/quant1x/gox@v1.21.2/encoding/binary/struc/bench_test.go (about) 1 package struc 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "testing" 7 ) 8 9 type BenchExample struct { 10 Test [5]byte 11 A int32 12 B, C, D int16 13 Test2 [4]byte 14 Length int32 15 } 16 17 func BenchmarkArrayEncode(b *testing.B) { 18 for i := 0; i < b.N; i++ { 19 var buf bytes.Buffer 20 if err := Pack(&buf, arrayReference); err != nil { 21 b.Fatal(err) 22 } 23 } 24 } 25 26 func BenchmarkSliceEncode(b *testing.B) { 27 for i := 0; i < b.N; i++ { 28 var buf bytes.Buffer 29 if err := Pack(&buf, sliceReference); err != nil { 30 b.Fatal(err) 31 } 32 } 33 } 34 35 func BenchmarkArrayDecode(b *testing.B) { 36 var out ExampleArray 37 for i := 0; i < b.N; i++ { 38 buf := bytes.NewBuffer(arraySliceReferenceBytes) 39 if err := Unpack(buf, &out); err != nil { 40 b.Fatal(err) 41 } 42 } 43 } 44 45 func BenchmarkSliceDecode(b *testing.B) { 46 var out ExampleSlice 47 for i := 0; i < b.N; i++ { 48 buf := bytes.NewBuffer(arraySliceReferenceBytes) 49 if err := Unpack(buf, &out); err != nil { 50 b.Fatal(err) 51 } 52 } 53 } 54 55 type BenchStrucExample struct { 56 Test [5]byte `struc:"[5]byte"` 57 A int `struc:"int32"` 58 B, C, D int `struc:"int16"` 59 Test2 [4]byte `struc:"[4]byte"` 60 Length int `struc:"int32,sizeof=Data"` 61 Data []byte 62 } 63 64 var benchRef = &BenchExample{ 65 [5]byte{1, 2, 3, 4, 5}, 66 1, 2, 3, 4, 67 [4]byte{1, 2, 3, 4}, 68 8, 69 } 70 71 var eightBytes = []byte("8bytestr") 72 73 var benchStrucRef = &BenchStrucExample{ 74 [5]byte{1, 2, 3, 4, 5}, 75 1, 2, 3, 4, 76 [4]byte{1, 2, 3, 4}, 77 8, eightBytes, 78 } 79 80 func BenchmarkEncode(b *testing.B) { 81 for i := 0; i < b.N; i++ { 82 var buf bytes.Buffer 83 err := Pack(&buf, benchStrucRef) 84 if err != nil { 85 b.Fatal(err) 86 } 87 } 88 } 89 90 func BenchmarkStdlibEncode(b *testing.B) { 91 for i := 0; i < b.N; i++ { 92 var buf bytes.Buffer 93 err := binary.Write(&buf, binary.BigEndian, benchRef) 94 if err != nil { 95 b.Fatal(err) 96 } 97 _, err = buf.Write(eightBytes) 98 if err != nil { 99 b.Fatal(err) 100 } 101 } 102 } 103 104 func BenchmarkManualEncode(b *testing.B) { 105 order := binary.BigEndian 106 s := benchStrucRef 107 for i := 0; i < b.N; i++ { 108 var buf bytes.Buffer 109 tmp := make([]byte, 29) 110 copy(tmp[0:5], s.Test[:]) 111 order.PutUint32(tmp[5:9], uint32(s.A)) 112 order.PutUint16(tmp[9:11], uint16(s.B)) 113 order.PutUint16(tmp[11:13], uint16(s.C)) 114 order.PutUint16(tmp[13:15], uint16(s.D)) 115 copy(tmp[15:19], s.Test2[:]) 116 order.PutUint32(tmp[19:23], uint32(s.Length)) 117 copy(tmp[23:], s.Data) 118 _, err := buf.Write(tmp) 119 if err != nil { 120 b.Fatal(err) 121 } 122 } 123 } 124 125 func BenchmarkDecode(b *testing.B) { 126 var out BenchStrucExample 127 var buf bytes.Buffer 128 if err := Pack(&buf, benchStrucRef); err != nil { 129 b.Fatal(err) 130 } 131 bufBytes := buf.Bytes() 132 for i := 0; i < b.N; i++ { 133 buf := bytes.NewReader(bufBytes) 134 err := Unpack(buf, &out) 135 if err != nil { 136 b.Fatal(err) 137 } 138 out.Data = nil 139 } 140 } 141 142 func BenchmarkStdlibDecode(b *testing.B) { 143 var out BenchExample 144 var buf bytes.Buffer 145 binary.Write(&buf, binary.BigEndian, *benchRef) 146 _, err := buf.Write(eightBytes) 147 if err != nil { 148 b.Fatal(err) 149 } 150 bufBytes := buf.Bytes() 151 for i := 0; i < b.N; i++ { 152 buf := bytes.NewReader(bufBytes) 153 err := binary.Read(buf, binary.BigEndian, &out) 154 if err != nil { 155 b.Fatal(err) 156 } 157 tmp := make([]byte, out.Length) 158 _, err = buf.Read(tmp) 159 if err != nil { 160 b.Fatal(err) 161 } 162 } 163 } 164 165 func BenchmarkManualDecode(b *testing.B) { 166 var o BenchStrucExample 167 var buf bytes.Buffer 168 if err := Pack(&buf, benchStrucRef); err != nil { 169 b.Fatal(err) 170 } 171 tmp := buf.Bytes() 172 order := binary.BigEndian 173 for i := 0; i < b.N; i++ { 174 copy(o.Test[:], tmp[0:5]) 175 o.A = int(order.Uint32(tmp[5:9])) 176 o.B = int(order.Uint16(tmp[9:11])) 177 o.C = int(order.Uint16(tmp[11:13])) 178 o.D = int(order.Uint16(tmp[13:15])) 179 copy(o.Test2[:], tmp[15:19]) 180 o.Length = int(order.Uint32(tmp[19:23])) 181 o.Data = make([]byte, o.Length) 182 copy(o.Data, tmp[23:]) 183 } 184 } 185 186 func BenchmarkFullEncode(b *testing.B) { 187 for i := 0; i < b.N; i++ { 188 var buf bytes.Buffer 189 if err := Pack(&buf, reference); err != nil { 190 b.Fatal(err) 191 } 192 } 193 } 194 195 func BenchmarkFullDecode(b *testing.B) { 196 var out Example 197 for i := 0; i < b.N; i++ { 198 buf := bytes.NewBuffer(referenceBytes) 199 if err := Unpack(buf, &out); err != nil { 200 b.Fatal(err) 201 } 202 } 203 }