mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
snow flake
This commit is contained in:
@ -1,6 +1,15 @@
|
||||
package snowflake
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/charlienet/go-mixed/sets"
|
||||
)
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
s := CreateSnowflake(2)
|
||||
t.Log(s.GetId())
|
||||
}
|
||||
|
||||
func TestGetId(t *testing.T) {
|
||||
s := CreateSnowflake(22)
|
||||
@ -8,3 +17,46 @@ func TestGetId(t *testing.T) {
|
||||
t.Log(s.GetId())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMutiGetId(t *testing.T) {
|
||||
s := CreateSnowflake(11)
|
||||
for i := 0; i < 100000; i++ {
|
||||
s.GetId()
|
||||
}
|
||||
}
|
||||
|
||||
func TestMutiConflict(t *testing.T) {
|
||||
set := sets.NewHashSet[int64]()
|
||||
s := CreateSnowflake(11)
|
||||
for i := 0; i < 10000000; i++ {
|
||||
id := s.GetId()
|
||||
if set.Contains(id) {
|
||||
t.Fatal("失败,生成重复数据")
|
||||
}
|
||||
|
||||
set.Add(id)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetId(b *testing.B) {
|
||||
s := CreateSnowflake(11)
|
||||
for i := 0; i < b.N; i++ {
|
||||
s.GetId()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMutiGetId(b *testing.B) {
|
||||
s := CreateSnowflake(11)
|
||||
set := sets.NewHashSet[int64]().WithSync()
|
||||
b.RunParallel(func(p *testing.PB) {
|
||||
for i := 0; p.Next(); i++ {
|
||||
id := s.GetId()
|
||||
|
||||
if set.Contains(id) {
|
||||
b.Fatal("标识重复", id)
|
||||
}
|
||||
|
||||
set.Add(id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user