-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcost_analysis_gsum_test.go
More file actions
300 lines (270 loc) · 11.1 KB
/
cost_analysis_gsum_test.go
File metadata and controls
300 lines (270 loc) · 11.1 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
package promsketch
import (
"fmt"
"math"
"strconv"
"testing"
"time"
)
type gsum_ans struct {
distinct float64
l1 float64
entropy float64
l2 float64
}
func gsum(values []float64) (float64, float64, float64, float64) {
m := make(map[float64]int)
n := float64(len(values))
for _, v := range values {
if _, ok := m[v]; !ok {
m[v] = 1
} else {
m[v] += 1
}
}
var l1, l2, entropy float64 = 0, 0, 0
for _, v := range m {
l1 += float64(v)
l2 += float64(v * v)
entropy += float64(v) * math.Log2(float64(v))
}
distinct := float64(len(m))
l2 = math.Sqrt(l2)
entropy = math.Log2(n) - entropy/n
return distinct, l1, entropy, l2
}
func prometheus_gsum(total_length int64, time_window_size int64, t1, t2 []int64, ground_truth *([][]gsum_ans)) (float64, float64, float64) {
input_values := make([]float64, 0)
insert_compute := 0.0
query_compute := 0.0
for t := int64(0); t < total_length; t++ {
start := time.Now()
input_values = append(input_values, cases[0].vec[t].F)
elapsed := time.Since(start)
insert_compute += float64(elapsed.Microseconds())
// evaluate scenarios
if t == total_length-1 {
for i := range len(t1) {
start_t := t1[i] + t - time_window_size + 1
end_t := t2[i] + t - time_window_size + 1
start = time.Now()
values := make([]float64, 0)
for t := start_t; t <= end_t; t++ {
values = append(values, cases[0].vec[t].F)
}
distinct, l1, entropy, l2 := gsum(values)
elapsed = time.Since(start)
query_compute += float64(elapsed.Microseconds())
(*ground_truth)[t][i] = gsum_ans{distinct: distinct, l1: l1, entropy: entropy, l2: l2}
}
} else {
if t >= time_window_size-1 && (t+1)%cost_query_interval == 0 {
for i := range len(t1) {
start_t := t1[i] + t - time_window_size + 1
end_t := t2[i] + t - time_window_size + 1
start = time.Now()
values := make([]float64, 0)
for t := start_t; t <= end_t; t++ {
values = append(values, cases[0].vec[t].F)
}
distinct, l1, entropy, l2 := gsum(values)
elapsed = time.Since(start)
query_compute += float64(elapsed.Microseconds())
(*ground_truth)[t][i] = gsum_ans{distinct: distinct, l1: l1, entropy: entropy, l2: l2}
}
}
}
}
return insert_compute, query_compute * 4, float64(time_window_size) * 8 / 1024 * 4
}
// Test cost (compute + memory) and accuracy under sliding window
func TestCostAnalysisGSum(t *testing.T) {
constructInputTimeSeriesZipf()
fmt.Println("Finished reading input timeseries")
query_window_size := int64(1000000)
total_length := int64(2000000)
// Create a scenario
// Query these subwindows every 1000 data sample insertions
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)
}
fmt.Println("t1:", t1)
fmt.Println("t2:", t2)
ground_truth := make([][]gsum_ans, total_length)
for t := 0; t < int(total_length); t++ {
ground_truth[t] = make([]gsum_ans, len(t1))
}
// Prometheus baseline
insert_compute, query_compute, memory := prometheus_gsum(total_length, query_window_size, t1, t2, &ground_truth)
fmt.Println("Prometheus")
fmt.Println("insert compute:", insert_compute, "us")
fmt.Println("query compute:", query_compute, "us")
fmt.Println("total compute:", query_compute+insert_compute, "us")
fmt.Println("memory:", memory, "KB")
// PromSketch, SHUniv
beta_input := []float64{0.7071, 0.5, 0.3535, 0.25, 0.177, 0.125, 0.0884, 0.0625, 0.044}
for _, beta := range beta_input {
fmt.Println("SHUniv", beta)
shu_distinct_error := make([]float64, 0)
shu_l1_error := make([]float64, 0)
shu_entropy_error := make([]float64, 0)
shu_l2_error := make([]float64, 0)
shu := SmoothInitUnivMon(beta, query_window_size)
total_compute := 0.0
insert_compute := 0.0
for t := int64(0); t < total_length; t++ {
start := time.Now()
shu.Update(t, strconv.FormatFloat(cases[0].vec[t].F, 'f', -1, 64))
elapsed := time.Since(start)
insert_compute += float64(elapsed.Microseconds())
if t == total_length-1 {
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_univ, _ := shu.QueryIntervalMergeUniv(t-end_t, t-start_t, t)
count := float64(merged_univ.GetBucketSize())
distinct := merged_univ.calcCard()
l1 := merged_univ.calcL1()
l2 := merged_univ.calcL2()
l2 = math.Sqrt(l2)
entropy := merged_univ.calcEntropy()
entropy = math.Log2(count) - entropy/count
elapsed := time.Since(start)
total_compute += float64(elapsed.Microseconds())
rel_err := AbsFloat64(ground_truth[t][j].distinct-distinct) / (ground_truth[t][j].distinct) * 100
shu_distinct_error = append(shu_distinct_error, rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l1-l1) / (ground_truth[t][j].l1) * 100
shu_l1_error = append(shu_l1_error, rel_err)
rel_err = AbsFloat64(ground_truth[t][j].entropy-entropy) / (ground_truth[t][j].entropy) * 100
shu_entropy_error = append(shu_entropy_error, rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l2-l2) / (ground_truth[t][j].l2) * 100
shu_l2_error = append(shu_l2_error, rel_err)
}
} else {
if t >= query_window_size-1 && (t+1)%cost_query_interval == 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_univ, _ := shu.QueryIntervalMergeUniv(t-end_t, t-start_t, t)
count := float64(merged_univ.GetBucketSize())
distinct := merged_univ.calcCard()
l1 := merged_univ.calcL1()
l2 := merged_univ.calcL2()
l2 = math.Sqrt(l2)
entropy := merged_univ.calcEntropy()
entropy = math.Log2(count) - entropy/count
elapsed := time.Since(start)
total_compute += float64(elapsed.Microseconds())
rel_err := AbsFloat64(ground_truth[t][j].distinct-distinct) / (ground_truth[t][j].distinct) * 100
shu_distinct_error = append(shu_distinct_error, rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l1-l1) / (ground_truth[t][j].l1) * 100
shu_l1_error = append(shu_l1_error, rel_err)
rel_err = AbsFloat64(ground_truth[t][j].entropy-entropy) / (ground_truth[t][j].entropy) * 100
shu_entropy_error = append(shu_entropy_error, rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l2-l2) / (ground_truth[t][j].l2) * 100
shu_l2_error = append(shu_l2_error, rel_err)
}
}
}
}
fmt.Println("distinct error:", shu_distinct_error)
fmt.Println("l1 error:", shu_l1_error)
fmt.Println("entropy error:", shu_entropy_error)
fmt.Println("l2 error:", shu_l2_error)
fmt.Println("insert compute:", insert_compute)
fmt.Println("query compute:", total_compute, "us")
fmt.Println("total compute:", total_compute+insert_compute, "us")
fmt.Println("memory:", shu.GetMemory(), "KB")
}
// Sampling baselines
sampling_distinct_error := make([][]float64, 0)
sampling_l1_error := make([][]float64, 0)
sampling_entropy_error := make([][]float64, 0)
sampling_l2_error := make([][]float64, 0)
sampling_rate := []float64{0.001, 0.01, 0.05, 0.1, 0.2, 0.3}
for i, rate := range sampling_rate {
fmt.Println("sampling", rate)
query_compute := 0.0
insert_compute := 0.0
sampling_l1_error = append(sampling_l1_error, make([]float64, 0))
sampling_l2_error = append(sampling_l2_error, make([]float64, 0))
sampling_entropy_error = append(sampling_entropy_error, make([]float64, 0))
sampling_distinct_error = append(sampling_distinct_error, make([]float64, 0))
sampling_size := int(float64(query_window_size) * rate)
sampling_instance := NewUniformSampling(query_window_size, rate, sampling_size)
for t := int64(0); t < total_length; t++ {
start := time.Now()
sampling_instance.Insert(t, cases[0].vec[t].F)
elapsed := time.Since(start)
insert_compute += float64(elapsed.Microseconds())
if t == total_length-1 {
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()
l1 := sampling_instance.QueryL1(start_t, end_t)
l2 := sampling_instance.QueryL2(start_t, end_t)
distinct := sampling_instance.QueryDistinct(start_t, end_t)
entropy := sampling_instance.QueryEntropy(start_t, end_t)
elapsed := time.Since(start)
query_compute += float64(elapsed.Microseconds())
rel_err := AbsFloat64(ground_truth[t][j].distinct-distinct) / (ground_truth[t][j].distinct) * 100
sampling_distinct_error[i] = append(sampling_distinct_error[i], rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l1-l1) / (ground_truth[t][j].l1) * 100
sampling_l1_error[i] = append(sampling_l1_error[i], rel_err)
rel_err = AbsFloat64(ground_truth[t][j].entropy-entropy) / (ground_truth[t][j].entropy) * 100
sampling_entropy_error[i] = append(sampling_entropy_error[i], rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l2-l2) / (ground_truth[t][j].l2) * 100
sampling_l2_error[i] = append(sampling_l2_error[i], rel_err)
}
} else {
if t >= query_window_size-1 && (t+1)%cost_query_interval == 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()
l1 := sampling_instance.QueryL1(start_t, end_t)
l2 := sampling_instance.QueryL2(start_t, end_t)
distinct := sampling_instance.QueryDistinct(start_t, end_t)
entropy := sampling_instance.QueryEntropy(start_t, end_t)
elapsed := time.Since(start)
query_compute += float64(elapsed.Microseconds())
rel_err := AbsFloat64(ground_truth[t][j].distinct-distinct) / (ground_truth[t][j].distinct) * 100
sampling_distinct_error[i] = append(sampling_distinct_error[i], rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l1-l1) / (ground_truth[t][j].l1) * 100
sampling_l1_error[i] = append(sampling_l1_error[i], rel_err)
rel_err = AbsFloat64(ground_truth[t][j].entropy-entropy) / (ground_truth[t][j].entropy) * 100
sampling_entropy_error[i] = append(sampling_entropy_error[i], rel_err)
rel_err = AbsFloat64(ground_truth[t][j].l2-l2) / (ground_truth[t][j].l2) * 100
sampling_l2_error[i] = append(sampling_l2_error[i], rel_err)
}
}
}
}
fmt.Println("distinct error:", sampling_distinct_error[i])
fmt.Println("l1 error:", sampling_l1_error[i])
fmt.Println("entropy error:", sampling_entropy_error[i])
fmt.Println("l2 error:", sampling_l2_error[i])
fmt.Println("insert compute:", insert_compute, "us")
fmt.Println("query compute:", query_compute, "us")
fmt.Println("total compute:", query_compute+insert_compute, "us")
fmt.Println("memory:", sampling_instance.GetMemory(), "KB")
}
}