This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_benchmark_test.go
More file actions
183 lines (162 loc) · 3.83 KB
/
random_benchmark_test.go
File metadata and controls
183 lines (162 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package fastrand_test
import (
crand "crypto/rand"
"fmt"
"github.com/SyNdicateFoundation/fastrand"
"testing"
)
var benchmarkErr error
func BenchmarkIntnFastRand(b *testing.B) {
b.ReportAllocs()
var res int
for i := 0; i < b.N; i++ {
res = fastrand.IntN(10000)
}
_ = res
}
func BenchmarkSecureIntFastRand(b *testing.B) {
b.ReportAllocs()
var res int
var err error
for i := 0; i < b.N; i++ {
res, err = fastrand.SecureInt(0, 10000-1)
}
_ = res
benchmarkErr = err
}
func BenchmarkFloat64FastRand(b *testing.B) {
b.ReportAllocs()
var res float64
for i := 0; i < b.N; i++ {
res = fastrand.Float64()
}
_ = res
}
func BenchmarkSecureFloat64FastRand(b *testing.B) {
b.ReportAllocs()
var res float64
for i := 0; i < b.N; i++ {
res = fastrand.SecureFloat64()
}
_ = res
}
var byteBenchmarkSizes = []int{8, 64, 512, 4096}
func BenchmarkInternalFastBytes(b *testing.B) {
for _, size := range byteBenchmarkSizes {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
b.ReportAllocs()
buf := make([]byte, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
filled := 0
for filled < size {
randValF := fastrand.Float64()
randVal := uint64(randValF * float64(1<<63))
bytesToCopy := 8
remaining := size - filled
if remaining < bytesToCopy {
bytesToCopy = remaining
}
if bytesToCopy > 0 {
buf[filled] = byte(randVal)
}
filled += bytesToCopy
}
}
_ = buf
})
}
}
func BenchmarkBytesCryptoRand(b *testing.B) {
for _, size := range byteBenchmarkSizes {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
b.ReportAllocs()
var res []byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
res = fastrand.Bytes(size)
}
_ = res
})
}
}
func BenchmarkSecureBytesFastRand(b *testing.B) {
for _, size := range byteBenchmarkSizes {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
b.ReportAllocs()
var res []byte
var err error
b.ResetTimer()
for i := 0; i < b.N; i++ {
res, err = fastrand.SecureBytes(size)
}
_ = res
benchmarkErr = err
})
}
}
func BenchmarkBytesStdLibCryptoRand(b *testing.B) {
for _, size := range byteBenchmarkSizes {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
b.ReportAllocs()
buf := make([]byte, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, benchmarkErr = crand.Read(buf)
if benchmarkErr != nil {
b.Fatal(benchmarkErr)
}
}
_ = buf
})
}
}
var stringBenchmarkSizes = []int{8, 32, 128, 1024}
var stringCharset = fastrand.CharsAlphabetDigits
func BenchmarkStringFastRand(b *testing.B) {
cs := string(stringCharset)
for _, size := range stringBenchmarkSizes {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
b.ReportAllocs()
var res string
b.ResetTimer()
for i := 0; i < b.N; i++ {
res = fastrand.String(size, fastrand.CharsList(cs))
}
_ = res
})
}
}
func BenchmarkSecureStringFastRand(b *testing.B) {
cs := string(stringCharset)
for _, size := range stringBenchmarkSizes {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
b.ReportAllocs()
var res string
var err error
b.ResetTimer()
for i := 0; i < b.N; i++ {
res, err = fastrand.SecureString(size, fastrand.CharsList(cs))
}
_ = res
benchmarkErr = err
})
}
}
func BenchmarkEngine(b *testing.B) {
engine := fastrand.NewEngine()
payload := []byte("User:{RAND;10-20;ABL,ABU}|Sess:{RAND;32;HEX}|ID:{RAND;UUID,HEX}|IP:{RAND;IPV4}|Data:{RAND;50,60,70}")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = engine.Randomizer(payload)
}
}
func BenchmarkRandomizer(b *testing.B) {
payload := []byte("User: {RAND;10-20;ABL,ABU} | Session: {RANDOM;32;HEX} | ID: {RAND;UUID,HEX} | IP: {RAND;IPV4} | Data: {RAND;50-99} --- End")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = fastrand.Randomizer(payload)
}
}