github.com/coyove/common@v0.0.0-20240403014525-f70e643f9de8/rand/rand_test.go (about) 1 package rand 2 3 import ( 4 "encoding/binary" 5 "sync" 6 "sync/atomic" 7 //"encoding/binary" 8 "math/rand" 9 "testing" 10 "time" 11 ) 12 13 func TestRand(t *testing.T) { 14 r := New() 15 sum := uint64(0) 16 count := 10000 17 for i := 0; i < count; i++ { 18 go atomic.AddUint64(&sum, uint64(r.Intn(11))) 19 } 20 t.Log(float64(sum) / float64(count)) 21 } 22 23 func BenchmarkRand(b *testing.B) { 24 r := New() 25 26 for i := 0; i < b.N; i++ { 27 r.Intn(100) 28 } 29 } 30 31 func BenchmarkRandRandom(b *testing.B) { 32 r := New() 33 buf := make([]byte, 1024) 34 35 for i := 0; i < b.N; i++ { 36 r.Read(buf[:r.Intn(1024)]) 37 } 38 } 39 40 func BenchmarkMathRandRandom(b *testing.B) { 41 r := rand.New(rand.NewSource(time.Now().UnixNano())) 42 buf := make([]byte, 1024) 43 44 for i := 0; i < b.N; i++ { 45 n := r.Intn(1024) 46 for j := 0; j <= n-8; j += 8 { 47 binary.BigEndian.PutUint64(buf[j:j+8], r.Uint64()) 48 } 49 50 for j := 0; j < n%8; j++ { 51 buf[len(buf)-1-j] = byte(r.Intn(256)) 52 } 53 } 54 } 55 56 var dummy = 0 57 58 func BenchmarkRandMulti(b *testing.B) { 59 r := New() 60 61 b.RunParallel(func(pb *testing.PB) { 62 for pb.Next() { 63 dummy += r.Intn(100) 64 } 65 }) 66 } 67 68 func BenchmarkMathRandMulti(b *testing.B) { 69 r := rand.New(rand.NewSource(time.Now().UnixNano())) 70 var mu sync.Mutex 71 72 b.RunParallel(func(pb *testing.PB) { 73 for pb.Next() { 74 mu.Lock() 75 dummy += r.Intn(100) 76 mu.Unlock() 77 } 78 }) 79 } 80 81 func BenchmarkMathRand(b *testing.B) { 82 r := rand.New(rand.NewSource(time.Now().UnixNano())) 83 84 for i := 0; i < b.N; i++ { 85 r.Intn(100) 86 } 87 } 88 89 func BenchmarkRand64(b *testing.B) { 90 r := New() 91 92 for i := 0; i < b.N; i++ { 93 r.Uint64() 94 } 95 } 96 97 func BenchmarkMathRand64(b *testing.B) { 98 r := rand.New(rand.NewSource(time.Now().UnixNano())) 99 100 for i := 0; i < b.N; i++ { 101 r.Uint64() 102 } 103 } 104 105 func BenchmarkRandRead32(b *testing.B) { 106 r := New() 107 buf := make([]byte, 32) 108 for i := 0; i < b.N; i++ { 109 r.Read(buf) 110 } 111 } 112 113 func BenchmarkRandRead32_2(b *testing.B) { 114 r := New() 115 buf := make([]byte, 32) 116 for i := 0; i < b.N; i++ { 117 binary.BigEndian.PutUint64(buf, r.Uint64()) 118 binary.BigEndian.PutUint64(buf[8:], r.Uint64()) 119 binary.BigEndian.PutUint64(buf[16:], r.Uint64()) 120 binary.BigEndian.PutUint64(buf[24:], r.Uint64()) 121 } 122 } 123 124 func BenchmarkMathRandRead32(b *testing.B) { 125 r := rand.New(rand.NewSource(time.Now().UnixNano())) 126 buf := make([]byte, 32) 127 128 for i := 0; i < b.N; i++ { 129 for j := 0; j < 32; j++ { 130 buf[j] = byte(r.Intn(256)) 131 } 132 } 133 } 134 135 func BenchmarkMathRandRead32_2(b *testing.B) { 136 r := rand.New(rand.NewSource(time.Now().UnixNano())) 137 buf := make([]byte, 32) 138 139 for i := 0; i < b.N; i++ { 140 binary.BigEndian.PutUint64(buf, r.Uint64()) 141 binary.BigEndian.PutUint64(buf[8:], r.Uint64()) 142 binary.BigEndian.PutUint64(buf[16:], r.Uint64()) 143 binary.BigEndian.PutUint64(buf[24:], r.Uint64()) 144 } 145 }