github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/lang/fastrand/fastrand_test.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fastrand 16 17 import ( 18 "math/rand" 19 "testing" 20 ) 21 22 func TestAll(t *testing.T) { 23 _ = Uint32() 24 25 p := make([]byte, 1000) 26 n, err := Read(p) 27 if n != len(p) || err != nil || (p[0] == 0 && p[1] == 0 && p[2] == 0) { 28 t.Fatal() 29 } 30 31 a := Perm(100) 32 for i := range a { 33 var find bool 34 for _, v := range a { 35 if v == i { 36 find = true 37 } 38 } 39 if !find { 40 t.Fatal() 41 } 42 } 43 44 Shuffle(len(a), func(i, j int) { 45 a[i], a[j] = a[j], a[i] 46 }) 47 for i := range a { 48 var find bool 49 for _, v := range a { 50 if v == i { 51 find = true 52 } 53 } 54 if !find { 55 t.Fatal() 56 } 57 } 58 } 59 60 func BenchmarkSingleCore(b *testing.B) { 61 b.Run("math/rand-Uint32()", func(b *testing.B) { 62 for i := 0; i < b.N; i++ { 63 _ = rand.Uint32() 64 } 65 }) 66 b.Run("fast-rand-Uint32()", func(b *testing.B) { 67 for i := 0; i < b.N; i++ { 68 _ = Uint32() 69 } 70 }) 71 72 b.Run("math/rand-Uint64()", func(b *testing.B) { 73 for i := 0; i < b.N; i++ { 74 _ = rand.Uint64() 75 } 76 }) 77 b.Run("fast-rand-Uint64()", func(b *testing.B) { 78 for i := 0; i < b.N; i++ { 79 _ = Uint64() 80 } 81 }) 82 83 b.Run("math/rand-Read1000", func(b *testing.B) { 84 p := make([]byte, 1000) 85 b.ResetTimer() 86 for i := 0; i < b.N; i++ { 87 rand.Read(p) 88 } 89 }) 90 b.Run("fast-rand-Read1000", func(b *testing.B) { 91 p := make([]byte, 1000) 92 b.ResetTimer() 93 for i := 0; i < b.N; i++ { 94 Read(p) 95 } 96 }) 97 98 } 99 100 func BenchmarkMultipleCore(b *testing.B) { 101 b.Run("math/rand-Uint32()", func(b *testing.B) { 102 b.RunParallel(func(pb *testing.PB) { 103 for pb.Next() { 104 _ = rand.Uint32() 105 } 106 }) 107 }) 108 b.Run("fast-rand-Uint32()", func(b *testing.B) { 109 b.RunParallel(func(pb *testing.PB) { 110 for pb.Next() { 111 _ = Uint32() 112 } 113 }) 114 }) 115 116 b.Run("math/rand-Uint64()", func(b *testing.B) { 117 b.RunParallel(func(pb *testing.PB) { 118 for pb.Next() { 119 _ = rand.Uint64() 120 } 121 }) 122 }) 123 b.Run("fast-rand-Uint64()", func(b *testing.B) { 124 b.RunParallel(func(pb *testing.PB) { 125 for pb.Next() { 126 _ = Uint64() 127 } 128 }) 129 }) 130 131 b.Run("math/rand-Read1000", func(b *testing.B) { 132 p := make([]byte, 1000) 133 b.ResetTimer() 134 b.RunParallel(func(pb *testing.PB) { 135 for pb.Next() { 136 rand.Read(p) 137 } 138 }) 139 }) 140 b.Run("fast-rand-Read1000", func(b *testing.B) { 141 p := make([]byte, 1000) 142 b.ResetTimer() 143 b.RunParallel(func(pb *testing.PB) { 144 for pb.Next() { 145 Read(p) 146 } 147 }) 148 }) 149 }