summaryrefslogtreecommitdiff
path: root/test/test_fuzz_test.cpp
blob: a97f4164283563b82bc4de00e25d60ee71597139 (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
#include <stdio.h>
#include <gtest/gtest.h>
#include <functional>
#include <unordered_map>
#include <time.h>
#include <random>
#include "fieldstat.h"
#include "fieldstat_exporter.h"
#include "utils.hpp"

using namespace std;

void fill_random_tag_of_length_1_to_3(Fieldstat_tag_list_wrapper *fields[], int tag_list_num)
{
    std::uniform_int_distribution<int> dist(1,100);
    std::mt19937 rng();

    for (int i = 0; i < tag_list_num; i++)
    {
        Fieldstat_tag_list_wrapper *tmp = new Fieldstat_tag_list_wrapper(dist, rand() % 3 + 1);
        tmp->sort_tag_list();
        fields[i] = tmp;
    }
}

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;
    }
}

long long fuzz_fieldstat_counter_get(const struct fieldstat *instance, int cube_id, int metric_id, const struct field_list *tag_list)
{
    long long value = 0;
    int ret = fieldstat_counter_get(instance, cube_id, tag_list, metric_id, &value);
    EXPECT_EQ(ret, 0);
    return value;
}

double fuzz_fieldstat_hll_get(const struct fieldstat *instance, int cube_id, int metric_id, const struct field_list *tag_list)
{
    double value = 0;
    int ret = fieldstat_hll_get(instance, cube_id, tag_list, metric_id, &value);
    EXPECT_EQ(ret, 0);
    return value;
}

TEST(Fuzz_test, many_instance_random_flow_unregister_calibrate_reset_fork_merge_comprehensive)
{
    const int METRIC_NUM = 2;
    const int METRIC_ID_COUNTER = 0;
    const int METRIC_ID_HLL = 1;
    const int CUBE_NUM = 5;
    const int INSTANCE_NUM = 10;
    const int FLOW_NUM = 5000;
    const int CELL_MAX = 5000; // must be no less than FLOW_NUM to ensure an accurate statistics
    const int TEST_ROUND = 100000;
    const int OUT_GAP = 10000;
    const char *metric_name[METRIC_NUM] = {"counter_", "hll_"};
    struct fieldstat *master = fieldstat_new();
    struct fieldstat *replica[INSTANCE_NUM];
    struct fieldstat *dest = 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_create_cube(master, shared_tags[i]->get_tag(), shared_tags[i]->get_tag_count(), SAMPLING_MODE_COMPREHENSIVE, CELL_MAX);
        EXPECT_EQ(cube_id, i);

        fieldstat_register_counter(master, cube_id, metric_name[METRIC_ID_COUNTER]);
        fieldstat_register_hll(master, cube_id, metric_name[METRIC_ID_HLL], 6);
    }

    // all the possible fields
    Fieldstat_tag_list_wrapper *tag_list_wrapper[FLOW_NUM];
    fill_random_tag_of_length_1_to_3(tag_list_wrapper, FLOW_NUM);
    //all the possible operations
    long long rand_nums[TEST_ROUND];
    string *rand_strs[TEST_ROUND] = {NULL};
    for (int i = 0; i < TEST_ROUND; i++) {
        rand_nums[i] = rand() % 1000;
        rand_strs[i] = new string(string("str val") + std::to_string(rand_nums[i]));
    }
    //init instance
    for (int i = 0; i < INSTANCE_NUM; i++) {
        replica[i] = fieldstat_fork(master);
    }
    // for benchmark
    unordered_map<string, int> comp_count;
    unordered_map<string, set<string>> comp_hll;

    clock_t start = clock();
    int next_shared_tag_value = CUBE_NUM;

    for (int i = 0; i < TEST_ROUND; i++) {
        if (i != 0 && i % OUT_GAP == 0) {
            // merge
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_merge(dest, replica[j]);
            }
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_reset(replica[j]);
            }

            // modify master and calibrate
            int cube_id_to_change = rand() % CUBE_NUM;
            Fieldstat_tag_list_wrapper *new_tag = new Fieldstat_tag_list_wrapper("shared_tag", next_shared_tag_value++);
            delete shared_tags[cube_id_to_change];
            shared_tags[cube_id_to_change] = new_tag;
            fieldstat_destroy_cube(master, cube_id_to_change);
            int cube_id_new = fieldstat_create_cube(master, new_tag->get_tag(), new_tag->get_tag_count(), SAMPLING_MODE_COMPREHENSIVE, CELL_MAX);
            fieldstat_register_counter(master, cube_id_new, metric_name[METRIC_ID_COUNTER]);
            fieldstat_register_hll(master, cube_id_new, metric_name[METRIC_ID_HLL], 6);
            EXPECT_EQ(cube_id_new, cube_id_to_change); // should new the cube in the hole leaved by the destroyed cube
            // calibrate
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_calibrate(master, replica[j]);
            }
            
            // check if no merge happens in the last 100 rounds
            if (i + OUT_GAP >= TEST_ROUND) {
                break;
            }
        }
        struct fieldstat *instance = replica[rand() % INSTANCE_NUM]; // the flow randomly goes to one of the instance
        const Fieldstat_tag_list_wrapper * field = tag_list_wrapper[rand() % FLOW_NUM];
        int cube_id = rand() % CUBE_NUM;
        const Fieldstat_tag_list_wrapper *shared_tag = shared_tags[cube_id];

        int ret_add = fieldstat_counter_incrby(instance, cube_id, METRIC_ID_COUNTER, field->get_tag(), field->get_tag_count(), rand_nums[i]);
        if (ret_add == FS_ERR_TOO_MANY_CELLS) {
            continue;
        }
        EXPECT_EQ(ret_add, FS_OK);
        string *val = rand_strs[i];
        ret_add = fieldstat_hll_add(instance, cube_id, METRIC_ID_HLL, field->get_tag(), field->get_tag_count(), val->c_str(), val->size());
        EXPECT_EQ(ret_add, FS_OK);
        string cell_key = shared_tag->to_string() + field->to_string();
        comp_count[cell_key] += rand_nums[i];
        comp_hll[cell_key].insert(*val);
    }

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

    for (int i = 0; i < TEST_ROUND; i++) {
        delete rand_strs[i];
    }
    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];
    }

    long long dummy_ll;
    double dummy_d;
    int *cube_ids;
    int cube_num;
    struct fieldstat *instance_in_focus = dest;
    fieldstat_get_cubes(instance_in_focus, &cube_ids, &cube_num);
    for (int i = 0; i < cube_num; i++) {
        struct field_list *shared_tag_out = fieldstat_cube_get_tags(instance_in_focus, cube_ids[i]);

        size_t cell_num0;
        struct field_list *tags0;
        fieldstat_cube_get_cells(instance_in_focus, cube_ids[i], &tags0, &cell_num0);

        for (size_t j = 0; j < cell_num0; j++) {
            EXPECT_EQ(fieldstat_counter_get(instance_in_focus, cube_ids[i], &tags0[j], METRIC_ID_COUNTER, &dummy_ll), FS_OK);
            EXPECT_EQ(fieldstat_hll_get(instance_in_focus, cube_ids[i], &tags0[j], METRIC_ID_HLL, &dummy_d), FS_OK);
        }

        for (size_t j = 0; j < cell_num0; j++) {
            string tag_str_out = Fieldstat_tag_list_wrapper(&tags0[j]).to_string();
            string cell_key = Fieldstat_tag_list_wrapper(shared_tag_out).to_string() + tag_str_out;
        
            EXPECT_EQ(comp_count[cell_key], fuzz_fieldstat_counter_get(instance_in_focus, cube_ids[i], 0, &tags0[j]));
        }

        fieldstat_tag_list_arr_free(tags0, cell_num0);
        fieldstat_tag_list_arr_free(shared_tag_out, 1);
    }
    free(cube_ids);

    fieldstat_free(master);
    fieldstat_free(dest);
    for (int i = 0; i < INSTANCE_NUM; i++) {
        fieldstat_free(replica[i]);
    }
}

TEST(Fuzz_test, many_instance_random_flow_unregister_calibrate_reset_fork_merge_topk)
{
    const int CUBE_NUM = 5;
    const int INSTANCE_NUM = 10;
    const int FLOW_NUM = 50000;
    const int CELL_MAX = 50;
    const int TEST_ROUND = 100000;
    const int OUT_GAP = 10000;
    struct fieldstat *master = fieldstat_new();
    struct fieldstat *replica[INSTANCE_NUM];
    struct fieldstat *dest = 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_create_cube(master, shared_tags[i]->get_tag(), shared_tags[i]->get_tag_count(), SAMPLING_MODE_TOPK, CELL_MAX);
        EXPECT_EQ(cube_id, i);
        fieldstat_register_counter(master, cube_id, "topk");
    }
    
    // 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[TEST_ROUND];
    for (int i = 0; i < TEST_ROUND; i++) {
        rand_nums[i] = rand() % 1000;
    }
    //init instance
    for (int i = 0; i < INSTANCE_NUM; i++) {
        replica[i] = fieldstat_fork(master);
    }
    // for benchmark
    unordered_map<string, unordered_map<string, int>> count_map; // hte first key is shared field, second key is field

    clock_t start = clock();
    int next_shared_tag_value = CUBE_NUM;

    for (int i = 0; i < TEST_ROUND; i++) {
        if (i != 0 && i % OUT_GAP == 0) {
            // merge
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_merge(dest, replica[j]);
            }
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_reset(replica[j]);
            }

            // modify master and calibrate
            int cube_id_to_change = rand() % CUBE_NUM;
            Fieldstat_tag_list_wrapper *new_tag = new Fieldstat_tag_list_wrapper("shared_tag", next_shared_tag_value++);
            delete shared_tags[cube_id_to_change];
            shared_tags[cube_id_to_change] = new_tag;
            fieldstat_destroy_cube(master, cube_id_to_change);
            int cube_id_new = fieldstat_create_cube(master, new_tag->get_tag(), new_tag->get_tag_count(), SAMPLING_MODE_TOPK, CELL_MAX);
            fieldstat_register_counter(master, cube_id_new, "topk");
            EXPECT_EQ(cube_id_new, cube_id_to_change); // should new the cube in the hole leaved by the destroyed cube
            // calibrate
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_calibrate(master, replica[j]);
            }
            
            // check if no merge happens in the last 100 rounds
            if (i + OUT_GAP >= TEST_ROUND) {
                break;
            }
        }
        struct fieldstat *instance = replica[rand() % INSTANCE_NUM]; // the flow randomly goes to one of the instance
        const Fieldstat_tag_list_wrapper * field = tag_list_wrapper[rand() % FLOW_NUM];
        int cube_id = rand() % CUBE_NUM;
        const Fieldstat_tag_list_wrapper *shared_tag = shared_tags[cube_id];

        int ret_add = fieldstat_counter_incrby(instance, cube_id, 0, field->get_tag(), field->get_tag_count(), rand_nums[i]);
        if (ret_add == FS_ERR_TOO_MANY_CELLS) {
            continue;
        }
        EXPECT_EQ(ret_add, FS_OK);
        count_map[shared_tag->to_string()][field->to_string()] += 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];
    }

    int *cube_ids;
    int cube_num;
    struct fieldstat *instance_in_focus = dest;
    fieldstat_get_cubes(instance_in_focus, &cube_ids, &cube_num);
    for (int i = 0; i < cube_num; i++) {
        struct field_list *shared_tag_out = fieldstat_cube_get_tags(instance_in_focus, cube_ids[i]);

        size_t cell_num;
        struct field_list *fields;
        fieldstat_cube_get_cells(instance_in_focus, cube_ids[i], &fields, &cell_num);

        std::vector<struct Fieldstat_tag_list_wrapper *> test_result;
        for (size_t j = 0; j < cell_num; j++) {
            test_result.push_back(new Fieldstat_tag_list_wrapper(&fields[j]));
        }

        double accuracy = test_cal_topk_accuracy(test_result, count_map[Fieldstat_tag_list_wrapper(shared_tag_out).to_string()]);
        EXPECT_GE(accuracy, 0.95);
        // printf("topk accuracy: %lf\n", accuracy);

        for (size_t j = 0; j < cell_num; j++) {
            delete test_result[j];
        }

        fieldstat_tag_list_arr_free(fields, cell_num);
        fieldstat_tag_list_arr_free(shared_tag_out, 1);
    }
    free(cube_ids);

    fieldstat_free(master);
    fieldstat_free(dest);
    for (int i = 0; i < INSTANCE_NUM; i++) {
        fieldstat_free(replica[i]);
    }
}

TEST(Fuzz_test, many_instance_random_flow_unregister_calibrate_reset_fork_merge_spreadsketch)
{
    const int CUBE_NUM = 5;
    const int INSTANCE_NUM = 10;
    const int CELL_MAX = 50;
    const int TEST_ROUND = 100000;
    const int OUT_GAP = 10000;
    struct fieldstat *master = fieldstat_new();
    struct fieldstat *replica[INSTANCE_NUM];
    struct fieldstat *dest = 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_create_cube(master, shared_tags[i]->get_tag(), shared_tags[i]->get_tag_count(), SAMPLING_MODE_SPREADSKETCH, CELL_MAX);
        EXPECT_EQ(cube_id, i);
        fieldstat_register_hll(master, cube_id, "hll", 6);
    }
    
    //init instance
    for (int i = 0; i < INSTANCE_NUM; i++) {
        replica[i] = fieldstat_fork(master);
    }

    SpreadSketchZipfGenerator generator(1.0, CELL_MAX * 10);
    unordered_map<string, unordered_map<string, int>> count_map; // the first key is cube dimension, second key is cell dimension. value is the fanout(hll return value)

    clock_t start = clock();
    int next_shared_tag_value = CUBE_NUM;

    for (int i = 0; i < TEST_ROUND; i++) {
        if (i != 0 && i % OUT_GAP == 0) {
            // merge
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_merge(dest, replica[j]);
            }
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_reset(replica[j]);
            }

            // modify master and calibrate
            int cube_id_to_change = rand() % CUBE_NUM;
            Fieldstat_tag_list_wrapper *new_tag = new Fieldstat_tag_list_wrapper("shared_tag", next_shared_tag_value++);
            delete shared_tags[cube_id_to_change];
            shared_tags[cube_id_to_change] = new_tag;
            fieldstat_destroy_cube(master, cube_id_to_change);
            int cube_id_new = fieldstat_create_cube(master, new_tag->get_tag(), new_tag->get_tag_count(), SAMPLING_MODE_SPREADSKETCH, CELL_MAX);
            fieldstat_register_hll(master, cube_id_new, "hll", 6);
            EXPECT_EQ(cube_id_new, cube_id_to_change); // should new the cube in the hole leaved by the destroyed cube
            // calibrate
            for (int j = 0; j < INSTANCE_NUM; j++) {
                fieldstat_calibrate(master, replica[j]);
            }
            
            // let merge happens last(no add operation is missed)
            if (i + OUT_GAP >= TEST_ROUND) {
                break;
            }
        }

        // add 
        Flow flow = generator.next();

        struct fieldstat *instance = replica[rand() % INSTANCE_NUM];
        const Fieldstat_tag_list_wrapper cell_dimension("src_ip", flow.src_ip.c_str());
        const Fieldstat_tag_list_wrapper item("dst_ip", flow.dst_ip.c_str());
        int cube_id = rand() % CUBE_NUM;
        const Fieldstat_tag_list_wrapper *shared_tag = shared_tags[cube_id];

        int ret_add = fieldstat_hll_add_field(instance, cube_id, 0, cell_dimension.get_tag(), cell_dimension.get_tag_count(), item.get_tag(), item.get_tag_count());
        if (ret_add == FS_ERR_TOO_MANY_CELLS) {
            continue;
        }
        EXPECT_EQ(ret_add, FS_OK);
        count_map[shared_tag->to_string()][cell_dimension.to_string()] += 1;
    }

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

    for (int i = 0; i < CUBE_NUM; i++) {
        delete shared_tags[i];
    }

    int *cube_ids;
    int cube_num;
    struct fieldstat *instance_in_focus = dest;
    fieldstat_get_cubes(instance_in_focus, &cube_ids, &cube_num);
    for (int i = 0; i < cube_num; i++) {
        struct field_list *shared_tag_out = fieldstat_cube_get_tags(instance_in_focus, cube_ids[i]);

        size_t cell_num;
        struct field_list *cells;
        fieldstat_cube_get_cells(instance_in_focus, cube_ids[i], &cells, &cell_num);

        std::vector<struct Fieldstat_tag_list_wrapper *> test_result;
        for (size_t j = 0; j < cell_num; j++) {
            test_result.push_back(new Fieldstat_tag_list_wrapper(&cells[j]));
        }

        double accuracy = test_cal_topk_accuracy(test_result, count_map[Fieldstat_tag_list_wrapper(shared_tag_out).to_string()]);
        EXPECT_GE(accuracy, 0.7);

        for (size_t j = 0; j < cell_num; j++) {
            delete test_result[j];
        }

        fieldstat_tag_list_arr_free(cells, cell_num);
        fieldstat_tag_list_arr_free(shared_tag_out, 1);
    }
    free(cube_ids);

    fieldstat_free(master);
    fieldstat_free(dest);
    for (int i = 0; i < INSTANCE_NUM; i++) {
        fieldstat_free(replica[i]);
    }
}

// issue: https://jira.geedge.net/browse/TSG-21192
// 在reset后,所有项都是dying 状态,此时添加count = 0 的项,不能正常把dying pop掉,误以为sorted set 已满,出现添加失败(FS_ERR_TOO_MANY_CELLS)但是查不到任何cell 的情况。
TEST(Fuzz_test, add_and_reset_with_randomly_generated_flows_and_randomly_chosen_metric)
{
    const int FLOW_NUM = 50000;
    
    Fieldstat_tag_list_wrapper *tag_list_wrapper[FLOW_NUM];
    fill_with_elephant_flows(tag_list_wrapper, FLOW_NUM);

    struct fieldstat *instance = fieldstat_new();

    int cube_id = fieldstat_create_cube(instance,NULL,0,SAMPLING_MODE_TOPK, 1); // K = 1, just to increase the possibility of FS_ERR_TOO_MANY_CELLS
    int primary_metric_id = fieldstat_register_counter(instance, cube_id, "counter");
    int counter2_id = fieldstat_register_counter(instance, cube_id, "counter2");
    fieldstat_counter_incrby(instance, cube_id, primary_metric_id, tag_list_wrapper[0]->get_tag(), tag_list_wrapper[0]->get_tag_count(), 1);
    fieldstat_counter_incrby(instance, cube_id, counter2_id, tag_list_wrapper[0]->get_tag(), tag_list_wrapper[0]->get_tag_count(), 1);

    struct field_list tag_list_tmp = {NULL, 0};

    for(int i = 0; i < FLOW_NUM; i++) {
        int using_id = rand() % 2 == 0 ? primary_metric_id : counter2_id;

        int ret = fieldstat_counter_incrby(instance, cube_id, using_id, tag_list_wrapper[i]->get_tag(), tag_list_wrapper[i]->get_tag_count(), 1);
        if (ret == FS_ERR_TOO_MANY_CELLS) {
            struct field_list *tag_list = NULL;
            size_t n_cell = 0;
            fieldstat_cube_get_cells(instance, cube_id, &tag_list, &n_cell);
            ASSERT_EQ(n_cell, 1);

            long long value;
            tag_list_tmp.field = (struct field *)tag_list_wrapper[i]->get_tag();
            tag_list_tmp.n_field = tag_list_wrapper[i]->get_tag_count();
            int counter_exist = fieldstat_counter_get(instance, cube_id, &tag_list_tmp, using_id,  &value);
            ASSERT_EQ(counter_exist, FS_ERR_INVALID_TAG); // the field is not added to the cube
            fieldstat_tag_list_arr_free(tag_list, n_cell);
        }

        if (i % 1000 == 0) {
            fieldstat_reset(instance);
        }
    }

    for (int i = 0; i < FLOW_NUM; i++) {
        delete tag_list_wrapper[i];
    }
    fieldstat_free(instance);
}


TEST(Fuzz_test, simple_one_for_perf)
{
    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_create_cube(master, shared_tags[i]->get_tag(), shared_tags[i]->get_tag_count(), SAMPLING_MODE_SPREADSKETCH, CELL_MAX);
        EXPECT_EQ(cube_id, i);
        fieldstat_register_counter(master, cube_id, "topk");
    }
    // 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);
}


int main(int argc, char *argv[]) 
{
	testing::InitGoogleTest(&argc, argv);
    testing::GTEST_FLAG(filter) = "*spreadsketch";
    // testing::GTEST_FLAG(filter) = "-Fuzz_test.simple_one_for_perf";

	return RUN_ALL_TESTS();
}