-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquery_time_quantile_test.go
More file actions
317 lines (280 loc) · 9.96 KB
/
query_time_quantile_test.go
File metadata and controls
317 lines (280 loc) · 9.96 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package promsketch
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"testing"
"time"
)
func readDynamicFloat() {
filename := "./testdata/dynamic_ehkll.txt"
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
vec := make(Vector, 0)
lines := 0
for scanner.Scan() {
if lines == 20000001 {
break
}
splits := strings.Split(scanner.Text(), " ")
F, _ := strconv.ParseFloat(strings.TrimSpace(splits[1]), 64)
T, _ := strconv.ParseFloat(strings.TrimSpace(splits[0]), 64)
vec = append(vec, Sample{T: int64(T), F: F})
lines += 1
}
key := "dynamic"
tmp := TestCase{
key: key,
vec: vec,
}
cases = append(cases, tmp)
}
func readUniformFloat() {
filename := "./testdata/uniform_ehkll.txt"
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
vec := make(Vector, 0)
lines := 0
for scanner.Scan() {
if lines == 10000001 {
break
}
splits := strings.Split(scanner.Text(), " ")
F, _ := strconv.ParseFloat(strings.TrimSpace(splits[1]), 64)
T, _ := strconv.ParseFloat(strings.TrimSpace(splits[0]), 64)
vec = append(vec, Sample{T: int64(T), F: F})
lines += 1
}
key := "uniform"
tmp := TestCase{
key: key,
vec: vec,
}
cases = append(cases, tmp)
}
func readZipfFloat() {
filename := "./testdata/zipf_ehkll.txt"
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
vec := make(Vector, 0)
lines := 0
for scanner.Scan() {
if lines == 20000001 {
break
}
splits := strings.Split(scanner.Text(), " ")
F, _ := strconv.ParseFloat(strings.TrimSpace(splits[1]), 64)
T, _ := strconv.ParseFloat(strings.TrimSpace(splits[0]), 64)
vec = append(vec, Sample{T: int64(T), F: F})
lines += 1
}
key := "zipf"
tmp := TestCase{
key: key,
vec: vec,
}
cases = append(cases, tmp)
}
// example usage:
// go test -v -timeout 0 -run ^TestQueryTimeQuantile$ github.com/zzylol/promsketch -dataset=Zipf
// go test -v -timeout 0 -run ^TestQueryTimeQuantile$ github.com/zzylol/promsketch -dataset=Uniform
// go test -v -timeout 0 -run ^TestQueryTimeQuantile$ github.com/zzylol/promsketch -dataset=Google2019
func TestQueryTimeQuantile(t *testing.T) {
total_length := int64(20000000)
// sliding_window_sizes := []int64{10000, 100000, 1000000, 10000000}
sliding_window_sizes := []int64{1000000}
var dataset_name string = "power"
switch ds := dataset; ds {
case "Power":
readPowerDataset()
dataset_name = "power"
case "Google2019":
readGoogle2019()
dataset_name = "google2019"
case "Google2009":
readGoogleClusterData2009()
dataset_name = "google2009"
case "Zipf":
readZipfFloat()
dataset_name = "zipf"
case "Dynamic":
readDynamicFloat()
dataset_name = "dynamic"
case "Uniform":
readUniformFloat()
dataset_name = "uniform"
}
for test_case := 0; test_case < 5; test_case++ {
filename := "query_time/" + dataset_name + "_20M_quantile_EHKLL_10sampling_" + strconv.Itoa(test_case) + ".txt"
fmt.Println(filename)
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
panic(err)
}
defer f.Close()
w := bufio.NewWriter(f)
for _, query_window_size := range sliding_window_sizes {
if query_window_size > total_length {
break
}
cost_query_interval_quantile := int64(query_window_size / 10)
t1 := make([]int64, 0)
t2 := make([]int64, 0)
t1 = append(t1, int64(0))
t2 = append(t2, query_window_size-1)
for i := 1; i <= 10; i++ {
t1 = append(t1, query_window_size/10*int64(i-1))
t2 = append(t2, query_window_size/10*int64(i)-1)
}
start_t := t1[len(t1)-1]
for i := 1; i <= 10; i++ {
t1 = append(t1, start_t+query_window_size/10/10*int64(i-1))
t2 = append(t2, start_t+query_window_size/10/10*int64(i)-1)
}
start_t = t1[len(t1)-1]
for i := 1; i <= 10; i++ {
t1 = append(t1, start_t+query_window_size/10/10/10*int64(i-1))
t2 = append(t2, start_t+query_window_size/10/10/10*int64(i)-1)
}
t1 = append(t1, query_window_size/3)
t2 = append(t2, query_window_size/3*2)
fmt.Fprintln(w, "t1:", t1)
fmt.Fprintln(w, "t2:", t2)
fmt.Fprintln(w, "sliding window size:", query_window_size)
w.Flush()
k_input := []int64{10, 20, 50, 100, 200, 500, 1000}
kllk_input := []int{64, 128, 256, 512, 1024}
for _, k := range k_input {
for _, kll_k := range kllk_input {
fmt.Fprintln(w, "EHKLL", k, kll_k)
ehkll := ExpoInitKLL(k, kll_k, query_window_size)
sampler := NewUniformSampling(query_window_size, 0.1, int(float64(query_window_size)*0.1))
insert_compute := 0.0
sampling_insert_compute := 0.0
query_time := make([]float64, len(t1))
total_query := make([]int64, len(t1))
gt_query_time := make([]float64, len(t1))
sampling_query_time := make([]float64, len(t1))
kstest_error_ehkll := make([]float64, len(t1))
kstest_error_ehkll2 := make([]float64, len(t1))
kstest_error_sampling := make([]float64, len(t1))
kstest_error_sampling2 := make([]float64, len(t1))
for j := 0; j < len(t1); j++ {
query_time[j] = 0
total_query[j] = 0
gt_query_time[j] = 0
kstest_error_ehkll[j] = 0
kstest_error_sampling[j] = 0
kstest_error_ehkll2[j] = 0
kstest_error_sampling2[j] = 0
}
for t := int64(0); t < total_length; t++ {
start := time.Now()
ehkll.Update(t, cases[0].vec[t].F)
elapsed := time.Since(start)
insert_compute += float64(elapsed.Microseconds())
start = time.Now()
sampler.Insert(t, cases[0].vec[t].F)
elapsed = time.Since(start)
sampling_insert_compute += float64(elapsed.Microseconds())
if t == total_length-1 || (t >= query_window_size-1 && (t+1)%cost_query_interval_quantile == 0) {
for j := range len(t1) {
start_t := t1[j] + t - query_window_size + 1
end_t := t2[j] + t - query_window_size + 1
start := time.Now()
merged_kll := ehkll.QueryIntervalMergeKLL(start_t, end_t)
cdf := merged_kll.CDF()
// sketch_quantile := cdf.Query(0.9)
_ = cdf.Query(0.9)
elapsed := time.Since(start)
query_time[j] += float64(elapsed.Microseconds())
total_query[j] += 1
kstest := KolmogorovSmirnovStatisticKLL(start_t, end_t, merged_kll)
kstest_error_ehkll[j] += kstest
kstest_error_ehkll2[j] += kstest * kstest
start = time.Now()
values := make([]float64, 0)
for tt := start_t; tt <= end_t; tt++ {
values = append(values, float64(cases[0].vec[tt].F))
}
// gt_quantile := quantile(0.9, values)
_ = quantile(0.9, values)
elapsed = time.Since(start)
gt_query_time[j] += float64(elapsed.Microseconds())
start = time.Now()
// sampling_quantile := sampler.QueryQuantile([]float64{0.9}, start_t, end_t)
_ = sampler.QueryQuantile([]float64{0.9}, start_t, end_t)
elapsed = time.Since(start)
sampling_query_time[j] += float64(elapsed.Microseconds())
samples := sampler.GetSamples(start_t, end_t)
kstest_sampling := KolmogorovSmirnovStatisticSampling(start_t, end_t, samples)
kstest_error_sampling[j] += kstest_sampling
kstest_error_sampling2[j] += kstest_sampling * kstest_sampling
/* fmt.Fprintln(w, "errors:", t, j, t2[j]-t1[j]+1,
AbsFloat64(gt_quantile-sampling_quantile[0])/gt_quantile,
AbsFloat64(gt_quantile-sketch_quantile)/gt_quantile)
*/
}
}
}
update_time := float64(insert_compute) / float64(total_length)
sampling_update_time := float64(sampling_insert_compute) / float64(total_length)
total_sketch_query_compute := 0.0
total_gt_query_compute := 0.0
total_sampling_query_compute := 0.0
for j := 0; j < len(t1); j++ {
fmt.Fprintln(w, "sketch avg error:", kstest_error_ehkll[j]/float64(total_query[j]), "window size=", t2[j]-t1[j]+1)
stdvar := kstest_error_ehkll2[j]/float64(total_query[j]) - math.Pow(kstest_error_ehkll[j]/float64(total_query[j]), 2)
stdvar = math.Sqrt(stdvar)
fmt.Fprintln(w, "sketch stdvar error:", stdvar, "window size=", t2[j]-t1[j]+1)
}
for j := 0; j < len(t1); j++ {
fmt.Fprintln(w, "10sampling avg error:", kstest_error_sampling[j]/float64(total_query[j]), "window size=", t2[j]-t1[j]+1)
stdvar := kstest_error_sampling2[j]/float64(total_query[j]) - math.Pow(kstest_error_sampling[j]/float64(total_query[j]), 2)
stdvar = math.Sqrt(stdvar)
fmt.Fprintln(w, "10sampling stdvar error:", stdvar, "window size=", t2[j]-t1[j]+1)
}
for j := 0; j < len(t1); j++ {
fmt.Fprintln(w, "sketch estimate query time=", query_time[j]/float64(total_query[j]), "us", "window size=", t2[j]-t1[j]+1)
total_sketch_query_compute += query_time[j]
}
for j := 0; j < len(t1); j++ {
fmt.Fprintln(w, "sampling query time=", sampling_query_time[j]/float64(total_query[j]), "us", "window size=", t2[j]-t1[j]+1)
total_sampling_query_compute += sampling_query_time[j]
}
for j := 0; j < len(t1); j++ {
fmt.Fprintln(w, "gt query time=", gt_query_time[j]/float64(total_query[j]), "us", "window size=", t2[j]-t1[j]+1)
total_gt_query_compute += gt_query_time[j]
}
fmt.Fprintln(w, "sketch insert compute:", insert_compute, "us")
fmt.Fprintln(w, "sketch update time per item:", update_time, "us")
fmt.Fprintln(w, "sketch query compute:", total_sketch_query_compute, "us")
fmt.Fprintln(w, "samling insert compute:", sampling_insert_compute, "us")
fmt.Fprintln(w, "sampling update time per item:", sampling_update_time, "us")
fmt.Fprintln(w, "sampling query compute:", sampling_query_time, "us")
fmt.Fprintln(w, "gt query compute:", total_gt_query_compute, "us")
fmt.Fprintln(w, "sketch memory:", ehkll.GetMemory(), "KB")
fmt.Fprintln(w, "sampling memory:", sampler.GetMemory(), "KB")
fmt.Fprintln(w, "gt memory:", float64(query_window_size)*8/1024, "KB")
w.Flush()
}
}
}
}
}