-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgosnowflake_test.go
executable file
·71 lines (56 loc) · 1.31 KB
/
gosnowflake_test.go
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
package main
import (
"fmt"
"github.com/erans/gosnowflake/idworker"
"github.com/stretchrcom/testify/assert"
"testing"
"time"
)
func TestNewIdWorker(t *testing.T) {
worker, err := idworker.NewIdWorker(1, 1)
assert.Nil(t, err)
id1, err := worker.Next()
assert.Nil(t, err)
id2, err := worker.Next()
assert.Nil(t, err)
if id1 > id2 {
t.Errorf("id2 %v is smaller then previous one %v", id2, id1)
}
}
func TestDuplicateKeys(t *testing.T) {
worker, err := idworker.NewIdWorker(1, 1)
assert.Nil(t, err)
const MAXIDS = 100000
var ids [MAXIDS]uint64
start := time.Now()
for i := 0; i < MAXIDS; i++ {
id, err := worker.Next()
assert.Nil(t, err)
ids[i] = id
}
for i := 1; i < MAXIDS; i++ {
if ids[i-1] > ids[i] {
t.Errorf("next id %v is smaller then previous one %v", ids[i], ids[i-1])
}
}
end := time.Now()
delta := float64(end.UnixNano()-start.UnixNano()) / float64(1000*1000)
t.Logf("Execution time: %fms\n", delta)
t.Logf("Ids/sec: %f\n", float64(MAXIDS)/delta)
}
func BenchmarkIdWorker(b *testing.B) {
worker, err := idworker.NewIdWorker(1, 1)
if err == nil {
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, err := worker.Next()
if err == nil {
fmt.Sprintf("%d", id)
} else {
b.Errorf("Failed to create id")
}
}
} else {
b.Errorf("Failed to create IdWorker")
}
}