More tests

This commit is contained in:
Bruce Marriner 2016-05-25 16:25:52 -05:00
parent 88e3540ee4
commit 433509f1af
2 changed files with 93 additions and 0 deletions

View File

@ -70,6 +70,7 @@ func (n *Node) Generator(c chan Flake) {
if n.step == 0 { if n.step == 0 {
// wait for ticker.. // wait for ticker..
// haha, funny, this isn't fast enough to ever get here.
} }
case <-ticker.C: case <-ticker.C:
now++ now++
@ -141,6 +142,58 @@ func (n *Node) Generate() (Flake, error) {
), nil ), nil
} }
// Return a freshly generated Flake ID
func (n *Node) GenerateNoSleep() (Flake, error) {
now := time.Now().UnixNano() / 1000000
if n.lastTime == now {
n.step = (n.step + 1) & StepMask
if n.step == 0 {
for now <= n.lastTime {
now = time.Now().UnixNano() / 1000000
}
}
} else {
n.step = 0
}
n.lastTime = now
return Flake((now-n.epoch)<<TimeShift |
(n.node << NodeShift) |
(n.step),
), nil
}
// Return a freshly generated Flake ID
func (n *Node) GenerateNoSleepLock() (Flake, error) {
n.Lock()
defer n.Unlock()
now := time.Now().UnixNano() / 1000000
if n.lastTime == now {
n.step = (n.step + 1) & StepMask
if n.step == 0 {
for now <= n.lastTime {
now = time.Now().UnixNano() / 1000000
}
}
} else {
n.step = 0
}
n.lastTime = now
return Flake((now-n.epoch)<<TimeShift |
(n.node << NodeShift) |
(n.step),
), nil
}
type Flake int64 type Flake int64
type Flakes []*Flake type Flakes []*Flake

View File

@ -35,6 +35,46 @@ func BenchmarkGenerateChanParallel(b *testing.B) {
}) })
} }
// Benchmarks Presence Update event with fake data.
func BenchmarkGenerateNoSleep(b *testing.B) {
node, _ := NewFlakeNode(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
_, _ = node.GenerateNoSleep()
}
}
// Benchmarks Presence Update event with fake data.
func BenchmarkGenerateNoSleepLock(b *testing.B) {
node, _ := NewFlakeNode(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
_, _ = node.GenerateNoSleepLock()
}
}
// Benchmarks Presence Update event with fake data.
func BenchmarkGenerateNoSleepLockParallel(b *testing.B) {
node, _ := NewFlakeNode(1)
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = node.GenerateNoSleepLock()
}
})
}
// Benchmarks Presence Update event with fake data.
// Benchmarks Presence Update event with fake data. // Benchmarks Presence Update event with fake data.
func BenchmarkGenerate(b *testing.B) { func BenchmarkGenerate(b *testing.B) {