github.com/zhangyunhao116/fastrand@v0.4.0/benchmark_test.go (about) 1 package fastrand 2 3 import ( 4 "math/rand" 5 "os" 6 "testing" 7 ) 8 9 type testFuncs struct { 10 fu32 func() uint32 11 fu64 func() uint64 12 fint func() int 13 fintn func(_ int) int 14 fread func(_ []byte) (int, error) 15 fperm func(_ int) []int 16 fshuffle func(_ int, swap func(i, j int)) 17 } 18 19 func init() { 20 if os.Getenv("BENCHMARK_TARGET") == "std" { 21 targetfs = stdrandfs 22 } else { 23 targetfs = fastrandfs 24 } 25 } 26 27 var ( 28 targetfs testFuncs // benchmark this target 29 stdrandfs = testFuncs{ 30 fu32: rand.Uint32, 31 fu64: rand.Uint64, 32 fint: rand.Int, 33 fintn: rand.Intn, 34 fread: rand.Read, 35 fperm: rand.Perm, 36 fshuffle: rand.Shuffle, 37 } 38 fastrandfs = testFuncs{ 39 fu32: Uint32, 40 fu64: Uint64, 41 fint: Int, 42 fintn: Intn, 43 fread: Read, 44 fperm: Perm, 45 fshuffle: Shuffle, 46 } 47 ) 48 49 func benchmarkSingleCore(b *testing.B, fs testFuncs) { 50 b.Run("Uint32()", func(b *testing.B) { 51 for i := 0; i < b.N; i++ { 52 _ = fs.fu32() 53 } 54 }) 55 b.Run("Uint64()", func(b *testing.B) { 56 for i := 0; i < b.N; i++ { 57 _ = fs.fu64() 58 } 59 }) 60 b.Run("Int()", func(b *testing.B) { 61 for i := 0; i < b.N; i++ { 62 _ = fs.fint() 63 } 64 }) 65 b.Run("Intn(32)", func(b *testing.B) { 66 for i := 0; i < b.N; i++ { 67 _ = fs.fintn(32) 68 } 69 }) 70 b.Run("Read/1024", func(b *testing.B) { 71 p := make([]byte, 1024) 72 b.ResetTimer() 73 for i := 0; i < b.N; i++ { 74 fs.fread(p) 75 } 76 }) 77 b.Run("Read/10240", func(b *testing.B) { 78 p := make([]byte, 10240) 79 b.ResetTimer() 80 for i := 0; i < b.N; i++ { 81 fs.fread(p) 82 } 83 }) 84 b.Run("Perm/1024", func(b *testing.B) { 85 b.ResetTimer() 86 for i := 0; i < b.N; i++ { 87 fs.fperm(1024) 88 } 89 }) 90 b.Run("Shuffle/1024", func(b *testing.B) { 91 x := make([]int, 1024) 92 b.ResetTimer() 93 for i := 0; i < b.N; i++ { 94 fs.fshuffle(1024, func(i, j int) { 95 x[i], x[j] = x[j], x[i] 96 }) 97 } 98 }) 99 } 100 101 func benchmarkMultipleCore(b *testing.B, fs testFuncs) { 102 b.Run("Uint32()", func(b *testing.B) { 103 b.RunParallel(func(pb *testing.PB) { 104 for pb.Next() { 105 _ = fs.fu32() 106 } 107 }) 108 }) 109 b.Run("Uint64()", func(b *testing.B) { 110 b.RunParallel(func(pb *testing.PB) { 111 for pb.Next() { 112 _ = fs.fu64() 113 } 114 }) 115 }) 116 b.Run("Int()", func(b *testing.B) { 117 b.RunParallel(func(pb *testing.PB) { 118 for pb.Next() { 119 _ = fs.fint() 120 } 121 }) 122 }) 123 b.Run("Intn(32)", func(b *testing.B) { 124 b.RunParallel(func(pb *testing.PB) { 125 for pb.Next() { 126 _ = fs.fintn(32) 127 } 128 }) 129 }) 130 b.Run("Read/1024", func(b *testing.B) { 131 p := make([]byte, 1024) 132 b.ResetTimer() 133 b.RunParallel(func(pb *testing.PB) { 134 for pb.Next() { 135 fs.fread(p) 136 } 137 }) 138 }) 139 b.Run("Read/10240", func(b *testing.B) { 140 p := make([]byte, 10240) 141 b.ResetTimer() 142 b.RunParallel(func(pb *testing.PB) { 143 for pb.Next() { 144 fs.fread(p) 145 } 146 }) 147 }) 148 b.Run("Perm/1024", func(b *testing.B) { 149 b.ResetTimer() 150 b.RunParallel(func(pb *testing.PB) { 151 for pb.Next() { 152 fs.fperm(1024) 153 } 154 }) 155 }) 156 } 157 158 func BenchmarkSingleCore(b *testing.B) { 159 benchmarkSingleCore(b, targetfs) 160 } 161 162 func BenchmarkMultipleCore(b *testing.B) { 163 benchmarkMultipleCore(b, targetfs) 164 }