-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached_test.go
82 lines (74 loc) · 1.2 KB
/
cached_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
package cachedrander
import (
"io"
"testing"
"github.com/google/uuid"
)
type gen struct {
size int
head, tail int
fills int
}
func (g *gen) Read(buf []byte) (int, error) {
if len(buf) > g.size {
buf = buf[:g.size]
}
if g.head == g.tail {
g.fills++
g.tail += g.size
}
for j := range buf {
if g.head == g.tail {
return j, nil
}
buf[j] = byte(g.head)
g.head++
}
return len(buf), nil
}
func TestReader(t *testing.T) {
g := &gen{size: 17}
r, err := New(g, 1024)
r.Max = 8
if err != nil {
t.Fatal(err)
}
var buf [64]byte
next := 0
for i := 1; i < len(buf); i++ {
n, err := io.ReadFull(r, buf[:i])
if err != nil {
t.Fatal(err)
}
for _, b := range buf[:n] {
if b != byte(next) {
t.Fatalf("byte %d: got %d, want %d", next, b, byte(next))
}
next++
}
}
}
func BenchmarkNormal(b *testing.B) {
b.StopTimer()
uuid.SetRand(nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 10000; j++ {
uuid.New()
}
}
}
func BenchmarkCached(b *testing.B) {
b.StopTimer()
r, err := NewUUIDReader(1000)
if err != nil {
b.Fatal(err)
}
uuid.SetRand(r)
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 10000; j++ {
uuid.New()
}
}
}