summaryrefslogtreecommitdiff
path: root/test/perfs.cpp
blob: 908713b215c5fa51f4ff6221f3361b1892820a06 (plain)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include <gtest/gtest.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "utils.hpp"

#include "fieldstat_easy.h"
#include "fieldstat.h"


void fill_with_elephant_flows(Fieldstat_tag_list_wrapper *fields[], int tag_list_num)
{
    for (int i = 0; i < tag_list_num; i++)
    {
        Fieldstat_tag_list_wrapper *tmp;
        int rand_ret = rand() % 10;
        if (rand_ret < 5) {
            tmp = new Fieldstat_tag_list_wrapper("elephant", rand() % 50); // most hit
        } else if (rand_ret == 6 || rand_ret == 7) {
            tmp = new Fieldstat_tag_list_wrapper("mid", rand() % 200);
        } else {
            tmp = new Fieldstat_tag_list_wrapper("mouse", rand() % 10000);
        }
        fields[i] = tmp;
    }
}


TEST(perf, simple_one_for_perf_topk)
{
    const int CUBE_NUM = 5;
    const int FLOW_NUM = 50000;
    const int CELL_MAX = 50;
    const int TEST_ROUND = 500000;
    struct fieldstat *master = fieldstat_new();
    
    Fieldstat_tag_list_wrapper *shared_tags[CUBE_NUM];

    // init cube
    for (int i = 0; i < CUBE_NUM; i++) {
        shared_tags[i] = new Fieldstat_tag_list_wrapper("shared_tag", i);
        int cube_id = fieldstat_cube_create(master, shared_tags[i]->get_tag(), shared_tags[i]->get_tag_count());
        EXPECT_EQ(cube_id, i);
        fieldstat_register_counter(master, cube_id, "topk");
        fieldstat_cube_set_sampling(master, cube_id, SAMPLING_MODE_TOPK, CELL_MAX, 0);
    }
    // init metric
    
    // all the possible fields
    Fieldstat_tag_list_wrapper *tag_list_wrapper[FLOW_NUM];
    fill_with_elephant_flows(tag_list_wrapper, FLOW_NUM);
    //all the possible operations
    long long *rand_nums = new long long[TEST_ROUND];
    for (int i = 0; i < TEST_ROUND; i++) {
        rand_nums[i] = rand() % 1000;
    }

    struct fieldstat *instance = master;

    clock_t start = clock();
    printf("press any key to start v46\n");
    getchar();

    for (int i = 0; i < TEST_ROUND; i++) {
        
        const Fieldstat_tag_list_wrapper * field = tag_list_wrapper[rand() % FLOW_NUM];
        int cube_id = rand() % CUBE_NUM;

        (void)fieldstat_counter_incrby(instance, cube_id, 0, field->get_tag(), field->get_tag_count(), rand_nums[i]);
    }

    clock_t end = clock();
    printf("time: %lf\n", (double)(end - start) / CLOCKS_PER_SEC);

    for (int i = 0; i < FLOW_NUM; i++) {
        delete tag_list_wrapper[i];
    }
    for (int i = 0; i < CUBE_NUM; i++) {
        delete shared_tags[i];
    }
    delete[] rand_nums;
    fieldstat_free(master);
}

TEST(perf, simple_one_for_perf_spreadsketch)
{
    const int CELL_MAX = 100;
    const int TEST_ROUND = 500000;
    struct fieldstat *instance = fieldstat_new();
    
    int cube_id = fieldstat_cube_create(instance, &TEST_FIELD_STRING, 1);
    fieldstat_register_hll(instance, cube_id, "hll", 6);
    fieldstat_cube_set_sampling(instance, cube_id, SAMPLING_MODE_TOP_CARDINALITY, CELL_MAX, 0);

    SpreadSketchZipfGenerator generator(1.0, CELL_MAX * 10);
    Fieldstat_tag_list_wrapper *cell_dimension[TEST_ROUND];
    Fieldstat_tag_list_wrapper *items[TEST_ROUND];
    for (int i = 0; i < TEST_ROUND; i++) {
        Flow flow = generator.next();
        cell_dimension[i] = new Fieldstat_tag_list_wrapper("src_ip", flow.src_ip.c_str());
        items[i] = new Fieldstat_tag_list_wrapper("dst_ip", flow.dst_ip.c_str());
    }

    clock_t start = clock();
    printf("press any key to start \n");
    getchar();

    for (int i = 0; i < TEST_ROUND; i++) {
        fieldstat_hll_add_fields(instance, cube_id, 0, cell_dimension[i]->get_tag(), cell_dimension[i]->get_tag_count(), items[i]->get_field_ptr_array(), items[i]->get_tag_count());
    }

    clock_t end = clock();
    printf("time: %lf second\n", (double)(end - start) / CLOCKS_PER_SEC);

    fieldstat_free(instance);
}

void generate_random_string(char *str, size_t length) {
    static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for (size_t i = 0; i < length - 1; i++) {
        int key = rand() % (int)(sizeof(charset) - 1);
        str[i] = charset[key];
    }
    str[length - 1] = '\0';
}

long long generate_random_integer() {
    return rand();
}

struct field *generate_random_dimensions(enum field_type type, int n_dimensions) {
    struct field *dimensions = (struct field *)malloc(sizeof(struct field) * n_dimensions);
    char key[16];
    int max_length = 16;

    for (int i = 0; i < n_dimensions; i++) {
        
        if (type == FIELD_VALUE_CSTRING) {
            snprintf(key, sizeof(key), "str_%d", i + 1);
            dimensions[i].type = FIELD_VALUE_CSTRING;
            char *random_str = (char *)malloc(max_length);

            generate_random_string(random_str, max_length);
            dimensions[i].value_str = random_str;
        } else if (type == FIELD_VALUE_INTEGER) {
            snprintf(key, sizeof(key), "int_%d", i + 1);
            dimensions[i].type = FIELD_VALUE_INTEGER;
            dimensions[i].value_longlong = rand() % 100; // 随机整数
        }
        dimensions[i].key = strdup(key);
    }

    return dimensions;
}

void free_dimensions(struct field *dimensions, int n_dimensions) {
    for (int i = 0; i < n_dimensions; i++) {
        if (dimensions[i].type == FIELD_VALUE_CSTRING) {
            free((void *)dimensions[i].value_str);
        }
        free((void *)dimensions[i].key);
    }
    free(dimensions);
}

// 初始化fieldstat_easy实例
struct fieldstat_easy *initialize_fieldstat_easy(const char *name, const struct field *global_dimensions, size_t n_global_dimensions, int num_counters, int *counter_ids) {
    struct fieldstat_easy *fse = fieldstat_easy_new(1, name, global_dimensions, n_global_dimensions);

    // 注册计数器
    for (int i = 0; i < num_counters; i++) {
        char metric_name[64];
        snprintf(metric_name, sizeof(metric_name), "counter_%d", i);
        int counter_id = fieldstat_easy_register_counter(fse, metric_name);
        if (counter_id < 0) {
            fprintf(stderr, "Failed to register counter %s\n", metric_name);
            exit(EXIT_FAILURE);
        }
        counter_ids[i] = counter_id;
    }
    return fse;
}

struct fs_instance {
    struct fieldstat *instance;
    struct fieldstat_easy *instance_easy;
};

clock_t g_run_time = 0;
void tested_function(struct fs_instance *instance, int cube_id, int metric_id, const struct field *dimensions, int n_dimensions, long long count) {
    clock_t start = clock();

    if (instance->instance != NULL) {
        fieldstat_counter_incrby(instance->instance, cube_id, metric_id, dimensions, n_dimensions, count);
    } else {
        fieldstat_easy_counter_incrby(instance->instance_easy, 0, metric_id, dimensions, n_dimensions, count);
    }

    clock_t end = clock();
    g_run_time += end - start;
}

void tested_function_batch(struct fs_instance *instance, int cube_id, int metric_ids[], const struct field *dimensions, int n_dimensions, long long counts[], size_t n_metrics) {
    clock_t start = clock();

    if (instance->instance != NULL) {
        fieldstat_counter_incrby_batch(instance->instance, cube_id,  dimensions, n_dimensions, metric_ids, counts, n_metrics);
    } else {
        fieldstat_easy_counter_incrby_batch(instance->instance_easy, 0, metric_ids, dimensions, n_dimensions, counts, n_metrics);
    }

    clock_t end = clock();
    g_run_time += end - start;
}

TEST(perf, complex_instance_group_for_real_case) {
    const int N_LOOP = 50000;

    printf("press any key to start\n");
    getchar();


    // traffic_general_stat
    {
        struct field *global_int_dimension = generate_random_dimensions(FIELD_VALUE_INTEGER, 1);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);
        struct field global_dimensions[4];
        memcpy(&global_dimensions[0], &global_int_dimension[0], sizeof(struct field));
        memcpy(&global_dimensions[1], &global_str_dimensions[0], sizeof(struct field) * 3);

        int num_counters = 13;
        int counter_ids[13];
        struct fieldstat_easy *fse_traffic_general = initialize_fieldstat_easy("traffic_general_stat", global_dimensions, 4, num_counters, counter_ids);
        struct fs_instance instance;
        instance.instance_easy = fse_traffic_general;
        instance.instance = NULL;

        // 在循环中调用counter_incrby
        long long count = rand() % 10000 + 1;
        for (int counter_index = 0; counter_index < num_counters; counter_index++) {
            int metric_id = counter_ids[counter_index];
            for (int iter = 0; iter < N_LOOP; iter++) {
                tested_function(&instance, 0, metric_id, NULL, 0, count);
            }
        }

        fieldstat_easy_free(fse_traffic_general);
        free_dimensions(global_int_dimension, 1);
        free_dimensions(global_str_dimensions, 3);
    }

    // traffic_application_protocol_stat
    {
        struct field *global_int_dimension = generate_random_dimensions(FIELD_VALUE_INTEGER, 1);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);

        // 合并global_dimensions
        struct field global_dimensions[4];
        memcpy(&global_dimensions[0], &global_int_dimension[0], sizeof(struct field));
        memcpy(&global_dimensions[1], &global_str_dimensions[0], sizeof(struct field) * 3);

        int num_counters = 19;
        int counter_ids[19];
        struct fieldstat_easy *fse_traffic_app = initialize_fieldstat_easy("traffic_application_protocol_stat", global_dimensions, 4, num_counters, counter_ids);
        struct fs_instance instance;
        instance.instance_easy = fse_traffic_app;
        instance.instance = NULL;

        // dimensions: string :2
        int n_dimensions = 2;
        struct field *dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, n_dimensions);

        long long count = rand() % 10000 + 1;
        for (int counter_index = 0; counter_index < num_counters; counter_index++) {
            int metric_id = counter_ids[counter_index];
            for (int iter = 0; iter < N_LOOP; iter++) {
                // 随机产生count
                tested_function(&instance, 0, metric_id, dimensions, n_dimensions, count);
            }
        }
        free_dimensions(dimensions, n_dimensions);

        fieldstat_easy_free(fse_traffic_app);
        free_dimensions(global_int_dimension, 1);
        free_dimensions(global_str_dimensions, 3);
    }

    // TRAFFIC-SKETCH-METRIC
    {
        struct field *global_int_dimensions = generate_random_dimensions(FIELD_VALUE_INTEGER, 2);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);

        // 合并global_dimensions
        struct field global_dimensions[5];
        memcpy(&global_dimensions[0], &global_int_dimensions[0], sizeof(struct field) * 2);
        memcpy(&global_dimensions[2], &global_str_dimensions[0], sizeof(struct field) * 3);

        int num_counters = 19;
        int counter_ids[19];
        struct fieldstat_easy *fse_traffic_sketch = initialize_fieldstat_easy("TRAFFIC-SKETCH-METRIC", global_dimensions, 5, num_counters, counter_ids);
        struct fs_instance instance;
        instance.instance_easy = fse_traffic_sketch;
        instance.instance = NULL;

        // dimension: string: [6,16]
        int n_dimensions = 16;

        // 在循环中调用counter_incrby
        long long count = rand() % 10000 + 1;
        int N_ITER_PER_COUNTER = N_LOOP / num_counters;
        for (int counter_index = 0; counter_index < num_counters; counter_index++) {
            int metric_id = counter_ids[counter_index];
            for (int iter = 0; iter < N_ITER_PER_COUNTER; iter++) {
                // 随机生成dimensions的值
                struct field *dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, n_dimensions);

                // 调用counter_incrby
                tested_function(&instance, 0, metric_id, dimensions, n_dimensions, count);

                // 释放dimensions
                free_dimensions(dimensions, n_dimensions);
            }
        }

        free_dimensions(global_int_dimensions, 2);
        free_dimensions(global_str_dimensions, 3);
        fieldstat_easy_free(fse_traffic_sketch);
    }

    // TOPK-METRIC
    {
        struct fieldstat *fs_topk = fieldstat_new();
        struct fs_instance instance;
        instance.instance_easy = NULL;
        instance.instance = fs_topk;

        // global_dimensions: integer :1, string:3
        struct field *global_int_dimension = generate_random_dimensions(FIELD_VALUE_INTEGER, 1);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);
        struct field global_dimensions[4];
        memcpy(&global_dimensions[0], &global_int_dimension[0], sizeof(struct field));
        memcpy(&global_dimensions[1], &global_str_dimensions[0], sizeof(struct field) * 3);

        // 注册cube
        int cube_ids[24];
        int num_cubes = 24;
        for (int i = 0; i < num_cubes; i++) {
            struct field cube_dimension;
            char cube_key[16];
            snprintf(cube_key, sizeof(cube_key), "cube_%d", i + 1);
            cube_dimension.key = cube_key;
            cube_dimension.type = FIELD_VALUE_INTEGER;
            cube_dimension.value_longlong = i + 1;

            int cube_id = fieldstat_cube_create(fs_topk, &cube_dimension, 1);
            if (cube_id < 0) {
                fprintf(stderr, "Failed to create cube %d\n", i);
                exit(EXIT_FAILURE);
            }
            cube_ids[i] = cube_id;
        }

        // 注册metrics
        int metric_ids[7];
        int num_metrics = 7;
        for (int j = 0; j < num_metrics; j++) {
            char metric_name[64];
            snprintf(metric_name, sizeof(metric_name), "counter_%d", j);

            for (int i = 0; i < num_cubes; i++) {
                int cube_id = cube_ids[i];
                int metric_id = fieldstat_register_counter(fs_topk, cube_id, metric_name);
                metric_ids[j] = metric_id;
            }
        }

        // 设置sampling
        for (int i = 0; i < num_cubes; i++) {
            fieldstat_cube_set_sampling(fs_topk, cube_ids[i], SAMPLING_MODE_TOPK, 1000, rand() % num_metrics);
        }

        // 在循环中调用counter_incrby
        long long count = rand() % 10000 + 1;
        for (int iter = 0; iter < N_LOOP; iter++) {
            struct field *cell_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 2); // dimensions: string:2
            for (int cube_index = 0; cube_index < num_cubes; cube_index++) {
                int cube_id = cube_ids[cube_index];
                for (int metric_index = 0; metric_index < num_metrics; metric_index++) {
                    int metric_id = metric_ids[metric_index];
                    
                    tested_function(&instance, cube_id, metric_id, cell_dimensions, 2, count);
                    
                }
            }
            free_dimensions(cell_dimensions, 2);
        }

        fieldstat_free(fs_topk);
        free_dimensions(global_int_dimension, 1);
        free_dimensions(global_str_dimensions, 3);
    }

    // DOS-SKETCH-METRIC
    {
        struct fieldstat *fs_dos = fieldstat_new();
        struct fs_instance instance;
        instance.instance_easy = NULL;
        instance.instance = fs_dos;

        // global_dimensions: integer:2, string:3
        struct field *global_int_dimensions = generate_random_dimensions(FIELD_VALUE_INTEGER, 2);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);

        // 合并global_dimensions
        struct field global_dimensions[5];
        memcpy(&global_dimensions[0], &global_int_dimensions[0], sizeof(struct field) * 2);
        memcpy(&global_dimensions[2], &global_str_dimensions[0], sizeof(struct field) * 3);

        // 创建cube
        int cube_ids[2];
        int num_cubes = 2;
        for (int i = 0; i < num_cubes; i++) {
            struct field cube_dimension;
            char cube_key[16];
            snprintf(cube_key, sizeof(cube_key), "cube_%d", i + 1);
            cube_dimension.key = cube_key;
            cube_dimension.type = FIELD_VALUE_INTEGER;
            cube_dimension.value_longlong = i + 1;

            int cube_id = fieldstat_cube_create(fs_dos, &cube_dimension, 1);
            if (cube_id < 0) {
                fprintf(stderr, "Failed to create cube %d\n", i);
                exit(EXIT_FAILURE);
            }
            cube_ids[i] = cube_id;
        }

        // 注册metrics
        int num_metrics = 3;
        for (int i = 0; i < num_cubes; i++) {
            int cube_id = cube_ids[i];
            for (int j = 0; j < num_metrics; j++) {
                char metric_name[64];
                snprintf(metric_name, sizeof(metric_name), "counter_%d", j);
                int metric_id = fieldstat_register_counter(fs_dos, cube_id, metric_name);
                if (metric_id < 0) {
                    fprintf(stderr, "Failed to register metric %s for cube %d\n", metric_name, cube_id);
                    exit(EXIT_FAILURE);
                }
            }
        }

        // 设置sampling
        for (int i = 0; i < num_cubes; i++) {
            fieldstat_cube_set_sampling(fs_dos, cube_ids[i], SAMPLING_MODE_TOPK, 1000, rand() % num_metrics);
        }

        // 在循环中调用counter_incrby
        int n_dimensions = 5; // dimension: string:  [2,5]

        for (int iter = 0; iter < N_LOOP; iter++) {
            struct field *cell_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, n_dimensions);
            for (int cube_index = 0; cube_index < num_cubes; cube_index++) {
                int cube_id = cube_ids[cube_index];
                for (int metric_index = 0; metric_index < num_metrics; metric_index++) {
                    int metric_id = metric_index;
    
                    long long count = rand() % 10000 + 1;

                    tested_function(&instance, cube_id, metric_id, cell_dimensions, n_dimensions, count);
                }
            }
            free_dimensions(cell_dimensions, n_dimensions);
        }
        free_dimensions(global_int_dimensions, 2);
        free_dimensions(global_str_dimensions, 3);
        fieldstat_free(fs_dos);
    }

    printf("run (%d) loops in %lf seconds\n", N_LOOP, (double)(g_run_time) / CLOCKS_PER_SEC);
}


TEST(perf, complex_instance_group_for_real_case_use_batch) {
    const int N_LOOP = 50000;

    printf("press any key to start\n");
    getchar();

    // traffic_general_stat
    {
        struct field *global_int_dimension = generate_random_dimensions(FIELD_VALUE_INTEGER, 1);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);
        struct field global_dimensions[4];
        memcpy(&global_dimensions[0], &global_int_dimension[0], sizeof(struct field));
        memcpy(&global_dimensions[1], &global_str_dimensions[0], sizeof(struct field) * 3);

        int num_counters = 13;
        int counter_ids[13];
        struct fieldstat_easy *fse_traffic_general = initialize_fieldstat_easy("traffic_general_stat", global_dimensions, 4, num_counters, counter_ids);
        struct fs_instance instance;
        instance.instance_easy = fse_traffic_general;
        instance.instance = NULL;

        long long counts[13];
        for (int i = 0; i < 13; i++) {
            counts[i] = rand() % 10000 + 1;
        }
        for (int iter = 0; iter < N_LOOP; iter++) {
            tested_function_batch(&instance, 0, counter_ids, NULL, 0, counts, 13);
        }

        fieldstat_easy_free(fse_traffic_general);
        free_dimensions(global_int_dimension, 1);
        free_dimensions(global_str_dimensions, 3);
    }

    // traffic_application_protocol_stat
    {
        struct field *global_int_dimension = generate_random_dimensions(FIELD_VALUE_INTEGER, 1);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);

        // 合并global_dimensions
        struct field global_dimensions[4];
        memcpy(&global_dimensions[0], &global_int_dimension[0], sizeof(struct field));
        memcpy(&global_dimensions[1], &global_str_dimensions[0], sizeof(struct field) * 3);

        int num_counters = 19;
        int counter_ids[19];
        struct fieldstat_easy *fse_traffic_app = initialize_fieldstat_easy("traffic_application_protocol_stat", global_dimensions, 4, num_counters, counter_ids);
        struct fs_instance instance;
        instance.instance_easy = fse_traffic_app;
        instance.instance = NULL;

        // dimensions: string :2
        int n_dimensions = 2;
        struct field *dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, n_dimensions);

        long long counts[19];
        for (int i = 0; i < 19; i++) {
            counts[i] = rand() % 10000 + 1;
        }
        for (int iter = 0; iter < N_LOOP; iter++) {
            tested_function_batch(&instance, 0, counter_ids, dimensions, n_dimensions, counts, 19);
        }
        free_dimensions(dimensions, n_dimensions);

        fieldstat_easy_free(fse_traffic_app);
        free_dimensions(global_int_dimension, 1);
        free_dimensions(global_str_dimensions, 3);
    }

    // TRAFFIC-SKETCH-METRIC
    {
        struct field *global_int_dimensions = generate_random_dimensions(FIELD_VALUE_INTEGER, 2);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);

        // 合并global_dimensions
        struct field global_dimensions[5];
        memcpy(&global_dimensions[0], &global_int_dimensions[0], sizeof(struct field) * 2);
        memcpy(&global_dimensions[2], &global_str_dimensions[0], sizeof(struct field) * 3);

        int num_counters = 19;
        int counter_ids[19];
        struct fieldstat_easy *fse_traffic_sketch = initialize_fieldstat_easy("TRAFFIC-SKETCH-METRIC", global_dimensions, 5, num_counters, counter_ids);
        struct fs_instance instance;
        instance.instance_easy = fse_traffic_sketch;
        instance.instance = NULL;

        long long counts[num_counters];
        for (int i = 0; i < num_counters; i++) {
            counts[i] = rand() % 10000 + 1;
        }
        for (int iter = 0; iter < N_LOOP; iter++) {
            struct field *cell_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 16); // dimensions: string:16
            tested_function_batch(&instance, 0, counter_ids, cell_dimensions, 16, counts, num_counters);
            free_dimensions(cell_dimensions, 16);
        }

        free_dimensions(global_int_dimensions, 2);
        free_dimensions(global_str_dimensions, 3);
        fieldstat_easy_free(fse_traffic_sketch);
    }

    // TOPK-METRIC
    {
        struct fieldstat *fs_topk = fieldstat_new();
        struct fs_instance instance;
        instance.instance_easy = NULL;
        instance.instance = fs_topk;

        // global_dimensions: integer :1, string:3
        struct field *global_int_dimension = generate_random_dimensions(FIELD_VALUE_INTEGER, 1);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);
        struct field global_dimensions[4];
        memcpy(&global_dimensions[0], &global_int_dimension[0], sizeof(struct field));
        memcpy(&global_dimensions[1], &global_str_dimensions[0], sizeof(struct field) * 3);

        // 注册cube
        int cube_ids[24];
        int num_cubes = 24;
        for (int i = 0; i < num_cubes; i++) {
            struct field cube_dimension;
            char cube_key[16];
            snprintf(cube_key, sizeof(cube_key), "cube_%d", i + 1);
            cube_dimension.key = cube_key;
            cube_dimension.type = FIELD_VALUE_INTEGER;
            cube_dimension.value_longlong = i + 1;

            int cube_id = fieldstat_cube_create(fs_topk, &cube_dimension, 1);
            if (cube_id < 0) {
                fprintf(stderr, "Failed to create cube %d\n", i);
                exit(EXIT_FAILURE);
            }
            cube_ids[i] = cube_id;
        }

        // 注册metrics
        int metric_ids[7];
        int num_metrics = 7;
        for (int j = 0; j < num_metrics; j++) {
            char metric_name[64];
            snprintf(metric_name, sizeof(metric_name), "counter_%d", j);

            for (int i = 0; i < num_cubes; i++) {
                int cube_id = cube_ids[i];
                int metric_id = fieldstat_register_counter(fs_topk, cube_id, metric_name);
                metric_ids[j] = metric_id;
            }
        }

        // 设置sampling
        for (int i = 0; i < num_cubes; i++) {
            fieldstat_cube_set_sampling(fs_topk, cube_ids[i], SAMPLING_MODE_TOPK, 1000, rand() % num_metrics);
        }

        // 在循环中调用counter_incrby
        long long counts[7];
        for (int i = 0; i < 7; i++) {
            counts[i] = rand() % 10000 + 1;
        }
        for (int iter = 0; iter < N_LOOP; iter++) {
            struct field *cell_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 2); // dimensions: string:2
            for (int cube_index = 0; cube_index < num_cubes; cube_index++) {
                int cube_id = cube_ids[cube_index];
                
                tested_function_batch(&instance, cube_id, metric_ids, cell_dimensions, 2, counts, 7);
            }
            free_dimensions(cell_dimensions, 2);
        }

        fieldstat_free(fs_topk);
        free_dimensions(global_int_dimension, 1);
        free_dimensions(global_str_dimensions, 3);
    }

    // DOS-SKETCH-METRIC
    {
        struct fieldstat *fs_dos = fieldstat_new();
        struct fs_instance instance;
        instance.instance_easy = NULL;
        instance.instance = fs_dos;

        // global_dimensions: integer:2, string:3
        struct field *global_int_dimensions = generate_random_dimensions(FIELD_VALUE_INTEGER, 2);
        struct field *global_str_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, 3);

        // 合并global_dimensions
        struct field global_dimensions[5];
        memcpy(&global_dimensions[0], &global_int_dimensions[0], sizeof(struct field) * 2);
        memcpy(&global_dimensions[2], &global_str_dimensions[0], sizeof(struct field) * 3);

        // 创建cube
        int cube_ids[2];
        int num_cubes = 2;
        for (int i = 0; i < num_cubes; i++) {
            struct field cube_dimension;
            char cube_key[16];
            snprintf(cube_key, sizeof(cube_key), "cube_%d", i + 1);
            cube_dimension.key = cube_key;
            cube_dimension.type = FIELD_VALUE_INTEGER;
            cube_dimension.value_longlong = i + 1;

            int cube_id = fieldstat_cube_create(fs_dos, &cube_dimension, 1);
            if (cube_id < 0) {
                fprintf(stderr, "Failed to create cube %d\n", i);
                exit(EXIT_FAILURE);
            }
            cube_ids[i] = cube_id;
        }

        // 注册metrics
        int num_metrics = 3;
        int metric_ids[3];  
        for (int i = 0; i < num_cubes; i++) {
            int cube_id = cube_ids[i];
            for (int j = 0; j < num_metrics; j++) {
                char metric_name[64];
                snprintf(metric_name, sizeof(metric_name), "counter_%d", j);
                int metric_id = fieldstat_register_counter(fs_dos, cube_id, metric_name);
                if (metric_id < 0) {
                    fprintf(stderr, "Failed to register metric %s for cube %d\n", metric_name, cube_id);
                    exit(EXIT_FAILURE);
                }
                metric_ids[j] = metric_id;
            }
        }

        // 设置sampling
        for (int i = 0; i < num_cubes; i++) {
            fieldstat_cube_set_sampling(fs_dos, cube_ids[i], SAMPLING_MODE_TOPK, 1000, rand() % num_metrics);
        }

        // 在循环中调用counter_incrby
        int n_dimensions = 5; // dimension: string:  [2,5]
        long long counts[3];
        for (int i = 0; i < 3; i++) {
            counts[i] = rand() % 10000 + 1;
        }

        for (int iter = 0; iter < N_LOOP; iter++) {
            struct field *cell_dimensions = generate_random_dimensions(FIELD_VALUE_CSTRING, n_dimensions);
            for (int cube_index = 0; cube_index < num_cubes; cube_index++) {
                int cube_id = cube_ids[cube_index];
                tested_function_batch(&instance, cube_id, metric_ids, cell_dimensions, n_dimensions, counts, 3);
            }
            free_dimensions(cell_dimensions, n_dimensions);
        }
        free_dimensions(global_int_dimensions, 2);
        free_dimensions(global_str_dimensions, 3);
        fieldstat_free(fs_dos);
    }

    printf("run (%d) loops in %lf seconds\n", N_LOOP, (double)(g_run_time) / CLOCKS_PER_SEC);
}

#include "uthash.h" 
struct my_struct {
    int id;                    /* we'll use this field as the key */
    UT_hash_handle hh;         /* makes this structure hashable */
};
TEST(perf, what_will_uthash_do) {
    int loop = 1000;
    struct my_struct *users = NULL;
    struct my_struct *s;
    for (int i = 0; i < loop; i++) {
        s = (struct my_struct*)malloc(sizeof(struct my_struct));
        s->id = i;
        HASH_ADD_INT( users, id, s );
    }

    // 得到内部每个bucket 里的链长度。
    std::vector<int> item_in_bucket;
    
    for (unsigned i = 0; i < users->hh.tbl->num_buckets; i++) {
        int count = 0;

        struct UT_hash_table *tbl = users->hh.tbl;
        struct UT_hash_handle *hh = tbl->buckets[i].hh_head;
        while (hh != NULL) {
            count++;
            hh = hh->hh_next;
        }

        item_in_bucket.push_back(count);
    }

    for (int i = 0; i < item_in_bucket.size(); i++) {
        printf("bucket %d has %d items\n", i, item_in_bucket[i]);
    }
    printf("mean of items in bucket: %lf\n", (double)loop / item_in_bucket.size());
    double std_dev = 0;
    for (int i = 0; i < item_in_bucket.size(); i++) {
        std_dev += (item_in_bucket[i] - loop / item_in_bucket.size()) * (item_in_bucket[i] - loop / item_in_bucket.size());
    }
    std_dev = sqrt(std_dev / item_in_bucket.size());
    printf("std_dev of items in bucket: %lf\n", std_dev);
    int number_of_empty_bucket = 0;
    for (int i = 0; i < item_in_bucket.size(); i++) {
        if (item_in_bucket[i] == 0) {
            number_of_empty_bucket++;
        }
    }
    printf("number of empty bucket: %d\n", number_of_empty_bucket);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    testing::GTEST_FLAG(filter) = "*complex_instance_group_for_real_case";
    // testing::GTEST_FLAG(filter) = "*what_will_uthash_do";

    return RUN_ALL_TESTS();
}