diff options
| author | chenzizhan <[email protected]> | 2023-08-17 14:35:59 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-08-17 14:35:59 +0800 |
| commit | b0dbf651b32c18bdbc343dabe425208560ea5fc1 (patch) | |
| tree | a90c11ea04ccc95ebbabcf56b2d31f44fbba16da | |
| parent | 134a5fc6b83847605b9628124466aaf3c9e398bd (diff) | |
impl with mutex, 3 more tests
| -rw-r--r-- | src/fieldstat.c | 156 | ||||
| -rw-r--r-- | src/metrics/metric.c | 2 | ||||
| -rw-r--r-- | test/CMakeLists.txt | 18 | ||||
| -rw-r--r-- | test/test_merge.cpp | 1436 |
4 files changed, 839 insertions, 773 deletions
diff --git a/src/fieldstat.c b/src/fieldstat.c index acb3c05..9c450cb 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -3,6 +3,7 @@ #include <assert.h> #include <stdlib.h> #include <stdio.h> +#include <pthread.h> #include "cjson/cJSON.h" #include "uthash.h" @@ -42,6 +43,8 @@ struct fieldstat { size_t max_n_cube; struct fs_cube **cube_to_merge; // read only cube, used only in merge + pthread_mutex_t mutex_lock; /* + only a small number of API use this lock, while the others are thread-unsafe. Refer to fieldstat.h to check which API is thread-safe.*/ struct cube_manager *shared_tag_cube_manager; // used only in merge to store shared tags in history }; @@ -56,6 +59,8 @@ struct fieldstat *fieldstat_new() instance->cube_to_merge = calloc(instance->max_n_cube, sizeof(struct fs_cube *)); instance->cube_version = calloc(instance->max_n_cube, sizeof(unsigned long)); + pthread_mutex_init(&instance->mutex_lock, NULL); + return instance; } @@ -75,6 +80,8 @@ void fieldstat_free(struct fieldstat *instance) cube_manager_free(instance->shared_tag_cube_manager); } + pthread_mutex_destroy(&instance->mutex_lock); + free(instance); } @@ -106,7 +113,12 @@ unsigned long fieldstat_get_cell_version(const struct fieldstat *instance) if (instance == NULL) { return 0; } - return instance->cell_version; + + pthread_mutex_lock(&((struct fieldstat *)instance)->mutex_lock); + unsigned long ret = instance->cell_version; + pthread_mutex_unlock(&((struct fieldstat *)instance)->mutex_lock); + + return ret; } int fieldstat_unregister_cube(struct fieldstat *instance, int cube_id) @@ -298,17 +310,24 @@ int fieldstat_cube_add(struct fieldstat *instance, int cube_id, const struct fie if (cube_id < 0 || cube_id >= instance->valid_cube_arr_length) { return -1; } + + pthread_mutex_lock(&instance->mutex_lock); - struct fs_cube *cube = instance->cube[cube_id]; - if (cube == NULL) { + if (instance->cube[cube_id] == NULL) { + pthread_mutex_unlock(&instance->mutex_lock); return -1; } + + struct fs_cube *cube = instance->cube[cube_id]; + if (cube->sampling_mode == SAMPLING_MODE_TOPK && increment <= 0) { + pthread_mutex_unlock(&instance->mutex_lock); return -1; } int ret = -1; struct tag_hash_key *tag_key = tag_hash_key_construct_with_fieldstat_tag(tags, n_tag); + if (cube->sampling_mode == SAMPLING_MODE_COMPREHENSIVE) { ret = cell_manager_add_cell(cube->cell_manager, tag_key); } else { @@ -321,6 +340,7 @@ int fieldstat_cube_add(struct fieldstat *instance, int cube_id, const struct fie } } } + pthread_mutex_unlock(&instance->mutex_lock); tag_hash_key_free(tag_key); @@ -475,45 +495,63 @@ static struct metric *fieldstat_find_metric(const struct fieldstat *instance, in return cube->metrics[metric_id]; } -#define FIELDSTAT_GENERAL_CHECK(instance, cube_id, metric_id, cell_id, metric_type) \ -do { \ - if ((instance) == NULL) { \ - return -1; \ - } \ - if ((cube_id) < 0 || (cube_id) >= (instance)->valid_cube_arr_length) { \ - return -1; \ - } \ - const struct fs_cube *cube = (instance)->cube[(cube_id)]; \ - if (cube == NULL) { \ - return -1; \ - } \ - if ((metric_id) < 0 || (metric_id) >= cube->n_metric) { \ - return -2; \ - } \ - const struct metric *metric = cube->metrics[(metric_id)]; \ - if (metric == NULL || metric_get_type(metric) != (metric_type)) { \ - return -2; \ - } \ - if ((cell_id) < 0 || cell_manager_get_tag_by_cell_id(cube->cell_manager, cell_id) == NULL) { \ - return -3; \ - } \ -} while (0) +int fieldstat_general_check(struct fieldstat *instance, int cube_id, int metric_id, int cell_id, enum metric_type metric_type) +{ + if (instance == NULL) { + return -1; + } + if (cube_id < 0 || cube_id >= instance->valid_cube_arr_length) { + return -1; + } + + struct fs_cube *cube = instance->cube[cube_id]; + if (cube == NULL) { + return -1; + } + if ((metric_id) < 0 || (metric_id) >= cube->n_metric) { + return -2; + } + const struct metric *metric = cube->metrics[(metric_id)]; + if (metric == NULL || metric_get_type(metric) != (metric_type)) { + return -2; + } + if ((cell_id) < 0 || cell_manager_get_tag_by_cell_id(cube->cell_manager, cell_id) == NULL) { + return -3; + } + return 0; +} int fieldstat_counter_incrby(struct fieldstat *instance, int cube_id, int metric_id, int cell_id, long long increment) { - FIELDSTAT_GENERAL_CHECK(instance, cube_id, metric_id, cell_id, METRIC_TYPE_COUNTER); - struct metric *metric = instance->cube[cube_id]->metrics[metric_id]; + pthread_mutex_lock(&(instance->mutex_lock)); + + int ret = fieldstat_general_check(instance, cube_id, metric_id, cell_id, METRIC_TYPE_COUNTER); + if (ret < 0) { + pthread_mutex_unlock(&(instance->mutex_lock)); + return ret; + } + struct metric *metric = instance->cube[cube_id]->metrics[metric_id]; metric_counter_incrby(metric, cell_id, increment); + + pthread_mutex_unlock(&(instance->mutex_lock)); return 0; } int fieldstat_counter_set(struct fieldstat *instance, int cube_id, int metric_id, int cell_id, long long value) { - FIELDSTAT_GENERAL_CHECK(instance, cube_id, metric_id, cell_id, METRIC_TYPE_COUNTER); + pthread_mutex_lock(&(instance->mutex_lock)); + + int ret = fieldstat_general_check(instance, cube_id, metric_id, cell_id, METRIC_TYPE_COUNTER); + if (ret < 0) { + pthread_mutex_unlock(&(instance->mutex_lock)); + return ret; + } struct metric *metric = instance->cube[cube_id]->metrics[metric_id]; + ret = metric_counter_set(metric, cell_id, value); + + pthread_mutex_unlock(&(instance->mutex_lock)); - int ret = metric_counter_set(metric, cell_id, value); if (ret < 0) { return -4; } @@ -522,26 +560,40 @@ int fieldstat_counter_set(struct fieldstat *instance, int cube_id, int metric_id int fieldstat_hll_add(struct fieldstat *instance, int cube_id, int metric_id, int cell_id, const char *key, size_t key_len) { - FIELDSTAT_GENERAL_CHECK(instance, cube_id, metric_id, cell_id, METRIC_TYPE_HLL); + pthread_mutex_lock(&(instance->mutex_lock)); + int ret = fieldstat_general_check(instance, cube_id, metric_id, cell_id, METRIC_TYPE_HLL); + if (ret < 0) { + pthread_mutex_unlock(&(instance->mutex_lock)); + return ret; + } + struct metric *metric = instance->cube[cube_id]->metrics[metric_id]; metric_hll_add(metric, cell_id, key, key_len); + pthread_mutex_unlock(&(instance->mutex_lock)); return 0; } int fieldstat_hist_record(struct fieldstat *instance, int cube_id, int metric_id, int cell_id, long long value) { - FIELDSTAT_GENERAL_CHECK(instance, cube_id, metric_id, cell_id, METRIC_TYPE_HISTOGRAM); + pthread_mutex_lock(&(instance->mutex_lock)); + + int ret = fieldstat_general_check(instance, cube_id, metric_id, cell_id, METRIC_TYPE_HISTOGRAM); + if (ret < 0) { + pthread_mutex_unlock(&(instance->mutex_lock)); + return ret; + } + struct metric *metric = instance->cube[cube_id]->metrics[metric_id]; - int ret = metric_histogram_record(metric, cell_id, value); + ret = metric_histogram_record(metric, cell_id, value); + pthread_mutex_unlock(&(instance->mutex_lock)); if (ret < 0) { return -4; } return 0; } - // /* -------------------------------------------------------------------------- */ // /* serialization */ // /* -------------------------------------------------------------------------- */ @@ -686,7 +738,6 @@ struct fieldstat *fieldstat_deserialize(const char *blob, size_t blob_size) struct fs_cube *fieldstat_cube_copy(const struct fs_cube *cube) { - printf("fieldstat_cube_copy\n"); struct fs_cube *cube_dup = fieldstat_cube_info_init(cube->shared_tags, cube->n_shared_tags, cube->sampling_mode, cube->max_n_cell); const struct cell_manager *cm_src = cube->cell_manager; @@ -710,13 +761,12 @@ void fieldstat_cube_merge_comprehensive(struct fs_cube *dest, const struct fs_cu for (int cell_id_src = 0; cell_id_src < arr_len_src; cell_id_src++) { const struct tag_hash_key *tag_src = tag_arr_src[cell_id_src]; if (tag_src == NULL) { - printf("tag_src is NULL\n"); + printf("ERR: tag_src is NULL\n"); continue; } int cell_id_final = cell_manager_add_cell(cell_manager_dest, tag_src); if (cell_id_final == -1) { // dest is full - printf("dest is full\n"); break; } for (int metric_id_src = 0; metric_id_src < src->n_metric; metric_id_src++) { @@ -1165,19 +1215,16 @@ int fieldstat_merge_and_reset(struct fieldstat *instance, struct fieldstat *src) // 总之会很麻烦,先不做了 // 也不是特别麻烦,实际上就是把reset改成dup,然后把reset放到最后。 - struct fs_cube **next_cube_WO = (struct fs_cube **)calloc(instance->valid_cube_arr_length, sizeof(struct fs_cube *)); - for(int i = 0; i < src->valid_cube_arr_length; i++) { + size_t n_cube_src = src->valid_cube_arr_length; + struct fs_cube **next_cube_WO = (struct fs_cube **)calloc(n_cube_src, sizeof(struct fs_cube *)); + for(int i = 0; i < n_cube_src; i++) { if (src->cube_to_merge[i] == NULL) { continue; } next_cube_WO[i] = fieldstat_cube_dup(src->cube_to_merge[i]); } - /* - here we cannot simply let src->cube_to_merge = src->cube only before merge. Because src->cube is always Write only, so we have to keep a copy of it, which is src->cube_to_merge, - or the previous initialization of next_cube_WO will be impossible. - free src->cube_to_merge before letting it point to src->cube. - */ + // free src->cube_to_merge before letting it point to src->cube for(int i = 0; i < src->valid_cube_arr_length; i++) { if (src->cube_to_merge[i] == NULL) { continue; @@ -1186,24 +1233,13 @@ int fieldstat_merge_and_reset(struct fieldstat *instance, struct fieldstat *src) } free(src->cube_to_merge); - // todo: 加锁 - - (void)__sync_lock_test_and_set(&src->cube_to_merge, src->cube); // src->cube is empty now - // src may also serve as a data record, so we need to do atomic operation for src instead of src->cube = next_cube_WO - (void)__sync_lock_test_and_set(&src->cube, next_cube_WO); // src->cube is empty now - src->cell_version++; // src is reset - // todo: 解锁 - - + pthread_mutex_lock(&src->mutex_lock); + src->cube_to_merge = src->cube; + src->cube = next_cube_WO; + src->cell_version++; // src is reset, so version+1 + pthread_mutex_unlock(&src->mutex_lock); int ret = fieldstat_merge_inner(instance, src); - // for(int i = 0; i < src->valid_cube_arr_length; i++) { - // if (src->cube_to_merge[i] == NULL) { - // continue; - // } - // fieldstat_cube_reset(src->cube_to_merge[i]); - // } - return ret; }
\ No newline at end of file diff --git a/src/metrics/metric.c b/src/metrics/metric.c index d7b254e..d66fafe 100644 --- a/src/metrics/metric.c +++ b/src/metrics/metric.c @@ -166,7 +166,6 @@ static int metric_scheme_counter_merge(struct metric_measure_data *pthis, const } switch (counter->mode) { case COUNTER_MERGE_BY_SUM: - printf("merge counter, from: %lld, to: %lld\n", from_counter->value, counter->value); counter->value += from_counter->value; break; case COUNTER_MERGE_BY_MAX: @@ -683,7 +682,6 @@ struct metric *metric_deserialize(const char *blob, size_t blob_size) int metric_merge_or_copy_cell(struct metric *dest, const struct metric *src, int dest_cell_id, int src_cell_id) { - printf("metric_merge_or_copy_cell\n"); struct metric_measure_data *src_data = metric_find_one_cell(src, src_cell_id); if (src_data == NULL) { return 1; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0f7830f..b86bfa1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -46,13 +46,13 @@ function (add_unit_test file_name) set_property(TARGET ${file_name} PROPERTY CXX_STANDARD 17)
endfunction()
-# add_unit_test(test_exporter_json)
-# add_unit_test(test_fuzz_test)
+add_unit_test(test_exporter_json)
+add_unit_test(test_fuzz_test)
add_unit_test(test_merge)
-# add_unit_test(test_metric_counter)
-# add_unit_test(test_metric_histogram)
-# add_unit_test(test_metric_hll)
-# add_unit_test(test_performance)
-# add_unit_test(test_register_and_reset)
-# add_unit_test(test_serialize)
-# add_unit_test(unit_test_cell_manager)
\ No newline at end of file +add_unit_test(test_metric_counter)
+add_unit_test(test_metric_histogram)
+add_unit_test(test_metric_hll)
+add_unit_test(test_performance)
+add_unit_test(test_register_and_reset)
+add_unit_test(test_serialize)
+add_unit_test(unit_test_cell_manager)
\ No newline at end of file diff --git a/test/test_merge.cpp b/test/test_merge.cpp index 715849e..16d1f20 100644 --- a/test/test_merge.cpp +++ b/test/test_merge.cpp @@ -32,853 +32,885 @@ double test_cal_accuracy_given_expected_key(vector<struct Fieldstat_tag_list_wra return test_cal_topk_accuracy(test_result, countMap); } -// TEST(unit_test_merge, test_metric_name_mapping_with_new_metric_on_existing_cube) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id1 = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// int metric_id_1_0 = fieldstat_register_counter(instance, cube_id1, "metric_name cube1 cube2", COUNTER_MERGE_BY_SUM); -// int cell_id_1_0 = fieldstat_cube_add(instance, cube_id1, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id1, metric_id_1_0, cell_id_1_0, 1); -// int metric_id_1_1 = fieldstat_register_counter(instance, cube_id1, "shared name", COUNTER_MERGE_BY_SUM); -// int cell_id_1_1 = fieldstat_cube_add(instance, cube_id1, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id1, metric_id_1_1, cell_id_1_1, 2); -// int cube_id2 = fieldstat_register_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// int metric_id_2_0 = fieldstat_register_counter(instance, cube_id2, "metric_name cube1 cube2", COUNTER_MERGE_BY_SUM); -// int cell_id_2_0 = fieldstat_cube_add(instance, cube_id2, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id2, metric_id_2_0, cell_id_2_0, 3); +TEST(unit_test_merge, test_metric_name_mapping_with_new_metric_on_existing_cube) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id1 = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + int metric_id_1_0 = fieldstat_register_counter(instance, cube_id1, "metric_name cube1 cube2", COUNTER_MERGE_BY_SUM); + int cell_id_1_0 = fieldstat_cube_add(instance, cube_id1, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id1, metric_id_1_0, cell_id_1_0, 1); + int metric_id_1_1 = fieldstat_register_counter(instance, cube_id1, "shared name", COUNTER_MERGE_BY_SUM); + int cell_id_1_1 = fieldstat_cube_add(instance, cube_id1, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id1, metric_id_1_1, cell_id_1_1, 2); + int cube_id2 = fieldstat_register_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + int metric_id_2_0 = fieldstat_register_counter(instance, cube_id2, "metric_name cube1 cube2", COUNTER_MERGE_BY_SUM); + int cell_id_2_0 = fieldstat_cube_add(instance, cube_id2, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id2, metric_id_2_0, cell_id_2_0, 3); -// struct fieldstat *instance_dest = fieldstat_new(); -// int cube_id_dest = fieldstat_register_cube(instance_dest, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// (void)fieldstat_register_counter(instance_dest, cube_id_dest, "shared name", COUNTER_MERGE_BY_SUM); + struct fieldstat *instance_dest = fieldstat_new(); + int cube_id_dest = fieldstat_register_cube(instance_dest, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + (void)fieldstat_register_counter(instance_dest, cube_id_dest, "shared name", COUNTER_MERGE_BY_SUM); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id, &n_cube); -// EXPECT_TRUE(n_cube == 2); -// EXPECT_TRUE(cube_id[0] == cube_id_dest); + int *cube_id; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id, &n_cube); + EXPECT_TRUE(n_cube == 2); + EXPECT_TRUE(cube_id[0] == cube_id_dest); -// EXPECT_EQ(fieldstat_get_max_metric_id(instance_dest, cube_id[0]) + 1, 2); -// EXPECT_EQ(fieldstat_get_max_metric_id(instance_dest, cube_id[1]) + 1, 1); + EXPECT_EQ(fieldstat_get_max_metric_id(instance_dest, cube_id[0]) + 1, 2); + EXPECT_EQ(fieldstat_get_max_metric_id(instance_dest, cube_id[1]) + 1, 1); -// EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest, 0), "shared name"); -// EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest, 1), "metric_name cube1 cube2"); -// EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id[1], 0), "metric_name cube1 cube2"); + EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest, 0), "shared name"); + EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest, 1), "metric_name cube1 cube2"); + EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id[1], 0), "metric_name cube1 cube2"); -// EXPECT_EQ(fieldstat_counter_get(instance_dest, cube_id_dest, 0, 0), 2); // shared name -// EXPECT_EQ(fieldstat_counter_get(instance_dest, cube_id_dest, 1, 0), 1); // metric_name cube1 cube2 on cube1 -// EXPECT_EQ(fieldstat_counter_get(instance_dest, cube_id[1], 0, 0), 3); // metric_name cube1 cube2 on cube2 + EXPECT_EQ(fieldstat_counter_get(instance_dest, cube_id_dest, 0, 0), 2); // shared name + EXPECT_EQ(fieldstat_counter_get(instance_dest, cube_id_dest, 1, 0), 1); // metric_name cube1 cube2 on cube1 + EXPECT_EQ(fieldstat_counter_get(instance_dest, cube_id[1], 0, 0), 3); // metric_name cube1 cube2 on cube2 -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(cube_id); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(cube_id); +} -// TEST(unit_test_merge, cube_shared_tag_mapping_with_new_cube) -// { -// struct fieldstat *instance = fieldstat_new(); -// (void)fieldstat_register_cube(instance, &TEST_TAG_DOUBLE, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// int cube_id2 = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// fieldstat_register_counter(instance, cube_id2, "metric in cube 2", COUNTER_MERGE_BY_SUM); -// struct fieldstat *instance_dest = fieldstat_new(); -// int cube_id_dest = fieldstat_register_cube(instance_dest, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); +TEST(unit_test_merge, cube_shared_tag_mapping_with_new_cube) +{ + struct fieldstat *instance = fieldstat_new(); + (void)fieldstat_register_cube(instance, &TEST_TAG_DOUBLE, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + int cube_id2 = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + fieldstat_register_counter(instance, cube_id2, "metric in cube 2", COUNTER_MERGE_BY_SUM); + struct fieldstat *instance_dest = fieldstat_new(); + int cube_id_dest = fieldstat_register_cube(instance_dest, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id, &n_cube); -// EXPECT_TRUE(n_cube == 2); -// EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest, 0), "metric in cube 2"); + int *cube_id; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id, &n_cube); + EXPECT_TRUE(n_cube == 2); + EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest, 0), "metric in cube 2"); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(cube_id); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(cube_id); +} -// TEST(unit_test_merge, empty_instance) -// { -// struct fieldstat *instance = fieldstat_new(); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); +TEST(unit_test_merge, empty_instance) +{ + struct fieldstat *instance = fieldstat_new(); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); -// int *cube_id; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id, &n_cube); -// EXPECT_TRUE(n_cube == 0); + int *cube_id; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id, &n_cube); + EXPECT_TRUE(n_cube == 0); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); +} -// TEST(unit_test_merge, new_cube_and_metric_to_empty_comprehensive) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); +TEST(unit_test_merge, new_cube_and_metric_to_empty_comprehensive) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); -// struct fieldstat *instance_dest = fieldstat_new(); + struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id_dest; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); -// EXPECT_TRUE(n_cube == 1); -// EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest[0], 0), "metric_name"); + int *cube_id_dest; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); + EXPECT_TRUE(n_cube == 1); + EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest[0], 0), "metric_name"); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(cube_id_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(cube_id_dest); +} -// TEST(unit_test_merge, new_cell_on_existing_cube_and_metric_comprehensive) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); +TEST(unit_test_merge, new_cell_on_existing_cube_and_metric_comprehensive) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); -// int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 10086); + int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 10086); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id_dest; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); -// EXPECT_TRUE(n_cube == 1); -// int ret_cube_id = cube_id_dest[0]; -// free(cube_id_dest); -// int ret_metric_id = fieldstat_get_max_metric_id(instance, ret_cube_id); -// EXPECT_EQ(ret_metric_id, 0); -// long long measure = fieldstat_counter_get(instance, cube_id, metric_id, cell_id); -// EXPECT_EQ(measure, 10086); + int *cube_id_dest; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); + EXPECT_TRUE(n_cube == 1); + int ret_cube_id = cube_id_dest[0]; + free(cube_id_dest); + int ret_metric_id = fieldstat_get_max_metric_id(instance, ret_cube_id); + EXPECT_EQ(ret_metric_id, 0); + long long measure = fieldstat_counter_get(instance, cube_id, metric_id, cell_id); + EXPECT_EQ(measure, 10086); -// int *ret_cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance, cube_id, metric_id, &ret_cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// EXPECT_EQ(tag_list->n_tag, 1); -// EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); + int *ret_cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance, cube_id, metric_id, &ret_cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_EQ(tag_list->n_tag, 1); + EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(ret_cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(ret_cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); +} -// TEST(unit_test_merge, merge_existing_cell_on_existing_cube_and_metric_comprehensive) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); -// int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 5); -// struct fieldstat *instance_dest = fieldstat_new(); +TEST(unit_test_merge, merge_existing_cell_on_existing_cube_and_metric_comprehensive) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); + int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 5); + struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); -// fieldstat_merge(instance_dest, instance); - -// int *cube_id_dest; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); -// EXPECT_TRUE(n_cube == 1); -// int ret_cube_id = cube_id_dest[0]; -// free(cube_id_dest); -// int ret_metric_id = fieldstat_get_max_metric_id(instance_dest, ret_cube_id); -// EXPECT_EQ(ret_metric_id, 0); -// long long measure = fieldstat_counter_get(instance_dest, cube_id, metric_id, 0); -// EXPECT_EQ(measure, 10); - -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); + + int *cube_id_dest; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); + EXPECT_TRUE(n_cube == 1); + int ret_cube_id = cube_id_dest[0]; + free(cube_id_dest); + int ret_metric_id = fieldstat_get_max_metric_id(instance_dest, ret_cube_id); + EXPECT_EQ(ret_metric_id, 0); + long long measure = fieldstat_counter_get(instance_dest, cube_id, metric_id, 0); + EXPECT_EQ(measure, 10); + + fieldstat_free(instance); + fieldstat_free(instance_dest); +} -// TEST(unit_test_merge, new_too_many_cells_on_one_metric_given_source_cube_reset_and_get_different_cube_comprehensive) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM); -// int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id1, 1); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); -// fieldstat_reset(instance); +TEST(unit_test_merge, new_too_many_cells_on_one_metric_given_source_cube_reset_and_get_different_cube_comprehensive) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM); + int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id1, 1); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); + fieldstat_reset(instance); -// int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id2, 2); -// int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id3, 3); + int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id2, 2); + int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id3, 3); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); -// Fieldstat_tag_list_wrapper tag_list_wrapper(tag_list); -// tag_list_wrapper.print_tag_list(); -// EXPECT_EQ(n_cell, 2); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); + Fieldstat_tag_list_wrapper tag_list_wrapper(tag_list); + tag_list_wrapper.print_tag_list(); + EXPECT_EQ(n_cell, 2); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); +} -// TEST(unit_test_merge, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_comprehensive) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); -// int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); -// int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); -// int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); -// fieldstat_reset(instance); +TEST(unit_test_merge, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_comprehensive) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); + int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); + int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); + fieldstat_reset(instance); -// int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); -// int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); -// int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); + int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); + int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); + int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); -// fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 0); + fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 0); -// fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_INT.key); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); + fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_INT.key); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); +} -// TEST(unit_test_merge, new_cube_and_metric_to_empty_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_TOPK, 10); -// fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); +TEST(unit_test_merge, new_cube_and_metric_to_empty_topk) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_TOPK, 10); + fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); -// struct fieldstat *instance_dest = fieldstat_new(); + struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id_dest; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); -// EXPECT_TRUE(n_cube == 1); -// EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest[0], 0), "metric_name"); + int *cube_id_dest; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); + EXPECT_TRUE(n_cube == 1); + EXPECT_STREQ(fieldstat_get_metric_name(instance_dest, cube_id_dest[0], 0), "metric_name"); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(cube_id_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(cube_id_dest); +} -// TEST(unit_test_merge, new_cell_on_existing_cube_and_metric_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); +TEST(unit_test_merge, new_cell_on_existing_cube_and_metric_topk) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); -// int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 10086); + int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 10086); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id_dest; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); -// EXPECT_TRUE(n_cube == 1); -// int ret_cube_id = cube_id_dest[0]; -// free(cube_id_dest); -// int ret_metric_id = fieldstat_get_max_metric_id(instance, ret_cube_id); -// EXPECT_EQ(ret_metric_id, 0); -// long long measure = fieldstat_counter_get(instance, cube_id, metric_id, cell_id); -// EXPECT_EQ(measure, 10086); + int *cube_id_dest; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); + EXPECT_TRUE(n_cube == 1); + int ret_cube_id = cube_id_dest[0]; + free(cube_id_dest); + int ret_metric_id = fieldstat_get_max_metric_id(instance, ret_cube_id); + EXPECT_EQ(ret_metric_id, 0); + long long measure = fieldstat_counter_get(instance, cube_id, metric_id, cell_id); + EXPECT_EQ(measure, 10086); -// int *ret_cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance, cube_id, metric_id, &ret_cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// EXPECT_EQ(tag_list->n_tag, 1); -// EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); + int *ret_cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance, cube_id, metric_id, &ret_cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_EQ(tag_list->n_tag, 1); + EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(ret_cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(ret_cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); +} -// TEST(unit_test_merge, merge_existing_cell_on_existing_cube_and_metric_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); -// int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 5); -// struct fieldstat *instance_dest = fieldstat_new(); +TEST(unit_test_merge, merge_existing_cell_on_existing_cube_and_metric_topk) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric_name", COUNTER_MERGE_BY_SUM); + int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 5); + struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cube_id_dest; -// int n_cube; -// fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); -// EXPECT_TRUE(n_cube == 1); -// int ret_cube_id = cube_id_dest[0]; -// free(cube_id_dest); -// int ret_metric_id = fieldstat_get_max_metric_id(instance_dest, ret_cube_id); -// EXPECT_EQ(ret_metric_id, 0); + int *cube_id_dest; + int n_cube; + fieldstat_get_cubes(instance_dest, &cube_id_dest, &n_cube); + EXPECT_TRUE(n_cube == 1); + int ret_cube_id = cube_id_dest[0]; + free(cube_id_dest); + int ret_metric_id = fieldstat_get_max_metric_id(instance_dest, ret_cube_id); + EXPECT_EQ(ret_metric_id, 0); -// int *ret_cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, ret_cube_id, ret_metric_id, &ret_cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// long long measure = fieldstat_counter_get(instance_dest, cube_id, metric_id, ret_cell_ids[0]); -// EXPECT_EQ(measure, 10); + int *ret_cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, ret_cube_id, ret_metric_id, &ret_cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + long long measure = fieldstat_counter_get(instance_dest, cube_id, metric_id, ret_cell_ids[0]); + EXPECT_EQ(measure, 10); -// free(ret_cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + free(ret_cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + fieldstat_free(instance); + fieldstat_free(instance_dest); +} -// TEST(unit_test_merge, new_too_many_cells_on_one_metric_given_source_cube_reset_and_get_different_cube_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM); -// int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id1, 1); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); -// fieldstat_reset(instance); +TEST(unit_test_merge, new_too_many_cells_on_one_metric_given_source_cube_reset_and_get_different_cube_topk) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM); + int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id1, 1); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); + fieldstat_reset(instance); -// int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id2, 2); -// int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id3, 3); + int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id2, 2); + int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id3, 3); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 2); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 2); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); +} -// TEST(unit_test_merge, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); -// int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); -// int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); -// int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge(instance_dest, instance); -// fieldstat_reset(instance); +TEST(unit_test_merge, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_topk) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); + int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); + int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge(instance_dest, instance); + fieldstat_reset(instance); -// int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); -// int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 2); -// fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); -// int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 3); -// fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); + int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); + int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 2); + fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); + int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 3); + fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); -// fieldstat_merge(instance_dest, instance); + fieldstat_merge(instance_dest, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); -// int pop_cell_cnt = 0; -// bool int_tag_found = false; -// bool double_tag_found = false; -// if (n_cell == 1) { -// if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { -// int_tag_found = true; -// } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { -// double_tag_found = true; -// } else { -// printf("the tag is %s\n", tag_list->tag[0].key); -// } -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } else { -// EXPECT_EQ(n_cell, 0); -// pop_cell_cnt++; -// } + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); + int pop_cell_cnt = 0; + bool int_tag_found = false; + bool double_tag_found = false; + if (n_cell == 1) { + if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { + int_tag_found = true; + } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { + double_tag_found = true; + } else { + printf("the tag is %s\n", tag_list->tag[0].key); + } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + } else { + EXPECT_EQ(n_cell, 0); + pop_cell_cnt++; + } -// fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); -// if (n_cell == 1) { -// if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { -// int_tag_found = true; -// } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { -// double_tag_found = true; -// } else { -// printf("the tag is %s\n", tag_list->tag[0].key); -// } -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } else { -// EXPECT_EQ(n_cell, 0); -// pop_cell_cnt++; -// } + fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); + if (n_cell == 1) { + if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { + int_tag_found = true; + } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { + double_tag_found = true; + } else { + printf("the tag is %s\n", tag_list->tag[0].key); + } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + } else { + EXPECT_EQ(n_cell, 0); + pop_cell_cnt++; + } -// fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); -// if (n_cell == 1) { -// if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { -// int_tag_found = true; -// } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { -// double_tag_found = true; -// } else { -// printf("the tag is %s\n", tag_list->tag[0].key); -// } -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } else { -// EXPECT_EQ(n_cell, 0); -// pop_cell_cnt++; -// } -// EXPECT_EQ(pop_cell_cnt, 1); -// EXPECT_TRUE(int_tag_found); -// EXPECT_TRUE(double_tag_found); + fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); + if (n_cell == 1) { + if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { + int_tag_found = true; + } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { + double_tag_found = true; + } else { + printf("the tag is %s\n", tag_list->tag[0].key); + } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + } else { + EXPECT_EQ(n_cell, 0); + pop_cell_cnt++; + } + EXPECT_EQ(pop_cell_cnt, 1); + EXPECT_TRUE(int_tag_found); + EXPECT_TRUE(double_tag_found); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); +} -// struct fieldstat *test_push_flows(vector<Fieldstat_tag_list_wrapper *> &flows_in_test, int K, long long count = 1) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, K); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM); -// for (size_t i = 0; i < flows_in_test.size(); i++) { -// int cell_id = fieldstat_cube_add(instance, cube_id, flows_in_test[i]->get_tag(), flows_in_test[i]->get_tag_count(), count); -// if (cell_id < 0) { -// continue; -// } -// fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, count); -// } -// return instance; -// } +struct fieldstat *test_push_flows(vector<Fieldstat_tag_list_wrapper *> &flows_in_test, int K, long long count = 1) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, K); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name", COUNTER_MERGE_BY_SUM); + for (size_t i = 0; i < flows_in_test.size(); i++) { + int cell_id = fieldstat_cube_add(instance, cube_id, flows_in_test[i]->get_tag(), flows_in_test[i]->get_tag_count(), count); + if (cell_id < 0) { + continue; + } + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, count); + } + return instance; +} -// TEST(unit_test_merge, merge_accuracy_test_with_K_large_enough_topk) -// { -// int K = 100; -// vector<Fieldstat_tag_list_wrapper *> flows_in_src = test_gen_topk_flows(K, K); -// struct fieldstat *instance_src = test_push_flows(flows_in_src, K); -// vector<Fieldstat_tag_list_wrapper *> flows_in_dest = test_gen_topk_flows(K, K); -// struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K); -// fieldstat_merge(instance_dest, instance_src); +TEST(unit_test_merge, merge_accuracy_test_with_K_large_enough_topk) +{ + int K = 100; + vector<Fieldstat_tag_list_wrapper *> flows_in_src = test_gen_topk_flows(K, K); + struct fieldstat *instance_src = test_push_flows(flows_in_src, K); + vector<Fieldstat_tag_list_wrapper *> flows_in_dest = test_gen_topk_flows(K, K); + struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K); + fieldstat_merge(instance_dest, instance_src); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); -// vector<Fieldstat_tag_list_wrapper *> flows_in_merged; -// for (size_t i = 0; i < n_cell; i++) { -// flows_in_merged.push_back(new Fieldstat_tag_list_wrapper(&tag_list[i])); -// } + vector<Fieldstat_tag_list_wrapper *> flows_in_merged; + for (size_t i = 0; i < n_cell; i++) { + flows_in_merged.push_back(new Fieldstat_tag_list_wrapper(&tag_list[i])); + } -// flows_in_dest.insert(flows_in_dest.end(), std::make_move_iterator(flows_in_src.begin()), std::make_move_iterator(flows_in_src.end())); -// double accuracy = test_cal_accuracy_given_expected_key(flows_in_dest, flows_in_merged); -// EXPECT_TRUE(accuracy > 0.99); // should be 1.0 + flows_in_dest.insert(flows_in_dest.end(), std::make_move_iterator(flows_in_src.begin()), std::make_move_iterator(flows_in_src.end())); + double accuracy = test_cal_accuracy_given_expected_key(flows_in_dest, flows_in_merged); + EXPECT_TRUE(accuracy > 0.99); // should be 1.0 -// fieldstat_free(instance_src); -// fieldstat_free(instance_dest); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// for (size_t i = 0; i < flows_in_merged.size(); i++) { -// delete flows_in_merged[i]; -// } -// for (size_t i = 0; i < flows_in_dest.size(); i++) { -// delete flows_in_dest[i]; -// } -// } + fieldstat_free(instance_src); + fieldstat_free(instance_dest); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + for (size_t i = 0; i < flows_in_merged.size(); i++) { + delete flows_in_merged[i]; + } + for (size_t i = 0; i < flows_in_dest.size(); i++) { + delete flows_in_dest[i]; + } +} -// TEST(unit_test_merge, merge_accuracy_test_gen_dest_full_all_inserted_given_src_flows_larger) -// { -// int K = 100; -// vector<Fieldstat_tag_list_wrapper *> flows_in_src = test_gen_topk_flows(10000, K); -// struct fieldstat *instance_src = test_push_flows(flows_in_src, K, 1000); -// vector<Fieldstat_tag_list_wrapper *> flows_in_dest; -// for (int i = 0; i < K; i++) { -// Fieldstat_tag_list_wrapper *tmp = new Fieldstat_tag_list_wrapper("flows in dest", to_string(i).c_str()); -// flows_in_dest.push_back(tmp); -// } -// struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K, 1); +TEST(unit_test_merge, merge_accuracy_test_gen_dest_full_all_inserted_given_src_flows_larger) +{ + int K = 100; + vector<Fieldstat_tag_list_wrapper *> flows_in_src = test_gen_topk_flows(10000, K); + struct fieldstat *instance_src = test_push_flows(flows_in_src, K, 1000); + vector<Fieldstat_tag_list_wrapper *> flows_in_dest; + for (int i = 0; i < K; i++) { + Fieldstat_tag_list_wrapper *tmp = new Fieldstat_tag_list_wrapper("flows in dest", to_string(i).c_str()); + flows_in_dest.push_back(tmp); + } + struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K, 1); -// fieldstat_merge(instance_dest, instance_src); + fieldstat_merge(instance_dest, instance_src); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); -// vector<Fieldstat_tag_list_wrapper *> flows_in_merged; -// for (size_t i = 0; i < n_cell; i++) { -// flows_in_merged.push_back(new Fieldstat_tag_list_wrapper(&tag_list[i])); -// } + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); + vector<Fieldstat_tag_list_wrapper *> flows_in_merged; + for (size_t i = 0; i < n_cell; i++) { + flows_in_merged.push_back(new Fieldstat_tag_list_wrapper(&tag_list[i])); + } -// flows_in_dest.insert(flows_in_dest.end(), std::make_move_iterator(flows_in_src.begin()), std::make_move_iterator(flows_in_src.end())); -// double accuracy = test_cal_accuracy_given_expected_key(flows_in_dest, flows_in_merged); + flows_in_dest.insert(flows_in_dest.end(), std::make_move_iterator(flows_in_src.begin()), std::make_move_iterator(flows_in_src.end())); + double accuracy = test_cal_accuracy_given_expected_key(flows_in_dest, flows_in_merged); -// EXPECT_TRUE(accuracy > 0.99); // should be 1.0 + EXPECT_TRUE(accuracy > 0.99); // should be 1.0 -// fieldstat_free(instance_src); -// fieldstat_free(instance_dest); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// for (size_t i = 0; i < flows_in_merged.size(); i++) { -// delete flows_in_merged[i]; -// } -// for (size_t i = 0; i < flows_in_dest.size(); i++) { -// delete flows_in_dest[i]; -// } -// } + fieldstat_free(instance_src); + fieldstat_free(instance_dest); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + for (size_t i = 0; i < flows_in_merged.size(); i++) { + delete flows_in_merged[i]; + } + for (size_t i = 0; i < flows_in_dest.size(); i++) { + delete flows_in_dest[i]; + } +} -// TEST(unit_test_merge, merge_accuracy_test_gen_dest_full_some_inserted_and_some_merged_and_some_fail_to_add) -// { -// int K = 100; -// vector<Fieldstat_tag_list_wrapper *> flows_in_src = test_gen_topk_flows(10000, K + 50); // let elephant flows in src and dest different -// struct fieldstat *instance_src = test_push_flows(flows_in_src, K); -// vector<Fieldstat_tag_list_wrapper *> flows_in_dest = test_gen_topk_flows(10000, K + 50); -// struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K); -// fieldstat_merge(instance_dest, instance_src); +TEST(unit_test_merge, merge_accuracy_test_gen_dest_full_some_inserted_and_some_merged_and_some_fail_to_add) +{ + int K = 100; + vector<Fieldstat_tag_list_wrapper *> flows_in_src = test_gen_topk_flows(10000, K + 50); // let elephant flows in src and dest different + struct fieldstat *instance_src = test_push_flows(flows_in_src, K); + vector<Fieldstat_tag_list_wrapper *> flows_in_dest = test_gen_topk_flows(10000, K + 50); + struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K); + fieldstat_merge(instance_dest, instance_src); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); -// vector<Fieldstat_tag_list_wrapper *> flows_in_merged; -// for (size_t i = 0; i < n_cell; i++) { -// flows_in_merged.push_back(new Fieldstat_tag_list_wrapper(&tag_list[i])); -// } + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); + vector<Fieldstat_tag_list_wrapper *> flows_in_merged; + for (size_t i = 0; i < n_cell; i++) { + flows_in_merged.push_back(new Fieldstat_tag_list_wrapper(&tag_list[i])); + } -// flows_in_dest.insert(flows_in_dest.end(), std::make_move_iterator(flows_in_src.begin()), std::make_move_iterator(flows_in_src.end())); -// double accuracy = test_cal_accuracy_given_expected_key(flows_in_dest, flows_in_merged); -// EXPECT_TRUE(accuracy >= 0.93); // by heavy keeper benchmark, with K = 100, merging result should be about 0.96, for adding the flows will also cause some inaccuracy, so here we set 0.93 -// printf("merge_accuracy_test_gen_dest_full_some_inserted_and_some_merged_and_some_fail_to_add accuracy is %lf\n", accuracy); + flows_in_dest.insert(flows_in_dest.end(), std::make_move_iterator(flows_in_src.begin()), std::make_move_iterator(flows_in_src.end())); + double accuracy = test_cal_accuracy_given_expected_key(flows_in_dest, flows_in_merged); + EXPECT_TRUE(accuracy >= 0.93); // by heavy keeper benchmark, with K = 100, merging result should be about 0.96, for adding the flows will also cause some inaccuracy, so here we set 0.93 + printf("merge_accuracy_test_gen_dest_full_some_inserted_and_some_merged_and_some_fail_to_add accuracy is %lf\n", accuracy); -// fieldstat_free(instance_src); -// fieldstat_free(instance_dest); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// for (size_t i = 0; i < flows_in_merged.size(); i++) { -// delete flows_in_merged[i]; -// } -// for (size_t i = 0; i < flows_in_dest.size(); i++) { -// delete flows_in_dest[i]; -// } -// } + fieldstat_free(instance_src); + fieldstat_free(instance_dest); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + for (size_t i = 0; i < flows_in_merged.size(); i++) { + delete flows_in_merged[i]; + } + for (size_t i = 0; i < flows_in_dest.size(); i++) { + delete flows_in_dest[i]; + } +} -// TEST(unit_test_merge, exception_test_given_cell_added_but_no_metric_operation_given_empty_dst) -// { -// struct fieldstat *instance = fieldstat_new(); -// struct fieldstat *instance2 = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); -// int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); +TEST(unit_test_merge, exception_test_given_cell_added_but_no_metric_operation_given_empty_dst) +{ + struct fieldstat *instance = fieldstat_new(); + struct fieldstat *instance2 = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_merge(instance2, instance); + fieldstat_merge(instance2, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance2, cube_id, metric_id, &cell_ids, &tag_list, &n_cell); -// EXPECT_TRUE(n_cell == 0); -// EXPECT_EQ(fieldstat_counter_get(instance2, cube_id, metric_id, cell_id), -1); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance2, cube_id, metric_id, &cell_ids, &tag_list, &n_cell); + EXPECT_TRUE(n_cell == 0); + EXPECT_EQ(fieldstat_counter_get(instance2, cube_id, metric_id, cell_id), -1); -// fieldstat_free(instance); -// fieldstat_free(instance2); -// } + fieldstat_free(instance); + fieldstat_free(instance2); +} -// TEST(unit_test_merge, exception_test_given_cell_added_but_no_metric_operation_given_dst_with_cell_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// struct fieldstat *instance2 = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); -// int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); +TEST(unit_test_merge, exception_test_given_cell_added_but_no_metric_operation_given_dst_with_cell_topk) +{ + struct fieldstat *instance = fieldstat_new(); + struct fieldstat *instance2 = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// int cube_id_dst = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); -// int metric_id_dst = fieldstat_register_counter(instance, cube_id_dst, "metric name1", COUNTER_MERGE_BY_SUM); -// int cell_id_dst = fieldstat_cube_add(instance, cube_id_dst, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id_dst, metric_id_dst, cell_id_dst, 123); + int cube_id_dst = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); + int metric_id_dst = fieldstat_register_counter(instance, cube_id_dst, "metric name1", COUNTER_MERGE_BY_SUM); + int cell_id_dst = fieldstat_cube_add(instance, cube_id_dst, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id_dst, metric_id_dst, cell_id_dst, 123); -// fieldstat_merge(instance2, instance); + fieldstat_merge(instance2, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance2, cube_id, metric_id, &cell_ids, &tag_list, &n_cell); -// EXPECT_TRUE(n_cell == 1); -// EXPECT_EQ(fieldstat_counter_get(instance2, cube_id, metric_id, cell_id), 123); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance2, cube_id, metric_id, &cell_ids, &tag_list, &n_cell); + EXPECT_TRUE(n_cell == 1); + EXPECT_EQ(fieldstat_counter_get(instance2, cube_id, metric_id, cell_id), 123); -// fieldstat_free(instance); -// fieldstat_free(instance2); + fieldstat_free(instance); + fieldstat_free(instance2); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); +} -// TEST(test_merge_and_reset, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_topk) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); -// int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); -// int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); -// int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge_and_reset(instance_dest, instance); +TEST(test_merge_and_reset, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_topk) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2); + int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); + int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge_and_reset(instance_dest, instance); -// int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); -// int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 2); -// fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); -// int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 3); -// fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); + int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); + int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 2); + fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); + int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 3); + fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); -// fieldstat_merge_and_reset(instance_dest, instance); + fieldstat_merge_and_reset(instance_dest, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); -// int pop_cell_cnt = 0; -// bool int_tag_found = false; -// bool double_tag_found = false; -// if (n_cell == 1) { -// if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { -// int_tag_found = true; -// } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { -// double_tag_found = true; -// } else { -// printf("the tag is %s\n", tag_list->tag[0].key); -// } -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } else { -// EXPECT_EQ(n_cell, 0); -// pop_cell_cnt++; -// } + int pop_cell_cnt = 0; + bool int_tag_found = false; + bool double_tag_found = false; + if (n_cell == 1) { + if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { + int_tag_found = true; + } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { + double_tag_found = true; + } else { + printf("the tag is %s\n", tag_list->tag[0].key); + } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + } else { + EXPECT_EQ(n_cell, 0); + pop_cell_cnt++; + } -// fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); -// if (n_cell == 1) { -// if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { -// int_tag_found = true; -// } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { -// double_tag_found = true; -// } else { -// printf("the tag is %s\n", tag_list->tag[0].key); -// } -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } else { -// EXPECT_EQ(n_cell, 0); -// pop_cell_cnt++; -// } + fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); + if (n_cell == 1) { + if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { + int_tag_found = true; + } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { + double_tag_found = true; + } else { + printf("the tag is %s\n", tag_list->tag[0].key); + } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + } else { + EXPECT_EQ(n_cell, 0); + pop_cell_cnt++; + } -// fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); -// if (n_cell == 1) { -// if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { -// int_tag_found = true; -// } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { -// double_tag_found = true; -// } else { -// printf("the tag is %s\n", tag_list->tag[0].key); -// } -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); -// } else { -// EXPECT_EQ(n_cell, 0); -// pop_cell_cnt++; -// } -// EXPECT_EQ(pop_cell_cnt, 1); -// EXPECT_TRUE(int_tag_found); -// EXPECT_TRUE(double_tag_found); + fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); + if (n_cell == 1) { + if (strcmp(tag_list->tag[0].key, TEST_TAG_INT.key) == 0) { + int_tag_found = true; + } else if (strcmp(tag_list->tag[0].key, TEST_TAG_DOUBLE.key) == 0) { + double_tag_found = true; + } else { + printf("the tag is %s\n", tag_list->tag[0].key); + } + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + } else { + EXPECT_EQ(n_cell, 0); + pop_cell_cnt++; + } + EXPECT_EQ(pop_cell_cnt, 1); + EXPECT_TRUE(int_tag_found); + EXPECT_TRUE(double_tag_found); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); +} -// TEST(test_merge_and_reset, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_comprehensive) -// { -// struct fieldstat *instance = fieldstat_new(); -// int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); -// int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); -// int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); -// int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); -// struct fieldstat *instance_dest = fieldstat_new(); -// fieldstat_merge_and_reset(instance_dest, instance); -// fieldstat_reset(instance); +TEST(test_merge_and_reset, new_too_many_cells_on_multiple_metric_given_source_cube_reset_and_get_different_cube_comprehensive) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); + int metric_id1 = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, cube_id, "metric name2", COUNTER_MERGE_BY_SUM); + int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id1, cell_id1, 1); + struct fieldstat *instance_dest = fieldstat_new(); + fieldstat_merge_and_reset(instance_dest, instance); + fieldstat_reset(instance); -// int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); -// int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); -// int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); -// fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); + int metric_id3 = fieldstat_register_counter(instance, cube_id, "metric name3", COUNTER_MERGE_BY_SUM); + int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id3, cell_id2, 2); + int cell_id3 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id2, cell_id3, 3); -// fieldstat_merge_and_reset(instance_dest, instance); + fieldstat_merge_and_reset(instance_dest, instance); -// int *cell_ids = NULL; -// struct fieldstat_tag_list *tag_list = NULL; -// size_t n_cell = 0; -// fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, metric_id1, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); -// fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 0); + fieldstat_get_cells(instance_dest, 0, metric_id2, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 0); -// fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); -// EXPECT_EQ(n_cell, 1); -// EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_INT.key); -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); + fieldstat_get_cells(instance_dest, 0, metric_id3, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_INT.key); + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); -// fieldstat_free(instance); -// fieldstat_free(instance_dest); -// } + fieldstat_free(instance); + fieldstat_free(instance_dest); +} + +TEST(test_merge_and_reset, accept_multiple_instance_multi_times) +{ + const int THREAD_NUM = 1; + struct fieldstat *instances[THREAD_NUM]; + int count = 3; + struct fieldstat *instance_dest = fieldstat_new(); + for (int i = 0; i < THREAD_NUM; i++) { + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + instances[i] = instance; + } -// TEST(test_merge_and_reset, accept_multiple_instance_multi_times) + while(count--) { + for (int i = 0; i < THREAD_NUM; i++) { + int cell_id = fieldstat_cube_add(instances[i], 0, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instances[i], 0, 0, 0, 1); + } + + for (int i = 0; i < THREAD_NUM; i++) { + fieldstat_merge_and_reset(instance_dest, instances[i]); + } + } + + int *cell_ids = NULL; + struct fieldstat_tag_list *tag_list = NULL; + size_t n_cell = 0; + fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); + EXPECT_EQ(n_cell, 1); + EXPECT_EQ(fieldstat_counter_get(instance_dest, 0, 0, cell_ids[0]), THREAD_NUM * 3); + + free(cell_ids); + fieldstat_tag_list_arr_free(tag_list, n_cell); + fieldstat_free(instance_dest); + for (int i = 0; i < THREAD_NUM; i++) { + fieldstat_free(instances[i]); + } +} + +// TEST(test_merge_and_reset, ensure_no_data_race) // { -// const int THREAD_NUM = 1; +// const int THREAD_NUM = 10; +// const int OPER_NUM = 1000000; +// std::vector<std::thread> threads; + // struct fieldstat *instances[THREAD_NUM]; -// int count = 3; -// struct fieldstat *instance_dest = fieldstat_new(); // for (int i = 0; i < THREAD_NUM; i++) { // struct fieldstat *instance = fieldstat_new(); // int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); -// int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); +// (void)fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); // instances[i] = instance; // } -// while(count--) { -// printf("count: %d\n", count); -// for (int i = 0; i < THREAD_NUM; i++) { -// int cell_id = fieldstat_cube_add(instances[i], 0, &TEST_TAG_STRING, 1, 1); -// fieldstat_counter_incrby(instances[i], 0, 0, 0, 1); -// } +// for (int i = 0; i < THREAD_NUM; i++) { +// std::thread t1([instances, i]() { +// for (int j = 0; j < OPER_NUM; j++) { +// fieldstat_cube_add(instances[i], 0, &TEST_TAG_STRING, 1, 1); +// fieldstat_counter_incrby(instances[i], 0, 0, 0, 1); +// } +// printf("thread %d done\n", i); +// }); +// threads.push_back(std::move(t1)); +// } -// for (int i = 0; i < THREAD_NUM; i++) { -// fieldstat_merge_and_reset(instance_dest, instances[i]); +// struct fieldstat *instance_dest = fieldstat_new(); +// std::thread t_merge([instance_dest, instances, THREAD_NUM]() { +// int count = OPER_NUM + 10000; // ensure merge is slower than incrby, incrby times: OPER_NUM * THREAD_NUM +// while (count--) { +// for (int i = 0; i < THREAD_NUM; i++) { +// fieldstat_merge_and_reset(instance_dest, instances[i]); +// } // } -// } +// printf("merge done\n"); +// }); +// for (auto &t : threads) { +// t.join(); +// } +// t_merge.join(); + // int *cell_ids = NULL; // struct fieldstat_tag_list *tag_list = NULL; // size_t n_cell = 0; // fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); // EXPECT_EQ(n_cell, 1); -// EXPECT_EQ(fieldstat_counter_get(instance_dest, 0, 0, cell_ids[0]), THREAD_NUM * 3); +// EXPECT_NEAR(fieldstat_counter_get(instance_dest, 0, 0, cell_ids[0]), THREAD_NUM * OPER_NUM, THREAD_NUM * OPER_NUM * 0.01); // a little bit operations during locking and unlocking the mutex will be lost -// free(cell_ids); -// fieldstat_tag_list_arr_free(tag_list, n_cell); // fieldstat_free(instance_dest); // for (int i = 0; i < THREAD_NUM; i++) { // fieldstat_free(instances[i]); // } +// free(cell_ids); +// fieldstat_tag_list_arr_free(tag_list, n_cell); // } -TEST(test_merge_and_reset, ensure_no_data_race) +TEST(test_merge_and_reset, merge_on_merge_destination) { - const int THREAD_NUM = 1; - std::vector<std::thread> threads; - - struct fieldstat *instances[THREAD_NUM]; - for (int i = 0; i < THREAD_NUM; i++) { - struct fieldstat *instance = fieldstat_new(); - int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); - int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); - instances[i] = instance; - } + struct fieldstat *instance = fieldstat_new(); + struct fieldstat *instance_transfer = fieldstat_new(); + struct fieldstat *instance_dest = fieldstat_new(); - for (int i = 0; i < THREAD_NUM; i++) { - std::thread t1([instances, i]() { - for (int j = 0; j < 10000; j++) { - fieldstat_cube_add(instances[i], 0, &TEST_TAG_STRING, 1, 1); - fieldstat_counter_incrby(instances[i], 0, 0, 0, 1); - } - printf("thread %d done\n", i); - }); - threads.push_back(std::move(t1)); - } + int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); + int metric_id = fieldstat_register_counter(instance, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int cell_id = fieldstat_cube_add(instance, 0, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance, 0, 0, 0, 1); - struct fieldstat *instance_dest = fieldstat_new(); - std::thread t_merge([instance_dest, instances, THREAD_NUM]() { - int count = 10000 + 5000; // ensure merge is slower than incrby, incrby times: 10000 * THREAD_NUM - while (count--) { - for (int i = 0; i < THREAD_NUM; i++) { - fieldstat_merge_and_reset(instance_dest, instances[i]); - } - } - printf("merge done\n"); - }); - - for (auto &t : threads) { - t.join(); - } - t_merge.join(); + fieldstat_merge_and_reset(instance_transfer, instance); + fieldstat_merge_and_reset(instance_dest, instance_transfer); - int *cell_ids = NULL; - struct fieldstat_tag_list *tag_list = NULL; - size_t n_cell = 0; - fieldstat_get_cells(instance_dest, 0, 0, &cell_ids, &tag_list, &n_cell); - EXPECT_EQ(n_cell, 1); - EXPECT_EQ(fieldstat_counter_get(instance_dest, 0, 0, cell_ids[0]), THREAD_NUM * 10000); + EXPECT_EQ(fieldstat_counter_get(instance_dest, 0, 0, cell_id), 1); + fieldstat_free(instance); + fieldstat_free(instance_transfer); fieldstat_free(instance_dest); - for (int i = 0; i < THREAD_NUM; i++) { - fieldstat_free(instances[i]); - } - free(cell_ids); - fieldstat_tag_list_arr_free(tag_list, n_cell); } -TEST(test_merge_and_reset, merge_on_merge_destination) +TEST(test_merge_and_reset, register_add_merge_on_merge_destination) { + struct fieldstat *instance = fieldstat_new(); + struct fieldstat *instance_transfer = fieldstat_new(); + struct fieldstat *instance_dest = fieldstat_new(); -} + fieldstat_merge_and_reset(instance_transfer, instance); -TEST(test_merge_and_reset, register_add_merge_on_merge_destination) -{ + int cube_id = fieldstat_register_cube(instance_transfer, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 2); + int metric_id = fieldstat_register_counter(instance_transfer, cube_id, "metric name1", COUNTER_MERGE_BY_SUM); + int cell_id = fieldstat_cube_add(instance_transfer, 0, &TEST_TAG_STRING, 1, 1); + fieldstat_counter_incrby(instance_transfer, 0, 0, 0, 1); + fieldstat_merge_and_reset(instance_dest, instance_transfer); + + EXPECT_EQ(fieldstat_counter_get(instance_dest, 0, 0, cell_id), 1); + fieldstat_free(instance); + fieldstat_free(instance_transfer); + fieldstat_free(instance_dest); } int main(int argc, char *argv[]) |
