forked from Vivino/rankdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_bench_test.go
136 lines (127 loc) · 3.25 KB
/
segment_bench_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
package rankdb
// Copyright 2019 Vivino. All rights reserved
//
// See LICENSE file for license details
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/Vivino/rankdb/blobstore"
"github.com/Vivino/rankdb/blobstore/memstore"
)
func timeUnix(t time.Time) uint32 {
return uint32(t.Unix())
}
const (
benchMaxElements = 10000
benchStartElements = 10
benchMulElements = 5
)
func randElements(n int, seed ...int64) Elements {
if n == 0 {
return Elements{}
}
rng := rand.New(rand.NewSource(0x1337beefc0cac01a))
if len(seed) > 0 {
rng = rand.New(rand.NewSource(seed[0]))
}
res := make(Elements, n)
for i := range res {
res[i] = Element{
Score: uint64(rng.Uint32()),
TieBreaker: rng.Uint32(),
Updated: timeUnix(time.Now().Add(-time.Millisecond * time.Duration(rng.Int63n(int64(time.Hour)*24*365)))),
ID: ElementID(rng.Uint64()),
Payload: []byte(`{"value":"` + RandString(5) + `", "type": "user-list"}`),
}
}
return res
}
func randSortedElements(n int, seed ...int64) Elements {
if n == 0 {
return Elements{}
}
rng := rand.New(rand.NewSource(0x1337beefc0cac01a))
if len(seed) > 0 {
rng = rand.New(rand.NewSource(seed[0]))
}
res := make(Elements, n)
for i := range res {
res[i] = Element{
Score: uint64(n - i),
TieBreaker: rng.Uint32(),
Updated: timeUnix(time.Now().Add(-time.Millisecond * time.Duration(rng.Int63n(int64(time.Hour)*24*365)))),
ID: ElementID(rng.Uint64()),
Payload: nil,
}
}
return res
}
func BenchmarkSegment_SaveElements(b *testing.B) {
ctx := context.Background()
for n := benchStartElements; n <= benchMaxElements; n *= benchMulElements {
in := NewElements(randSortedElements(n))
segs := NewSegments(0, false)
s := NewSegmentElements(segs, in)
memStore := memstore.NewMemStore()
store := blobstore.StoreWithSet(memStore, "test")
err := s.saveElements(ctx, store, s.elements)
if err != nil {
b.Fatal(err)
}
pl, err := store.Get(ctx, s.seg.StorageKey())
if err != nil {
memStore.Dir("test")
b.Log(s.seg.StorageKey())
b.Fatal(err)
}
b.Log(n, "Elements. Blob size:", len(pl))
b.ResetTimer()
b.Run(fmt.Sprint(n, " Elements"), func(b *testing.B) {
b.SetBytes(int64(len(pl)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := s.saveElements(ctx, store, in)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkSegment_LoadElements(b *testing.B) {
ctx := context.Background()
for n := benchStartElements; n <= benchMaxElements; n *= benchMulElements {
in := NewElements(randElements(n))
segs := NewSegments(0, false)
s := NewSegmentElements(segs, in)
mem := memstore.NewMemStore()
store := blobstore.StoreWithSet(mem, "test")
err := s.saveElements(ctx, store, in)
if err != nil {
b.Fatal(err)
}
pl, err := store.Get(ctx, s.seg.StorageKey())
if err != nil {
b.Log(mem.Dir("test"))
b.Fatal(err)
}
b.ResetTimer()
b.Log(n, "Elements. Blob size:", len(pl))
b.Run(fmt.Sprint(n, " Elements"), func(b *testing.B) {
b.SetBytes(int64(len(pl)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
e, err := s.seg.loadElements(ctx, store, true)
if err != nil {
b.Fatal(err)
}
if len(e) != len(in) {
b.Fatal(len(e), "!=", len(in))
}
}
})
}
}