github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/sys/xxhash3/performance_test.go (about) 1 package xxhash3 2 3 import ( 4 "runtime" 5 "testing" 6 ) 7 8 type benchTask struct { 9 name string 10 version int 11 action func(b []byte) uint64 12 action128 func(b []byte) [2]uint64 13 } 14 15 func BenchmarkDefault(b *testing.B) { 16 all := []benchTask{{ 17 name: "Target64", version: 64, action: func(b []byte) uint64 { 18 return Hash(b) 19 }}, { 20 name: "Target128", version: 128, action128: func(b []byte) [2]uint64 { 21 return Hash128(b) 22 }}, 23 } 24 25 benchLen0_16(b, all) 26 benchLen17_128(b, all) 27 benchLen129_240(b, all) 28 benchLen241_1024(b, all) 29 benchScalar(b, all) 30 benchAVX2(b, all) 31 benchSSE2(b, all) 32 } 33 34 func benchLen0_16(b *testing.B, benchTasks []benchTask) { 35 for _, v := range benchTasks { 36 b.Run("Len0_16/"+v.name, func(b *testing.B) { 37 b.ResetTimer() 38 for n := 0; n < b.N; n++ { 39 for i := 0; i <= 16; i++ { 40 input := dat[b.N : b.N+i] 41 if v.version == 64 { 42 a := v.action(input) 43 runtime.KeepAlive(a) 44 } else { 45 a := v.action128(input) 46 runtime.KeepAlive(a) 47 } 48 } 49 } 50 }) 51 } 52 } 53 54 func benchLen17_128(b *testing.B, benchTasks []benchTask) { 55 for _, v := range benchTasks { 56 b.Run("Len17_128/"+v.name, func(b *testing.B) { 57 b.ResetTimer() 58 for n := 0; n < b.N; n++ { 59 for i := 17; i <= 128; i++ { 60 input := dat[b.N : b.N+i] 61 if v.version == 64 { 62 a := v.action(input) 63 runtime.KeepAlive(a) 64 } else { 65 a := v.action128(input) 66 runtime.KeepAlive(a) 67 } 68 } 69 } 70 }) 71 } 72 } 73 74 func benchLen129_240(b *testing.B, benchTasks []benchTask) { 75 for _, v := range benchTasks { 76 b.Run("Len129_240/"+v.name, func(b *testing.B) { 77 b.ResetTimer() 78 for n := 0; n < b.N; n++ { 79 for i := 129; i <= 240; i++ { 80 input := dat[b.N : b.N+i] 81 if v.version == 64 { 82 a := v.action(input) 83 runtime.KeepAlive(a) 84 } else { 85 a := v.action128(input) 86 runtime.KeepAlive(a) 87 } 88 } 89 } 90 }) 91 } 92 } 93 94 func benchLen241_1024(b *testing.B, benchTasks []benchTask) { 95 avx2, sse2 = false, false 96 for _, v := range benchTasks { 97 b.Run("Len241_1024/"+v.name, func(b *testing.B) { 98 b.ResetTimer() 99 for n := 0; n < b.N; n++ { 100 for i := 241; i <= 1024; i++ { 101 input := dat[b.N : b.N+i] 102 if v.version == 64 { 103 a := v.action(input) 104 runtime.KeepAlive(a) 105 } else { 106 a := v.action128(input) 107 runtime.KeepAlive(a) 108 } 109 } 110 } 111 }) 112 } 113 } 114 115 func benchScalar(b *testing.B, benchTasks []benchTask) { 116 avx2, sse2 = false, false 117 for _, v := range benchTasks { 118 b.Run("Scalar/"+v.name, func(b *testing.B) { 119 b.ResetTimer() 120 for n := 0; n < b.N; n++ { 121 input := dat[n:33554432] 122 if v.version == 64 { 123 a := v.action(input) 124 runtime.KeepAlive(a) 125 } else { 126 a := v.action128(input) 127 runtime.KeepAlive(a) 128 } 129 } 130 }) 131 } 132 } 133 134 func benchAVX2(b *testing.B, benchTasks []benchTask) { 135 avx2, sse2 = true, false 136 for _, v := range benchTasks { 137 b.Run("AVX2/"+v.name, func(b *testing.B) { 138 b.ResetTimer() 139 for n := 0; n < b.N; n++ { 140 input := dat[n:33554432] 141 if v.version == 64 { 142 a := v.action(input) 143 runtime.KeepAlive(a) 144 } else { 145 a := v.action128(input) 146 runtime.KeepAlive(a) 147 } 148 } 149 }) 150 } 151 } 152 func benchSSE2(b *testing.B, benchTasks []benchTask) { 153 avx2, sse2 = false, true 154 for _, v := range benchTasks { 155 b.Run("SSE2/"+v.name, func(b *testing.B) { 156 b.ResetTimer() 157 for n := 0; n < b.N; n++ { 158 input := dat[n:33554432] 159 if v.version == 64 { 160 a := v.action(input) 161 runtime.KeepAlive(a) 162 } else { 163 a := v.action128(input) 164 runtime.KeepAlive(a) 165 } 166 } 167 }) 168 } 169 }