-
Notifications
You must be signed in to change notification settings - Fork 0
/
idgen_test.go
170 lines (134 loc) · 3.02 KB
/
idgen_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
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
// Copyright 2020 @thiinbit. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file
package idgen
import (
"fmt"
"github.com/google/uuid"
suuid "github.com/satori/go.uuid"
"log"
"runtime"
"testing"
"time"
)
// var lock sync.RWMutex
func init() {
log.Println("Init max procs ", runtime.NumCPU()+1)
runtime.GOMAXPROCS(runtime.NumCPU() + 1)
}
// TestUsage usage test
func TestUsage(t *testing.T) {
// Generate a new ID
id, _ := Next()
// Get the ID generation time (int64 timestamp of millis)
idTimestamp := ExtractTimestamp(id)
fmt.Println(id, idTimestamp)
}
// TestNext
func TestNext(t *testing.T) {
runTimes := 3
testSize := 4000000
printSize := 100
checkDuplicate := false
// var wg sync.WaitGroup
// var lock sync.RWMutex
counter := make(map[int64]int)
timeCount := time.Duration(0)
for rt := 0; rt < runTimes; rt++ {
start := time.Now()
for i := 0; i < testSize; i++ {
// wg.Add(1)
// go func(wg *sync.WaitGroup, idx int) {
// defer wg.Done()
id, err := Next()
// _, err := Next()
if err != nil {
t.Fatal(err)
}
if checkDuplicate {
// lock.Lock()
counter[id]++
// if idx < printSize {
if i < printSize {
t.Log(id)
}
// lock.Unlock()
}
// val, _ := counter.LoadOrStore(id, 0)
// counter.Store(id, val.(int)+1)
// }(&wg, i)
}
elapsed := time.Since(start)
t.Log("Elapsed: ", elapsed)
timeCount += elapsed
}
t.Log("AverageElapsed: ", timeCount.Seconds()/float64(runTimes))
// wg.Wait()
// counter.Range(func(k, v interface{}) bool {
for k, v := range counter {
// if v.(int) != 1 {
if v != 1 {
t.Fatalf("%d is appears %d times", k, v)
}
}
// return true
// })
}
// TestExtractTimestamp
func TestExtractTimestamp(t *testing.T) {
testSize := 1000000
start := time.Now()
for i := 0; i < testSize; i++ {
id, _ := Next()
tT := ExtractTimestamp(id)
if tT != getTimestamp() && tT != getTimestamp()-1 {
t.Fatalf("id: %d, t: %d, extract timestampt fail!", id, tT)
}
}
t.Log("Finished! Elapsed ", time.Since(start))
}
func TestExtractMachine(t *testing.T) {
SetMachineID(128)
n, _ := Next()
if 128 != ExtractMachine(n) {
t.Fatal("F")
}
SetMachineID(1024)
n1, _ := Next()
if 1024 == ExtractMachine(n1) || 0 != ExtractMachine(n1) {
log.Fatal("overflow")
}
for i := 0; i < 1024; i++ {
SetMachineID(i)
nextID, err := Next()
if err != nil {
t.Fatal(err)
}
mID := ExtractMachine(nextID)
if i != mID {
t.Fatalf("id %d, machine: %d, expect: %d", nextID, mID, i)
}
}
}
func TestMod(t *testing.T) {
modReArr := [4095]int{0}
var m int64 = 64
for i := 0; i < 4000000; i++ {
n, _ := Next()
mIdx, _ := Mod(n, m)
modReArr[mIdx] += 1
uuid := UUID(n)
umIdx, err := ModUUID(uuid, m)
if err != nil {
t.Fatal(err)
}
if mIdx != umIdx {
t.Fatal("mIdx err")
}
}
log.Print(modReArr)
}
func TestUUID(t *testing.T) {
log.Println(uuid.Must(uuid.NewRandom()).String())
log.Println(suuid.Must(suuid.NewV4()).String())
}