-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
98 lines (77 loc) · 1.85 KB
/
main_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
package main
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/alex-shch/travels/loader"
"github.com/alex-shch/travels/proto"
"github.com/alex-shch/travels/store"
)
var (
testStore *store.Store
usersCount int
visitsCount int
locationsCount int
)
func initBench() {
if testStore != nil {
return
}
startTime := time.Now()
rand.Seed(startTime.Unix())
testStore = store.New()
if err := loader.Load(testStore, "testdata/full/data.zip"); err != nil {
panic(err)
}
usersCount = len(testStore.Users.Pool)
visitsCount = len(testStore.Visits.Pool)
locationsCount = len(testStore.Locations.Pool)
fmt.Printf("users count: %d, visits count: %d, locations count: %d\n",
usersCount, visitsCount, locationsCount,
)
fmt.Printf("init ok, load time: %f\n", time.Now().Sub(startTime).Seconds())
}
func BenchmarkGetUser(b *testing.B) {
initBench()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := rand.Intn(usersCount + int(usersCount/2))
testStore.GetUser(id)
}
}
func BenchmarkGetVisit(b *testing.B) {
initBench()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := rand.Intn(visitsCount + int(visitsCount/2))
testStore.GetVisit(id)
}
}
func BenchmarkGetLocation(b *testing.B) {
initBench()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := rand.Intn(locationsCount + int(locationsCount/2))
testStore.GetLocation(id)
}
}
func BenchmarkUpdateVisit(b *testing.B) {
initBench()
id := rand.Intn(visitsCount + int(visitsCount/2))
data := proto.Visit{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if rand.Intn(5) == 0 {
data.Location.IsSet = true
data.Location.Val = rand.Intn(locationsCount + int(locationsCount/2))
}
if rand.Intn(5) == 0 {
data.User.IsSet = true
data.User.Val = rand.Intn(usersCount + int(usersCount/2))
}
testStore.UpdateVisit(id, &data)
}
}
func BenchmarkLocationAvg(b *testing.B) {
}