-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgeneration.go
More file actions
271 lines (230 loc) · 6.98 KB
/
generation.go
File metadata and controls
271 lines (230 loc) · 6.98 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package benchmark
import (
crand "crypto/rand"
mrand "math/rand"
"sync"
"testing"
"time"
"github.com/bwmarrin/snowflake"
"github.com/celrenheit/sandflake"
"github.com/gofrs/uuid"
"github.com/lucsky/cuid"
"github.com/muyo/sno"
"github.com/oklog/ulid"
"github.com/rs/xid"
"github.com/segmentio/ksuid"
"github.com/sony/sonyflake"
)
func benchmarkGeneration(b *testing.B) {
println("\n-- Generation (sequential) -------------------------------------------------------------------\n")
b.Run("s", benchmarkGenerateSequential)
println("\n-- Generation (parallel) ---------------------------------------------------------------------\n")
b.Run("p", benchmarkGenerateParallel)
}
func benchmarkGenerateSequential(b *testing.B) {
b.Run("sno", benchmarkGenerateSequentialSno) // Bounded
b.Run("xid", benchmarkGenerateSequentialXid) // Unbounded
b.Run("snowflake", benchmarkGenerateSequentialSnowflake) // Bounded
b.Run("sonyflake", benchmarkGenerateSequentialSonyflake) // Bounded
b.Run("sandflake", benchmarkGenerateSequentialSandflake) // Unbounded
b.Run("cuid", benchmarkGenerateSequentialCuid) // Unbounded
b.Run("uuid", benchmarkGenerateSequentialUUID) // Unbounded
b.Run("ulid", benchmarkGenerateSequentialULID) // Unbounded
b.Run("ksuid", benchmarkGenerateSequentialKSUID) // Unbounded
}
func benchmarkGenerateParallel(b *testing.B) {
b.Run("sno", benchmarkGenerateParallelSno) // Bounded
b.Run("xid", benchmarkGenerateParallelXid) // Unbounded
b.Run("snowflake", benchmarkGenerateParallelSnowflake) // Bounded
b.Run("sonyflake", benchmarkGenerateParallelSonyflake) // Bounded
b.Run("sandflake", benchmarkGenerateParallelSandflake) // Unbounded
b.Run("cuid", benchmarkGenerateParallelCuid) // Unbounded
b.Run("uuid", benchmarkGenerateParallelUUID) // Unbounded
b.Run("ulid", benchmarkGenerateParallelULID) // Unbounded
b.Run("ksuid", benchmarkGenerateParallelKSUID) // Unbounded
}
func benchmarkGenerateSequentialSno(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sno.New(255)
}
}
func benchmarkGenerateSequentialXid(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = xid.New()
}
}
func benchmarkGenerateSequentialSnowflake(b *testing.B) {
n, _ := snowflake.NewNode(255)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = n.Generate()
}
}
func benchmarkGenerateSequentialSonyflake(b *testing.B) {
g := sonyflake.NewSonyflake(sonyflake.Settings{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = g.NextID()
}
}
func benchmarkGenerateSequentialSandflake(b *testing.B) {
var g sandflake.Generator
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = g.Next()
}
}
func benchmarkGenerateSequentialCuid(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = cuid.New()
}
}
func benchmarkGenerateSequentialUUID(b *testing.B) {
b.Run("v1", benchmarkGenerateSequentialUUIDv1)
b.Run("v4", benchmarkGenerateSequentialUUIDv4)
}
func benchmarkGenerateSequentialUUIDv1(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = uuid.NewV1()
}
}
func benchmarkGenerateSequentialUUIDv4(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = uuid.NewV4()
}
}
// A note about the included ULID runs.
//
// ULIDs generators expect time to be passed in as a timestamp with msec precision. All of the other
// libraries being tested handle time sourcing themselves, which is reflected in their results.
// Therefore the time fetching (via time.Now()) including the unit conversion (via ulid.Timestamp())
// is included in each iteration. If the time had been fetched outside the benchmark loop, the results
// would be roughly 7nsec/op lower (@go 1.14.1, Windows 10, i7 4770k 4.4GHz).
//
// The ULID package benchmarks itself when no entropy source is provided, which in a run resulted
// at 29.8ns/op (relative to unbounded Sno at 8.8ns/op, for reference). However, this test is
// excluded in this benchmark. While it may measure ULID's raw overhead, it does not measure
// a end-user usable case since ULIDs without entropy are essentially a 48bit timestamp and...
// 10 zero bytes, which defeats the purpose of the spec.
func benchmarkGenerateSequentialULID(b *testing.B) {
b.Run("crypto", benchmarkGenerateSequentialULIDCrypto)
b.Run("math", benchmarkSequentialNewULIDMath)
}
func benchmarkGenerateSequentialULIDCrypto(b *testing.B) {
rng := crand.Reader
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ulid.New(ulid.Timestamp(time.Now()), rng)
}
}
func benchmarkSequentialNewULIDMath(b *testing.B) {
rng := mrand.New(mrand.NewSource(time.Now().UnixNano()))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ulid.New(ulid.Timestamp(time.Now()), rng)
}
}
func benchmarkGenerateSequentialKSUID(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = ksuid.NewRandom()
}
}
func benchmarkGenerateParallelSno(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = sno.New(255)
}
})
}
func benchmarkGenerateParallelXid(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = xid.New()
}
})
}
func benchmarkGenerateParallelSnowflake(b *testing.B) {
n, _ := snowflake.NewNode(255)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = n.Generate()
}
})
}
func benchmarkGenerateParallelSonyflake(b *testing.B) {
g := sonyflake.NewSonyflake(sonyflake.Settings{})
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = g.NextID()
}
})
}
func benchmarkGenerateParallelSandflake(b *testing.B) {
var g sandflake.Generator
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = g.Next()
}
})
}
func benchmarkGenerateParallelCuid(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = cuid.New()
}
})
}
func benchmarkGenerateParallelUUID(b *testing.B) {
b.Run("v1", benchmarkGenerateParallelUUIDv1)
b.Run("v4", benchmarkGenerateParallelUUIDv4)
}
func benchmarkGenerateParallelUUIDv1(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = uuid.NewV1()
}
})
}
func benchmarkGenerateParallelUUIDv4(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = uuid.NewV4()
}
})
}
func benchmarkGenerateParallelULID(b *testing.B) {
b.Run("crypto", benchmarkGenerateParallelULIDCrypto)
b.Run("math", benchmarkGenerateParallelULIDMath)
}
func benchmarkGenerateParallelULIDCrypto(b *testing.B) {
rng := crand.Reader
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = ulid.New(ulid.Timestamp(time.Now()), rng)
}
})
}
func benchmarkGenerateParallelULIDMath(b *testing.B) {
// Note: Requires manual locking for this run to complete.
rng := mrand.New(mrand.NewSource(time.Now().UnixNano()))
mu := sync.Mutex{}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
mu.Lock()
_, _ = ulid.New(ulid.Timestamp(time.Now()), rng)
mu.Unlock()
}
})
}
func benchmarkGenerateParallelKSUID(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = ksuid.NewRandom()
}
})
}