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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
package sfive
import (
"fmt"
"os"
"os/user"
"testing"
"time"
)
var (
__sqlitePath = "file:memdb1?mode=memory&cache=shared"
__boltPath = "/run/s5hub_testing_db.bolt"
)
func TestMain(m *testing.M) {
u, err := user.Current()
if err != nil {
os.Exit(-1)
}
__boltPath = fmt.Sprintf("/run/user/%s/s5hub_testing_db.bolt", u.Uid)
os.Exit(m.Run())
}
func TestAppend(t *testing.T) {
os.Remove(__boltPath)
store, err := NewStore(__sqlitePath, __boltPath)
if err != nil {
t.Errorf("Failed to initialize: %v", err)
return
}
defer store.Close()
startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC)
update := DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: startTime, Duration: 5000}
streamId := StreamId{ContentId: "content", Format: "7bitascii", Quality: QualityHigh}
source := SourceId{Hostname: "localhost", Tags: []string{"tag1", "master"}, StreamId: streamId, Version: 1}
dat := StatisticsData{nil, nil, source, update}
err = store.Append(dat)
if err != nil {
t.Errorf("Failed to append: %v", err)
return
}
}
func TestGetUpdatesAfter(t *testing.T) {
os.Remove(__boltPath)
store, err := NewStore(__sqlitePath, __boltPath)
if err != nil {
t.Errorf("Failed to initialize: %v", err)
return
}
defer store.Close()
startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC)
update := DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: startTime, Duration: 5000}
streamId := StreamId{ContentId: "content", Format: "7bitascii", Quality: QualityHigh}
source := SourceId{Hostname: "localhost", Tags: []string{"tag1", "master"}, StreamId: streamId, Version: 1}
dat := StatisticsData{nil, nil, source, update}
err = store.Append(dat)
if err != nil {
t.Errorf("Failed to append: %v", err)
return
}
res, err := store.GetUpdatesAfter(2)
t.Logf("got updates (err %v):\n%#v", err, res)
}
func generateStatisticsData(n int) (data []StatisticsData) {
hostnames := []string{"streamer1", "streamer2"}
contents := []string{"av", "audio"}
formats := []string{"webm", "flash", "hls"}
qualities := []string{"high", "medium", "low"}
numcombis := len(hostnames) * len(contents) * len(formats) * len(qualities)
clients := []ClientData{
ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400},
ClientData{"10.12.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400},
ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400},
ClientData{"192.168.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400},
ClientData{"172.16.0.2", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}}
starttime := time.Now()
duration := int64(15000)
tags := []string{"2017", "elevate", "discourse"}
for i := 0; i < n; i += numcombis {
for _, hostname := range hostnames {
for _, content := range contents {
for _, format := range formats {
for _, quality := range qualities {
d := StatisticsData{}
d.SourceId.Version = 1
d.SourceId.Hostname = hostname
d.SourceId.Tags = tags
d.SourceId.StreamId.ContentId = content
d.SourceId.StreamId.Format = format
d.SourceId.StreamId.Quality = quality
d.DataUpdate.StartTime = starttime
d.DataUpdate.Duration = duration
d.DataUpdate.Data.ClientCount = uint(len(clients))
d.DataUpdate.Data.BytesSent = 6400 * uint(len(clients))
for _, client := range clients {
c := ClientData{client.Ip, client.UserAgent, client.BytesSent}
d.DataUpdate.Data.Clients = append(d.DataUpdate.Data.Clients, c)
}
data = append(data, d)
}
}
}
}
starttime = starttime.Add(time.Duration(duration) * time.Millisecond)
}
return
}
func BenchmarkAppendMany(b *testing.B) {
os.Remove(__boltPath)
store, err := NewStore(__sqlitePath, __boltPath)
if err != nil {
b.Errorf("Failed to initialize: %v", err)
}
defer store.Close()
data := generateStatisticsData(b.N)
b.ResetTimer()
if err := store.AppendMany(data); err != nil {
b.Errorf("Failed to append: %v", err)
}
}
func BenchmarkGetUpdatesAfter(b *testing.B) {
os.Remove(__boltPath)
store, err := NewStore(__sqlitePath, __boltPath)
if err != nil {
b.Errorf("Failed to initialize: %v", err)
}
defer store.Close()
data := generateStatisticsData(b.N)
if err := store.AppendMany(data); err != nil {
b.Errorf("Failed to append: %v", err)
}
b.ResetTimer()
latestId := -1
for {
updates, err := store.GetUpdatesAfter(latestId)
if err != nil {
b.Errorf("Failed to retrieve: %v", err)
}
if len(updates) == 0 {
break
}
latestId = findMaxId(updates)
}
}
|