summaryrefslogtreecommitdiff
path: root/test/monitor/gtest_topk.cpp
blob: ec6c2f39fe2857f9615cc1c6881358d5611daf41 (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
#include <assert.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <getopt.h>
#include <sys/time.h>
#include <gtest/gtest.h>
#include <arpa/inet.h>
// #include <fieldstat.h>
#include <fieldstat/fieldstat_exporter.h>

#define MAX_TOPK_CELL_NUM 100
#define MAX_TOPK_METRIC_NUM 255
#define SHOW_TOPK_RESULT_NUM 10
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif

#define TOPK_DIMENSION_NUM 8
static const char *g_topk_dimension[TOPK_DIMENSION_NUM] = {
    "top_client_ip",
    "top_server_ip",
    "top_internal_ip",
    "top_external_ip",
    "top_server_fqdn",
    "top_server_domain",
    "top_client_country",
    "top_server_country",
};

#define TOPK_RANK_BY_NUM 3
static const char *g_topk_rank_by[TOPK_RANK_BY_NUM] = {
    "sessions",
    "packets",
    "bytes",
};

#define TOPK_METRIC_NUM 3
/* index corresponding to g_topk_rank_by[] */
static const char *g_metric_name[TOPK_METRIC_NUM] = {
    "sessions",
    "pkts",
    "bytes",
};
struct topk_t
{
    char name[128];
    long long value;
};

static int g_topk_cubeid[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM] = {};                      // [dimension][rank_by]
static int g_topk_counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM] = {}; // [dimension][rank_by][metric]

static struct fieldstat *g_fs4_ins = NULL;

/* the biggest value index is 0 */
static int qsort_topk_value_cmp_cb(const void *a, const void *b)
{
    struct topk_t *la = (struct topk_t *)a;
    struct topk_t *lb = (struct topk_t *)b;
    return (int)(lb->value - la->value);
}

// lijia temp add for test

static void show_topk_stat(const char *dimension, const char *rank_by, struct topk_t *topk_result, size_t n_result, size_t topk)
{

    if (n_result)
    {
        printf("### top '%s' rank_by '%s' result %zu: \n", dimension, rank_by, topk);
        qsort((void *)topk_result, n_result, sizeof(struct topk_t), qsort_topk_value_cmp_cb);
        int show_num = MIN(topk, n_result);
        for (int i = 0; i < show_num; i++)
        {
            printf("%2d)  %16s\t%8lld\n", i + 1, topk_result[i].name, topk_result[i].value);
        }
    }
}

static int get_topk_cube_id(struct fieldstat *fs4_ins, const char *dimension, const char *rank_by)
{
    struct field find_cube_tmp[2] = {};
    find_cube_tmp[0].key = "name";
    find_cube_tmp[0].type = FIELD_VALUE_CSTRING;
    find_cube_tmp[0].value_str = dimension;
    find_cube_tmp[1].key = "rank_by";
    find_cube_tmp[1].type = FIELD_VALUE_CSTRING;
    find_cube_tmp[1].value_str = rank_by;
    return fieldstat_find_cube(fs4_ins, find_cube_tmp, 2);
}

/*
   dimensions: client_ip, server_ip, internal_ip, external_ip, server_fqdn, server_domain, client_country, server_country
   rank_by: sessions, bytes, packets
   metric: sessions, bytes, packets
*/
static int get_topk_rank_by(struct fieldstat *fs4_ins, const char *dimension, const char *rank_by, const char *metric_name, struct topk_t *topk_array, size_t max_array_num)
{
    int cube_id = get_topk_cube_id(fs4_ins, dimension, rank_by);
    assert(cube_id >= 0);

    int metric_id = fieldstat_cube_get_metric_id_by_name(fs4_ins, cube_id, metric_name);
    assert(metric_id >= 0);

    struct field_list *cell_dimensions_list = NULL;
    size_t n_cell = 0;
    fieldstat_cube_get_cells(fs4_ins, cube_id, &cell_dimensions_list, &n_cell);
    if (NULL == cell_dimensions_list)
    {
        return 0;
    }
    size_t result_min = MIN(max_array_num, n_cell);
    for (size_t ce = 0; ce < result_min; ce++)
    {
        strncpy(topk_array[ce].name, cell_dimensions_list[ce].field->value_str, sizeof(topk_array[ce].name));
        fieldstat_counter_get(fs4_ins, cube_id, &cell_dimensions_list[ce], metric_id, &topk_array[ce].value);
    }
    fieldstat_field_list_arr_free(cell_dimensions_list, n_cell);
    return result_min;
}

static void stm_topk_register_cube(struct fieldstat *fs4_ins, int cube_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM])
{
    struct field reg_cube_tmp[2] = {};
    for (size_t i = 0; i < sizeof(g_topk_dimension) / sizeof(g_topk_dimension[0]); i++)
    {
        for (size_t j = 0; j < sizeof(g_topk_rank_by) / sizeof(g_topk_rank_by[0]); j++)
        {
            reg_cube_tmp[0].key = "name";
            reg_cube_tmp[0].type = FIELD_VALUE_CSTRING;
            reg_cube_tmp[0].value_str = g_topk_dimension[i];
            reg_cube_tmp[1].key = "rank_by";
            reg_cube_tmp[1].type = FIELD_VALUE_CSTRING;
            reg_cube_tmp[1].value_str = g_topk_rank_by[j];
            cube_id[i][j] = fieldstat_cube_create(fs4_ins, reg_cube_tmp, 2);
            assert(cube_id[i][j] >= 0);
        }
    }
}

static void stm_topk_register_counter(struct fieldstat *fs4_ins, int cube_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM],
                                      int counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM])
{
    for (size_t i = 0; i < sizeof(g_topk_dimension) / sizeof(g_topk_dimension[0]); i++)
    {
        for (size_t j = 0; j < sizeof(g_topk_rank_by) / sizeof(g_topk_rank_by[0]); j++)
        {
            for (size_t k = 0; k < sizeof(g_metric_name) / sizeof(char *); k++)
            {
                counter_id[i][j][k] = fieldstat_register_counter(fs4_ins, cube_id[i][j], g_metric_name[k]);
                assert(counter_id[i][j][k] >= 0);
            }
        }
    }
}

static void stm_topk_set_cube_sampling(struct fieldstat *fs4_ins, enum sampling_mode mode, int cube_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM],
                                       int counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM])
{
    for (size_t i = 0; i < sizeof(g_topk_dimension) / sizeof(g_topk_dimension[0]); i++)
    {
        for (size_t j = 0; j < sizeof(g_topk_rank_by) / sizeof(g_topk_rank_by[0]); j++)
        {
            fieldstat_cube_set_sampling(fs4_ins, cube_id[i][j], mode, MAX_TOPK_CELL_NUM, counter_id[i][j][j]);
        }
    }
}

static int stm_topk_client_ip_rankby_sessions(void)
{
    struct field ip_tag = {};
    ip_tag.key = "top_client_ip";
    ip_tag.type = FIELD_VALUE_CSTRING;
    unsigned int begin_ip_addr = 0x04030201;
    struct topk_t benchmark_result[MAX_TOPK_METRIC_NUM];
    long long max_value = 0;
    memset(benchmark_result, 0, sizeof(benchmark_result));
    for (int i = 0; i < MAX_TOPK_METRIC_NUM; i++)
    {
        benchmark_result[i].value = rand() % 10000;
        if (benchmark_result[i].value > max_value)
        {
            max_value = benchmark_result[i].value;
        }
        unsigned int ip_addr_net = htonl(begin_ip_addr + i);
        inet_ntop(AF_INET, &ip_addr_net, benchmark_result[i].name, sizeof(benchmark_result[i].name));
        ip_tag.value_str = benchmark_result[i].name;
        fieldstat_counter_incrby(g_fs4_ins, g_topk_cubeid[0][0], g_topk_counter_id[0][0][0], &ip_tag, 1, benchmark_result[i].value);
    }

    /***  query and verify  ***/
    struct topk_t fs4_topk_result[MAX_TOPK_CELL_NUM] = {};
    int num = get_topk_rank_by(g_fs4_ins, "top_client_ip", "sessions", "sessions", fs4_topk_result, sizeof(fs4_topk_result) / sizeof(struct topk_t));
    assert(num > 0);
    printf("topk top_client_ip rankby sessions, max value:%lld\n", max_value);
    show_topk_stat("top_client_ip", "sessions", fs4_topk_result, num, SHOW_TOPK_RESULT_NUM);
    qsort((void *)benchmark_result, MAX_TOPK_METRIC_NUM, sizeof(struct topk_t), qsort_topk_value_cmp_cb);
    assert(max_value == fs4_topk_result[0].value);
    return memcmp(fs4_topk_result, &benchmark_result[0], SHOW_TOPK_RESULT_NUM);
}

static int stm_topk_server_ip_rankby_packets(void)
{
    struct field ip_tag = {};
    ip_tag.key = "top_server_ip";
    ip_tag.type = FIELD_VALUE_CSTRING;
    unsigned int begin_ip_addr = 0x64640101;
    long long max_value = 0;
    struct topk_t benchmark_result[MAX_TOPK_METRIC_NUM];
    memset(benchmark_result, 0, sizeof(benchmark_result));
    for (int i = 0; i < MAX_TOPK_METRIC_NUM; i++)
    {
        benchmark_result[i].value = rand() % 10000;
        if (benchmark_result[i].value > max_value)
        {
            max_value = benchmark_result[i].value;
        }
        unsigned int ip_addr_net = htonl(begin_ip_addr + i);
        inet_ntop(AF_INET, &ip_addr_net, benchmark_result[i].name, sizeof(benchmark_result[i].name));
        ip_tag.value_str = benchmark_result[i].name;
        fieldstat_counter_incrby(g_fs4_ins, g_topk_cubeid[1][1], g_topk_counter_id[1][1][1], &ip_tag, 1, benchmark_result[i].value);
    }

    /***  query and verify  ***/
    struct topk_t fs4_topk_result[MAX_TOPK_CELL_NUM] = {};
    int num = get_topk_rank_by(g_fs4_ins, "top_server_ip", "packets", "pkts", fs4_topk_result, sizeof(fs4_topk_result) / sizeof(struct topk_t));
    assert(num > 0);
    printf("topk top_server_ip rankby packets, max value:%lld\n", max_value);
    show_topk_stat("top_server_ip", "packets", fs4_topk_result, num, SHOW_TOPK_RESULT_NUM);
    qsort((void *)benchmark_result, MAX_TOPK_METRIC_NUM, sizeof(struct topk_t), qsort_topk_value_cmp_cb);
    assert(max_value == fs4_topk_result[0].value);
    return memcmp(fs4_topk_result, &benchmark_result[0], SHOW_TOPK_RESULT_NUM);
}

static int stm_topk_server_domain_rankby_bytes(void)
{
    struct field domain_tag = {};
    domain_tag.key = "top_server_domain";
    domain_tag.type = FIELD_VALUE_CSTRING;
    struct topk_t benchmark_result[MAX_TOPK_METRIC_NUM];
    memset(benchmark_result, 0, sizeof(benchmark_result));
    for (int i = 0; i < MAX_TOPK_METRIC_NUM; i++)
    {
        snprintf(benchmark_result[i].name, sizeof(benchmark_result[i].name), "www.abcd.com");
    }

    for (int i = 0; i < MAX_TOPK_METRIC_NUM; i++)
    {
        benchmark_result[i].value = rand() % 10000;
        benchmark_result[i].name[4] = rand() % 26 + 'a';
        benchmark_result[i].name[5] = rand() % 26 + 'a';
        benchmark_result[i].name[6] = rand() % 26 + 'a';
        benchmark_result[i].name[7] = rand() % 26 + 'a';
        domain_tag.value_str = benchmark_result[i].name;
        fieldstat_counter_incrby(g_fs4_ins, g_topk_cubeid[5][2], g_topk_counter_id[5][2][2], &domain_tag, 1, benchmark_result[i].value);
    }

    /***  query and verify  ***/
    struct topk_t fs4_topk_result[SHOW_TOPK_RESULT_NUM] = {};
    int num = get_topk_rank_by(g_fs4_ins, "top_server_domain", "bytes", "bytes", fs4_topk_result, sizeof(fs4_topk_result) / sizeof(struct topk_t));
    assert(num > 0);
    show_topk_stat("top_server_domain", "bytes", fs4_topk_result, num, SHOW_TOPK_RESULT_NUM);
    qsort((void *)benchmark_result, MAX_TOPK_METRIC_NUM, sizeof(struct topk_t), qsort_topk_value_cmp_cb);
    return memcmp(fs4_topk_result, &benchmark_result[0], SHOW_TOPK_RESULT_NUM);
}

class MONITOR_TOPK : public testing::Test
{
protected:
    static void SetUpTestCase()
    {
        srand(time(NULL));
        g_fs4_ins = fieldstat_new();
        assert(g_fs4_ins != NULL);

        stm_topk_register_cube(g_fs4_ins, g_topk_cubeid);
        stm_topk_register_counter(g_fs4_ins, g_topk_cubeid, g_topk_counter_id);
        stm_topk_set_cube_sampling(g_fs4_ins, SAMPLING_MODE_TOPK, g_topk_cubeid, g_topk_counter_id);
    }
    static void TearDownTestCase()
    {
        fieldstat_reset(g_fs4_ins);
        fieldstat_free(g_fs4_ins);
    }
};

static void generate_random_counter(struct fieldstat *fs4_ins, int cube_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM],
                                    int counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM])
{
    struct field tag = {};
    tag.type = FIELD_VALUE_CSTRING;
    tag.value_str = "random";

    for (size_t i = 0; i < sizeof(g_topk_dimension) / sizeof(g_topk_dimension[0]); i++)
    {
        for (size_t j = 0; j < sizeof(g_topk_rank_by) / sizeof(g_topk_rank_by[0]); j++)
        {
            for (size_t k = 0; k < sizeof(g_metric_name) / sizeof(char *); k++)
            {
                assert(g_topk_counter_id[i][j][k] >= 0);
                tag.key = g_topk_dimension[i];
                fieldstat_counter_incrby(fs4_ins, cube_id[i][j], counter_id[i][j][k], &tag, 1, random());
            }
        }
    }
}

char **show_topk_get_metrics_name(struct fieldstat *show_fs4_ins, int *metric_array_num)
{
    int *cube_id_array = NULL;
    int n_cube = 0;
    fieldstat_get_cubes(show_fs4_ins, &cube_id_array, &n_cube);
    if (NULL == cube_id_array)
    {
        return NULL;
    }

    struct field_list *cell_dimensions_list = NULL;
    size_t n_cell = 0;
    fieldstat_cube_get_cells(show_fs4_ins, cube_id_array[0], &cell_dimensions_list, &n_cell);
    if (cell_dimensions_list == NULL)
    {
        return NULL;
    }

    /* get metric name  of any valid cube_id */
    int *metric_id_out;
    size_t n_metric;
    char **metric_array = NULL;
    fieldstat_cell_get_metrics(show_fs4_ins, cube_id_array[0], cell_dimensions_list, &metric_id_out, &n_metric);
    if (n_metric == 0)
    {
        fieldstat_field_list_arr_free(cell_dimensions_list, n_cell);
        return NULL;
    }
    metric_array = (char **)malloc(sizeof(char *) * n_metric);
    for (size_t j = 0; j < n_metric; j++)
    {
        metric_array[j] = strdup(fieldstat_metric_get_name(show_fs4_ins, cube_id_array[0], metric_id_out[j]));
    }
    fieldstat_field_list_arr_free(cell_dimensions_list, n_cell);

    *metric_array_num = n_metric;
    return metric_array;
}

TEST(MONITOR, show_all_valid_metrics)
{
    struct fieldstat *show_fs4_ins = fieldstat_new();
    int show_topk_cubeid[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM] = {};                      // [dimension][rank_by]
    int show_topk_counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM] = {}; // [dimension][rank_by][metric]

    stm_topk_register_cube(show_fs4_ins, show_topk_cubeid);
    stm_topk_register_counter(show_fs4_ins, show_topk_cubeid, show_topk_counter_id);
    stm_topk_set_cube_sampling(show_fs4_ins, SAMPLING_MODE_TOPK, show_topk_cubeid, show_topk_counter_id);

    generate_random_counter(show_fs4_ins, show_topk_cubeid, show_topk_counter_id);
    int metric_array_num = 0;
    char **metric_array = show_topk_get_metrics_name(show_fs4_ins, &metric_array_num);
    EXPECT_TRUE(metric_array_num > 0);
    EXPECT_TRUE(metric_array != NULL);

    for (int i = 0; i < metric_array_num; i++)
    {
        printf("metric name: %s\n", metric_array[i]);
        free(metric_array[i]);
    }
    free(metric_array);

    fieldstat_free(show_fs4_ins);
}

static int charchar_array_exist(char **dimension_array, int dimension_array_num, const char *expect_name)
{
    for (int i = 0; i < dimension_array_num && dimension_array[i] != NULL; i++)
    {
        if (0 == strcmp(dimension_array[i], expect_name))
        {
            return 1;
        }
    }
    return 0;
}

char **show_topk_get_dimensions_name(struct fieldstat *show_fs4_ins, int *dimension_array_result)
{
    int *cube_id_array = NULL;
    int n_cube = 0;
    fieldstat_get_cubes(show_fs4_ins, &cube_id_array, &n_cube);
    if (0 == n_cube)
    {
        return NULL;
    }
    char **dimension_array = NULL;
    int dimension_array_num = 0;

    struct field_list *cell_dimensions_list = NULL;
    for (int i = 0; i < n_cube; i++)
    {
        size_t n_cell = 0;
        fieldstat_cube_get_cells(show_fs4_ins, cube_id_array[i], &cell_dimensions_list, &n_cell);
        if (NULL == cell_dimensions_list)
        {
            continue;
        }
        for (size_t j = 0; j < cell_dimensions_list->n_field; j++)
        {
            if (0 == charchar_array_exist(dimension_array, dimension_array_num, cell_dimensions_list->field[j].key))
            {
                dimension_array = (char **)realloc(dimension_array, sizeof(char *) * (dimension_array_num + 1));
                dimension_array[dimension_array_num] = strdup(cell_dimensions_list->field[j].key);
                dimension_array_num++;
            }
        }
        fieldstat_field_list_arr_free(cell_dimensions_list, n_cell);
    }

    *dimension_array_result = dimension_array_num;
    return dimension_array;
}

TEST(MONITOR, show_all_valid_dimensions)
{
    struct fieldstat *show_fs4_ins = fieldstat_new();
    int show_topk_cubeid[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM] = {};                      // [dimension][rank_by]
    int show_topk_counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM] = {}; // [dimension][rank_by][metric]

    stm_topk_register_cube(show_fs4_ins, show_topk_cubeid);
    stm_topk_register_counter(show_fs4_ins, show_topk_cubeid, show_topk_counter_id);
    stm_topk_set_cube_sampling(show_fs4_ins, SAMPLING_MODE_TOPK, show_topk_cubeid, show_topk_counter_id);

    generate_random_counter(show_fs4_ins, show_topk_cubeid, show_topk_counter_id);

    int dimension_array_num = 0;
    char **dimension_array = show_topk_get_dimensions_name(show_fs4_ins, &dimension_array_num);
    EXPECT_TRUE(dimension_array_num > 0);
    EXPECT_TRUE(dimension_array != NULL);

    for (int i = 0; i < dimension_array_num; i++)
    {
        printf("dimension name: %s\n", dimension_array[i]);
        free(dimension_array[i]);
    }
    free(dimension_array);
    fieldstat_free(show_fs4_ins);
}

char **show_topk_get_rankby_name(struct fieldstat *show_fs4_ins, int *rankby_array_result)
{
    int *cube_id_array = NULL;
    int n_cube = 0;
    fieldstat_get_cubes(show_fs4_ins, &cube_id_array, &n_cube);
    if (NULL == cube_id_array)
    {
        return NULL;
    }
    char **rankby_array = NULL;
    int rankby_array_num = 0;

    for (int i = 0; i < n_cube; i++)
    {
        struct field_list *flist = fieldstat_cube_get_dimension(show_fs4_ins, cube_id_array[i]);
        if (flist == NULL)
        {
            continue;
        }
        for (size_t j = 0; j < flist->n_field; j++)
        {
            if (0 == strcmp(flist->field[j].key, "rank_by"))
            {
                if (0 == charchar_array_exist(rankby_array, rankby_array_num, flist->field[j].value_str))
                {
                    rankby_array = (char **)realloc(rankby_array, sizeof(char *) * (rankby_array_num + 1));
                    rankby_array[rankby_array_num] = strdup(flist->field[j].value_str);
                    rankby_array_num++;
                }
            }
        }
        fieldstat_field_list_arr_free(flist, 1);
    }
    *rankby_array_result = rankby_array_num;
    return rankby_array;
}

TEST(MONITOR, show_all_valid_rankby)
{
    struct fieldstat *show_fs4_ins = fieldstat_new();
    int show_topk_cubeid[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM] = {};                      // [dimension][rank_by]
    int show_topk_counter_id[TOPK_DIMENSION_NUM][TOPK_RANK_BY_NUM][TOPK_METRIC_NUM] = {}; // [dimension][rank_by][metric]

    stm_topk_register_cube(show_fs4_ins, show_topk_cubeid);
    stm_topk_register_counter(show_fs4_ins, show_topk_cubeid, show_topk_counter_id);
    stm_topk_set_cube_sampling(show_fs4_ins, SAMPLING_MODE_TOPK, show_topk_cubeid, show_topk_counter_id);

    generate_random_counter(show_fs4_ins, show_topk_cubeid, show_topk_counter_id);

    int rankby_array_num = 0;
    char **rankby_array = show_topk_get_rankby_name(show_fs4_ins, &rankby_array_num);
    EXPECT_TRUE(rankby_array_num > 0);
    EXPECT_TRUE(rankby_array != NULL);

    for (int i = 0; i < rankby_array_num; i++)
    {
        printf("rankby name: %s\n", rankby_array[i]);
        free(rankby_array[i]);
    }
    free(rankby_array);

    fieldstat_free(show_fs4_ins);
}

TEST_F(MONITOR_TOPK, show_client_ip_by_sessions)
{
    EXPECT_EQ(0, stm_topk_client_ip_rankby_sessions());
}

TEST_F(MONITOR_TOPK, show_server_ip_by_packets)
{
    EXPECT_EQ(0, stm_topk_server_ip_rankby_packets());
}

TEST_F(MONITOR_TOPK, show_server_domain_by_bytes)
{
    EXPECT_EQ(0, stm_topk_server_domain_rankby_bytes());
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}