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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
#include <gtest/gtest.h>
#include "fieldstat.h"
#include "fieldstat_exporter.h"
#include "utils.hpp"
// /* -------------------------------------------------------------------------- */
// /* merge */
// /* -------------------------------------------------------------------------- */
TEST(test_performance, merge_performance_when_comprehensive_sampling_multi_instance)
{
const int INSTANCE_NUM = 100;
const int MAX_CELL_NUM = 65535;
const int DIMENSION_TOTAL = 100000;
// const int INSTANCE_NUM = 2;
// const int MAX_CELL_NUM = 1000;
// const int DIMENSION_TOTAL = 1024;
Fieldstat_tag_list_wrapper *tags[DIMENSION_TOTAL];
for (int i = 0; i < DIMENSION_TOTAL; i++)
{
tags[i] = new Fieldstat_tag_list_wrapper("my key", i);
}
struct fieldstat *instances[INSTANCE_NUM];
for (int i = 0; i < INSTANCE_NUM; i++) {
struct fieldstat *tmp_i = fieldstat_new();
int cube_id = fieldstat_register_cube(tmp_i, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, MAX_CELL_NUM);
int metric_id = fieldstat_register_counter(tmp_i, cube_id, "metric name", COUNTER_MERGE_BY_SUM);
for (int j = 0; j < MAX_CELL_NUM; j++) {
int cell_id = fieldstat_cube_add(tmp_i, cube_id, tags[rand() % DIMENSION_TOTAL]->get_tag(), 1, 1);
if (cell_id == -1) {
printf("cell_id == -1\n");
continue;
}
fieldstat_counter_incrby(tmp_i, cube_id, metric_id, cell_id, 1);
}
instances[i] = tmp_i;
}
struct fieldstat *instance_dest = fieldstat_new();
printf("prepare done\n");
clock_t start = clock();
// getchar();
for (int i = 0; i < INSTANCE_NUM; i++) {
fieldstat_merge(instance_dest, instances[i]);
}
// exit(0);
clock_t end = clock();
double elapsed_secs = double(end - start) / CLOCKS_PER_SEC;
printf("merge_performance_when_comprehensive_sampling_multi_instance elapsed_secs: %f\n", elapsed_secs);
EXPECT_TRUE(elapsed_secs < 0.1);
fieldstat_free(instance_dest);
for (int i = 0; i < INSTANCE_NUM; i++) {
fieldstat_free(instances[i]);
}
for (int i = 0; i < DIMENSION_TOTAL; i++) {
delete tags[i];
}
}
clock_t perform_merge_test(std::function<void (struct fieldstat*, int, int, int)> metric_add_func,
std::function<int(struct fieldstat*, int)> metric_register_func, enum sampling_mode mode, bool merge_empty_dest)
{
const int MAX_CELL_NUM = 1000;
Fieldstat_tag_list_wrapper *tags[MAX_CELL_NUM];
for (int i = 0; i < MAX_CELL_NUM; i++) {
tags[i] = new Fieldstat_tag_list_wrapper("my key", i);
}
struct fieldstat *instance = fieldstat_new();
int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, mode, MAX_CELL_NUM);
int metric_id = metric_register_func(instance, cube_id);
for (int j = 0; j < MAX_CELL_NUM; j++) {
int cell_id = fieldstat_cube_add(instance, cube_id, tags[j]->get_tag(), 1, 1);
metric_add_func(instance, cube_id, metric_id, cell_id);
}
struct fieldstat *instance_dest = fieldstat_new();
if (!merge_empty_dest) {
fieldstat_merge(instance_dest, instance);
}
clock_t start = clock();
fieldstat_merge(instance_dest, instance);
clock_t end = clock();
fieldstat_free(instance_dest);
fieldstat_free(instance);
for (int i = 0; i < MAX_CELL_NUM; i++) {
delete tags[i];
}
return end - start;
}
TEST(test_performance, merge_performance_one_instance_comprehensive_counter_empty_dest)
{
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE, true);
printf("merge_performance_one_instance_comprehensive_counter_empty_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1000000);
}
TEST(test_performance, merge_performance_one_instance_comprehensive_hll_empty_dest)
{
// int metric_id = fieldstat_register_hll(instance, cube_id, "czz_test hll metric", 10);
// int ret = fieldstat_hll_add(instance, cube_id, metric_id, cell_id, "hello", 5);
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_hll_add(instance, cube_id, metric_id, cell_id, "hello", 5);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_hll(instance, cube_id, "hll metric", 6);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE, true);
printf("merge_performance_one_instance_comprehensive_hll_empty_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1500);
}
TEST(test_performance, merge_performance_one_instance_comprehensive_histogram_empty_dest)
{
// int metric_id = fieldstat_register_hist(instance, cube_id, "czz_test", 1, 100000, 1);
// int ret = fieldstat_hist_record(instance, cube_id, metric_id, cell_id, 1234);
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_hist_record(instance, cube_id, metric_id, cell_id, 1234);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_hist(instance, cube_id, "histogram metric", 1, 100000, 1);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE, true);
printf("merge_performance_one_instance_comprehensive_histogram_empty_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 5000);
}
TEST(test_performance, merge_performance_one_instance_topk_counter_empty_dest)
{
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, rand() % 1000);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_TOPK, true);
printf("merge_performance_one_instance_topk_counter_empty_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1000);
}
TEST(test_performance, merge_performance_one_instance_comprehensive_counter_full_dest)
{
// int metric_id = fieldstat_register_counter(tmp_i, cube_id, "metric name", false);
// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1);
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE, false);
printf("merge_performance_one_instance_comprehensive_counter_full_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1000);
}
TEST(test_performance, merge_performance_one_instance_comprehensive_hll_full_dest)
{
// int metric_id = fieldstat_register_hll(instance, cube_id, "czz_test hll metric", 10);
// int ret = fieldstat_hll_add(instance, cube_id, metric_id, cell_id, "hello", 5);
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_hll_add(instance, cube_id, metric_id, cell_id, "hello", 5);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_hll(instance, cube_id, "hll metric", 6);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE, false);
printf("merge_performance_one_instance_comprehensive_hll_full_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1300);
}
TEST(test_performance, merge_performance_one_instance_comprehensive_histogram_full_dest)
{
// int metric_id = fieldstat_register_hist(instance, cube_id, "czz_test", 1, 600000, 3);
// int ret = fieldstat_hist_record(instance, cube_id, metric_id, cell_id, 1234);
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_hist_record(instance, cube_id, metric_id, cell_id, 1234);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_hist(instance, cube_id, "histogram metric", 1, 100000, 1);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE, false);
printf("merge_performance_one_instance_comprehensive_histogram_full_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 3 * 1000);
}
TEST(test_performance, merge_performance_one_instance_topk_counter_full_dest)
{
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, rand() % 1000);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM);
};
clock_t elapsed = perform_merge_test(metric_add_func, metric_register_func, SAMPLING_MODE_TOPK, false);
printf("merge_performance_one_instance_topk_counter_full_dest elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1500);
}
/* -------------------------------------------------------------------------- */
/* add */
/* -------------------------------------------------------------------------- */
TEST(test_performance, performance_test_add_cells_comprehensive)
{
size_t cell_count = 100000;
struct fieldstat_tag tags[cell_count];
for (size_t i = 0; i < cell_count; i++) {
tags[i] = TEST_TAG_INT;
tags[i].value_longlong = i;
}
// getchar();
struct fieldstat *instance = fieldstat_new();
fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, cell_count);
fieldstat_register_counter(instance, 0, "test", COUNTER_MERGE_BY_SUM);
clock_t start = clock();
for (size_t i = 0; i < cell_count; i++) {
fieldstat_cube_add(instance, 0, &tags[i % cell_count], 1, 1);
}
clock_t end = clock();
double seconds = (double)(end - start) / cell_count;
printf("performance_test_add_cells_comprehensive time cost: %f\n", seconds);
EXPECT_TRUE(seconds < 1);
fieldstat_free(instance);
}
TEST(test_performance, performance_test_add_cells_topk)
{
size_t cell_count = 100000;
struct fieldstat_tag tags[cell_count];
for (size_t i = 0; i < cell_count; i++) {
tags[i] = TEST_TAG_INT;
// tags[i].value_longlong = rand() % 10000;
if (rand()%2)
tags[i].value_longlong = i;
else
tags[i].value_longlong = rand() % 1000;
}
struct fieldstat *instance = fieldstat_new();
fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_TOPK, 1000);
fieldstat_register_counter(instance, 0, "test", COUNTER_MERGE_BY_SUM);
// getchar();
clock_t start = clock();
for (size_t i = 0; i < cell_count; i++) {
fieldstat_cube_add(instance, 0, &tags[i % cell_count], 1, 1);
}
clock_t end = clock();
double seconds = (double)(end - start) / cell_count;
// exit(0);
EXPECT_TRUE(seconds < 1);
printf("performance_test_on_1000_cells_topk_1000000_times time cost: %f\n", seconds);
fieldstat_free(instance);
}
TEST(test_performance, performance_test_add_cells_histogram_record)
{
struct fieldstat *instance = fieldstat_new();
fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
fieldstat_register_hist(instance, 0, "test", 1, 1000000, 3);
int cell_id = fieldstat_cube_add(instance, 0, &TEST_TAG_DOUBLE, 1, 1);
size_t test_num = 100000;
long long vals[test_num];
for (size_t i = 0; i < test_num; i++) {
vals[i] = rand() % 1000000 + 1;
}
clock_t start = clock();
for (size_t i = 0; i < test_num; i++) {
fieldstat_hist_record(instance, 0, 0, cell_id, vals[i]);
}
clock_t end = clock();
double seconds = (double)(end - start) / test_num;
printf("performance_test_add_cells_histogram_record time cost: %f\n", seconds);
EXPECT_TRUE(seconds < 1);
fieldstat_free(instance);
}
TEST(test_performance, performance_test_add_cells_hll_add)
{
struct fieldstat *instance = fieldstat_new();
fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
fieldstat_register_hll(instance, 0, "test", 6);
int cell_id = fieldstat_cube_add(instance, 0, &TEST_TAG_DOUBLE, 1, 1);
size_t test_num = 100000;
std::string vals[test_num];
for (size_t i = 0; i < test_num; i++) {
vals[i] = std::to_string(rand() % 1000000 + 1);
}
clock_t start = clock();
for (size_t i = 0; i < test_num; i++) {
fieldstat_hll_add(instance, 0, 0, cell_id, vals[i].c_str(), vals[i].length());
}
clock_t end = clock();
double seconds = (double)(end - start) / test_num;
printf("performance_test_add_cells_hll_add time cost: %f\n", seconds);
EXPECT_TRUE(seconds < 1);
fieldstat_free(instance);
}
/* -------------------------------------------------------------------------- */
/* export */
/* -------------------------------------------------------------------------- */
using namespace std;
TEST(test_performance, export_many_cells)
{
const int MAX_CELL_NUM = 1000;
const int TAG_NUM = 3000;
const int CUBE_NUM = 10;
const int METRIC_NUM = 10;
Fieldstat_tag_list_wrapper *tags[TAG_NUM];
for (int i = 0; i < TAG_NUM; i++) {
tags[i] = new Fieldstat_tag_list_wrapper("my key", i);
}
struct fieldstat *instance = fieldstat_new();
int cell_id[MAX_CELL_NUM];
for (int i = 0; i < CUBE_NUM; i++) {
Fieldstat_tag_list_wrapper cube_tag("shared key", i);
int cube_id = fieldstat_register_cube(instance, cube_tag.get_tag(), cube_tag.get_tag_count(), SAMPLING_MODE_COMPREHENSIVE, MAX_CELL_NUM);
for (int k = 0; k < MAX_CELL_NUM; k++) {
cell_id[k] = fieldstat_cube_add(instance, cube_id, tags[rand() % TAG_NUM]->get_tag(), 1, 1);
}
for (int j = 0; j < METRIC_NUM; j++) {
string metric_name = "metric name" + to_string(i) + to_string(j);
int metric_id = fieldstat_register_counter(instance, cube_id, metric_name.c_str(), COUNTER_MERGE_BY_SUM);
for (int k = 0; k < MAX_CELL_NUM; k++) {
fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id[k], 1);
}
}
}
struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance);
printf("export_many_cells\n");
// getchar();
clock_t start = clock();
char *json_string = fieldstat_json_exporter_export(fieldstat_json_exporter);
clock_t end = clock();
// exit(0);
free(json_string);
fieldstat_json_exporter_free(fieldstat_json_exporter);
printf("export_many_cells us: %ld\n", end - start);
fieldstat_free(instance);
for (int i = 0; i < TAG_NUM; i++) {
delete tags[i];
}
}
/* -------------------------------------------------------------------------- */
/* serialize */
/* -------------------------------------------------------------------------- */
clock_t perform_serialize_test(std::function<void (struct fieldstat*, int, int, int)> metric_add_func,
std::function<int(struct fieldstat*, int)> metric_register_func, enum sampling_mode mode)
{
const int MAX_CELL_NUM = 100000;
Fieldstat_tag_list_wrapper *tags[MAX_CELL_NUM];
for (int i = 0; i < MAX_CELL_NUM; i++) {
tags[i] = new Fieldstat_tag_list_wrapper("my key", i);
}
struct fieldstat *instance = fieldstat_new();
int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, mode, MAX_CELL_NUM);
int metric_id = metric_register_func(instance, cube_id);
for (int j = 0; j < MAX_CELL_NUM; j++) {
int cell_id = fieldstat_cube_add(instance, cube_id, tags[j]->get_tag(), 1, 1);
metric_add_func(instance, cube_id, metric_id, cell_id);
}
char *blob;
size_t blob_size;
printf("start\n");
clock_t start = clock();
// printf("getchar\n");
// getchar();
fieldstat_serialize(instance, &blob, &blob_size);
// exit(0);
clock_t end = clock();
printf("end\n");
fieldstat_free(instance);
for (int i = 0; i < MAX_CELL_NUM; i++) {
delete tags[i];
}
free(blob);
return end - start;
}
TEST(test_performance, serialize_counter)
{
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM);
};
clock_t elapsed = perform_serialize_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE);
printf("serialize_counter elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 100000); // every one 1us
}
TEST(test_performance, serialize_histogram)
{
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_hist_record(instance, cube_id, metric_id, cell_id, 1234);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_hist(instance, cube_id, "histogram metric", 1, 100000, 1);
};
clock_t elapsed = perform_serialize_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE);
printf("serialize_histogram elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 1000000); // every one 10us
}
TEST(test_performance, serialize_hll)
{
auto metric_add_func = [](struct fieldstat *instance, int cube_id, int metric_id, int cell_id) {
fieldstat_hll_add(instance, cube_id, metric_id, cell_id, "hello", 5);
};
auto metric_register_func = [](struct fieldstat *instance, int cube_id) {
return fieldstat_register_hll(instance, cube_id, "hll metric", 6);
};
clock_t elapsed = perform_serialize_test(metric_add_func, metric_register_func, SAMPLING_MODE_COMPREHENSIVE);
printf("serialize_hll elapsed_secs: %ld\n", elapsed);
EXPECT_TRUE(elapsed < 100000); // every one 1us
}
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|