diff options
| author | chenzizhan <[email protected]> | 2023-07-28 14:43:27 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-07-28 14:43:27 +0800 |
| commit | 27c1ef4949920fa3de70007093d08f7ed7fadede (patch) | |
| tree | 757a771654dad417abb3b0a355b3c767baefadfe /test | |
| parent | 559de7cfddaeeea7ac6e9c45100c5eb723ea1484 (diff) | |
new-ver cppcheck, new tests. push for testing CI
Diffstat (limited to 'test')
| -rw-r--r-- | test/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | test/test_exporter_json.cpp | 12 | ||||
| -rw-r--r-- | test/test_merge.cpp | 231 | ||||
| -rw-r--r-- | test/test_metric_counter.cpp | 81 | ||||
| -rw-r--r-- | test/test_metric_histogram.cpp | 6 | ||||
| -rw-r--r-- | test/test_metric_hll.cpp | 4 | ||||
| -rw-r--r-- | test/test_performance.cpp | 204 | ||||
| -rw-r--r-- | test/test_serialize.cpp | 3 | ||||
| -rw-r--r-- | test/unit_test_cell_manager.cpp | 8 | ||||
| -rw-r--r-- | test/utils.cpp | 26 | ||||
| -rw-r--r-- | test/utils.hpp | 1 |
11 files changed, 370 insertions, 211 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 18dd5a5..00c9896 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -48,7 +48,8 @@ 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_reset_and_delete_cube)
add_unit_test(test_serialize)
-add_unit_test(unit_test_cell_manager)
-add_unit_test(unit_test_fast_hash)
\ No newline at end of file +# add_unit_test(unit_test_cell_manager)
+# add_unit_test(unit_test_fast_hash)
\ No newline at end of file diff --git a/test/test_exporter_json.cpp b/test/test_exporter_json.cpp index c7f8c4e..7757c5c 100644 --- a/test/test_exporter_json.cpp +++ b/test/test_exporter_json.cpp @@ -112,7 +112,7 @@ void fill_random_tag(Fieldstat_tag_list_wrapper *tags[], int tag_list_num) } } -cJSON *test_exporter_extract_results_with_standard_global(struct fieldstat *instance) +cJSON *test_exporter_extract_results_with_standard_global(const struct fieldstat *instance) { struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); fieldstat_json_exporter_set_global_tag(fieldstat_json_exporter, TEST_TAG_GLOBAL, 3); @@ -126,7 +126,7 @@ cJSON *test_exporter_extract_results_with_standard_global(struct fieldstat *inst return root_arr; } -cJSON *test_exporter_extract_results(struct fieldstat *instance) +cJSON *test_exporter_extract_results(const struct fieldstat *instance) { struct fieldstat_json_exporter *fieldstat_json_exporter = fieldstat_json_exporter_new(instance); @@ -139,7 +139,7 @@ cJSON *test_exporter_extract_results(struct fieldstat *instance) return root_arr; } -void topk_standard_oper(std::function<void(Fieldstat_tag_list_wrapper *, unsigned int *)> &topk_add, size_t metric_num, unsigned int test_expected_big_count) +void topk_standard_oper(const std::function<void(Fieldstat_tag_list_wrapper *, unsigned int *)> &topk_add, size_t metric_num, unsigned int test_expected_big_count) { for (size_t i = 0; i < OPER_NUM; i++) { int tmp; @@ -167,7 +167,7 @@ void topk_init(struct fieldstat *instance, unsigned int test_expected_big_count) int m2 = fieldstat_register_counter(instance, cube_id, field_name[1], 0); std::function<void(Fieldstat_tag_list_wrapper *, unsigned int *)> topk_add = [instance, cube_id, m1, m2]( - Fieldstat_tag_list_wrapper *my_tags, unsigned int counts[TEST_METRIC_NUM]) { + const Fieldstat_tag_list_wrapper *my_tags, unsigned int counts[TEST_METRIC_NUM]) { int cell_id = fieldstat_cube_add(instance, cube_id, my_tags->get_tag(), my_tags->get_tag_count(), counts[0]); if (cell_id >= 0) { fieldstat_counter_incrby(instance, cube_id, m1, cell_id, counts[0]); @@ -313,7 +313,7 @@ TEST(export_test, skip_two_empty_cube_and_export_last_one_with_global_tag) const int tag_num = 1; Fieldstat_tag_list_wrapper *tags[tag_num]; fill_random_tag(tags, tag_num); - Fieldstat_tag_list_wrapper *the_tag = tags[0]; + const Fieldstat_tag_list_wrapper *the_tag = tags[0]; int cell_id = fieldstat_cube_add(instance, cube_id_3, the_tag->get_tag(), the_tag->get_tag_count(), 1); for (size_t i = 0; i < OPER_NUM; i++){ fieldstat_histogram_record(instance, cube_id_3, id_histogram, cell_id, g_histogram_standard_oper[i]); @@ -362,7 +362,7 @@ TEST(export_test, skip_empty_metrics_given_cube_deleted) { const int tag_num = 1; Fieldstat_tag_list_wrapper *tags[tag_num]; fill_random_tag(tags, tag_num); - Fieldstat_tag_list_wrapper *the_tag = tags[0]; + const Fieldstat_tag_list_wrapper *the_tag = tags[0]; int cell_id = fieldstat_cube_add(instance, cube_id, the_tag->get_tag(), the_tag->get_tag_count(), 1); fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1234); diff --git a/test/test_merge.cpp b/test/test_merge.cpp index 03bbc6d..74f60f3 100644 --- a/test/test_merge.cpp +++ b/test/test_merge.cpp @@ -1,8 +1,71 @@ #include <gtest/gtest.h> +#include <set> +#include <map> #include "fieldstat.h" #include "utils.hpp" +using namespace std; + +vector<Fieldstat_tag_list_wrapper *> test_gen_topk_flows(int num, int rand_flow_type_num) +{ + vector<Fieldstat_tag_list_wrapper *> flows; + for (int i = 0; i < num; i++) + { + if (rand() % 2) { + flows.push_back(new Fieldstat_tag_list_wrapper("my key", i)); + } else { + flows.push_back(new Fieldstat_tag_list_wrapper("elephant", rand() % rand_flow_type_num)); + } + } + return flows; +} + +double test_cal_accuracy(vector<struct Fieldstat_tag_list_wrapper *> &expected_keys, vector<struct Fieldstat_tag_list_wrapper *> &test_result) { + map<string, int> countMap; + for (size_t i = 0; i < expected_keys.size(); i++) { + std::string key = expected_keys[i]->to_string(); + countMap[key]++; + } + + std::vector<std::pair<std::string, int>> countVector(countMap.begin(), countMap.end()); + std::sort(countVector.begin(), countVector.end(), [](const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) { + return a.second > b.second; + }); + + std::set<std::string> myset; + int min_in_max_count = 0; + size_t i; + for (i = 0; i < test_result.size(); ++i) { + myset.insert(countVector[i].first); + min_in_max_count = countVector[i].second; + } + while (i < countVector.size()) { + if (countVector[i].second != min_in_max_count) { + break; + } + myset.insert(countVector[i].first); + i++; + } + + cout << "myset : " << endl; + for (auto it = myset.begin(); it != myset.end(); it++) { + cout << *it << endl; + } + cout << "------------------------- " << endl; + + int correct = 0; + for (size_t i = 0; i < test_result.size(); i++) { + string key = test_result[i]->to_string(); + if (myset.find(key) != myset.end()) { + correct++; + } + } + + double accuracy = (double)correct / test_result.size(); + return accuracy; +} + TEST(unit_test_merge, test_metric_name_mapping_with_new_metric_on_existing_cube) { struct fieldstat *instance = fieldstat_new(); @@ -459,134 +522,70 @@ TEST(unit_test_merge, new_too_many_cells_on_multiple_metric_given_source_cube_re fieldstat_free(instance_dest); } -// todo: 把unit test 里的merge测试移过来 - -// todo:topk 数量超了8倍K - -extern "C" { -extern int tmp_get_hash_count(); -extern int tmp_get_simple_hash_count(); -void reset_hash_count(); +struct fieldstat *test_push_flows(vector<Fieldstat_tag_list_wrapper *> &flows_in_test, int K) +{ + 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", false); + 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(), 1); + if (cell_id < 0) { + continue; + } + fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); + } + return instance; } -// TEST(unit_test_merge, performance_test_when_comprehensive_sampling) -// { -// const int INSTANCE_NUM = 100; -// const int MAX_CELL_NUM = 65535; -// const int DIMENSION_TOTAL = 100000; -// // const int INSTANCE_NUM = 2; -// // const int MAX_CELL_NUM = 1000; -// // const int DIMENSION_TOTAL = 1024; -// Fieldstat_tag_list_wrapper *tags[DIMENSION_TOTAL]; -// for (int i = 0; i < DIMENSION_TOTAL; i++) -// { -// tags[i] = new Fieldstat_tag_list_wrapper("my key", i); -// } - -// struct fieldstat *instances[INSTANCE_NUM]; -// for (int i = 0; i < INSTANCE_NUM; i++) { -// struct fieldstat *tmp_i = fieldstat_new(); -// int cube_id = fieldstat_register_cube(tmp_i, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, MAX_CELL_NUM); -// int metric_id = fieldstat_register_counter(tmp_i, cube_id, "metric name", false); -// for (int j = 0; j < MAX_CELL_NUM; j++) { -// int cell_id = fieldstat_cube_add(tmp_i, cube_id, tags[rand() % DIMENSION_TOTAL]->get_tag(), 1, 1); - -// fieldstat_counter_incrby(tmp_i, cube_id, metric_id, cell_id, 1); -// } -// instances[i] = tmp_i; -// } - -// struct fieldstat *instance_dest = fieldstat_new(); -// reset_hash_count(); -// printf("prepare done\n"); - -// clock_t start = clock(); -// // getchar(); -// for (int i = 0; i < INSTANCE_NUM; i++) { -// fieldstat_merge(instance_dest, instances[i]); -// } -// // exit(0); -// clock_t end = clock(); - -// double elapsed_secs = double(end - start) / CLOCKS_PER_SEC; -// printf("performance_test_when_comprehensive_sampling elapsed_secs: %f\n", elapsed_secs); -// printf("get hash count: %d\n", tmp_get_hash_count()); -// printf("get simple hash count: %d\n", tmp_get_simple_hash_count()); -// EXPECT_TRUE(elapsed_secs < 0.05); // fixme: approximately around 100 cube(templates), 10 metrics. So 0.005s finish it. - -// fieldstat_free(instance_dest); -// for (int i = 0; i < INSTANCE_NUM; i++) { -// fieldstat_free(instances[i]); -// } -// for (int i = 0; i < DIMENSION_TOTAL; i++) { -// delete tags[i]; -// } -// } - -TEST(unit_test_merge, performance_test_when_topk_sampling){ - const int INSTANCE_NUM = 100; - const int MAX_CELL_NUM = 1024; - const int DIMENSION_TOTAL = 100000; - const int CUBE_NUM = 100; - - Fieldstat_tag_list_wrapper *tags[DIMENSION_TOTAL]; - for (int i = 0; i < DIMENSION_TOTAL; i++) - { - if (i%2) - tags[i] = new Fieldstat_tag_list_wrapper("my key", i); - else - tags[i] = new Fieldstat_tag_list_wrapper("elephant", i % 1024); - } +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); + printf("src test_push_flows\n"); + 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); + printf("dest test_push_flows\n"); + struct fieldstat *instance_dest = test_push_flows(flows_in_dest, K); + printf("merge\n"); + fieldstat_merge(instance_dest, instance_src); + printf("merge done\n"); - Fieldstat_tag_list_wrapper *shared_tags[CUBE_NUM]; - for (int i = 0; i < CUBE_NUM; i++) - { - shared_tags[i] = new Fieldstat_tag_list_wrapper("shared key", i); - } - - struct fieldstat *instances[INSTANCE_NUM]; - for (int i = 0; i < INSTANCE_NUM; i++) { - for (int j = 0; j < CUBE_NUM; j++) { - struct fieldstat *tmp_i = fieldstat_new(); - int cube_id = fieldstat_register_cube(tmp_i, shared_tags[j]->get_tag(), 1, SAMPLING_MODE_TOPK, MAX_CELL_NUM); - int metric_id = fieldstat_register_counter(tmp_i, cube_id, "metric name", false); - for (int j = 0; j < MAX_CELL_NUM; j++) { - int cell_id = fieldstat_cube_add(tmp_i, cube_id, tags[rand() % DIMENSION_TOTAL]->get_tag(), 1, 1); - if (cell_id == -1) { - continue; - } - fieldstat_counter_incrby(tmp_i, cube_id, metric_id, cell_id, 1); - } - instances[i] = tmp_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); - struct fieldstat *instance_dest = fieldstat_new(); - reset_hash_count(); - printf("prepare done\n"); - clock_t start = clock(); - // getchar(); - for (int i = 0; i < INSTANCE_NUM; i++) { - fieldstat_merge(instance_dest, instances[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])); } - // exit(0); - clock_t end = clock(); - double elapsed_secs = double(end - start) / CLOCKS_PER_SEC; - printf("performance_test_when_comprehensive_sampling elapsed_secs: %f\n", elapsed_secs); - printf("get hash count: %d\n", tmp_get_hash_count()); - printf("get simple hash count: %d\n", tmp_get_simple_hash_count()); - EXPECT_TRUE(elapsed_secs < 0.2); // fixme: approximately around 100 cube(templates), 10 metrics. So 0.005s finish it. + // // print + // printf("flows_in_merged\n"); + // for (size_t i = 0; i < flows_in_merged.size(); i++) { + // cout << flows_in_merged[i]->to_string() << endl; + // } + // cout << "---------------------------------------------" << endl; + + 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(flows_in_dest, flows_in_merged); + EXPECT_TRUE(accuracy > 0.99); // should be 1.0 + printf("accuracy is %lf\n", accuracy); + fieldstat_free(instance_src); fieldstat_free(instance_dest); - for (int i = 0; i < INSTANCE_NUM; i++) { - fieldstat_free(instances[i]); + 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 (int i = 0; i < DIMENSION_TOTAL; i++) { - delete tags[i]; + for (size_t i = 0; i < flows_in_dest.size(); i++) { + delete flows_in_dest[i]; } } +// todo:topk 数量超了8倍K + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); diff --git a/test/test_metric_counter.cpp b/test/test_metric_counter.cpp index 9b2a1e7..be43af6 100644 --- a/test/test_metric_counter.cpp +++ b/test/test_metric_counter.cpp @@ -22,7 +22,7 @@ struct fieldstat *test_init_standard_instance_one_cube_one_metric_one_cell_count return instance; } -void test_assert_standard_instance(struct fieldstat *instance) +void test_assert_standard_instance(const struct fieldstat *instance) { int *ret_cube_id_arr = NULL; int n_cube = 0; @@ -249,84 +249,7 @@ TEST(metric_test_counter, topk_add_and_test_accuracy) delete tags[i]; } -TEST(metric_test_counter, performance_test_on_1_cells_1000000_times) -{ - // getchar(); - struct fieldstat *instance = fieldstat_new(); - fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(instance, 0, "test", 0); - int cell_id = fieldstat_cube_add(instance, 0, &TEST_TAG_DOUBLE, 1, 1); - clock_t start = clock(); - for (size_t i = 0; i < 1000000; i++) { - fieldstat_counter_incrby(instance, 0, 0, cell_id, 1); - } - clock_t end = clock(); - double seconds = (double)(end - start) / CLOCKS_PER_SEC; - printf("performance_test_on_1_cells_1000000_times time cost: %f\n", seconds); - EXPECT_TRUE(seconds < 1); // 1 = 0.000001 * 1000000 - fieldstat_free(instance); -} - -TEST(metric_test_counter, performance_test_on_60000_cells_comprehensive_1000000_times) -{ - int cell_count = 60000; - struct fieldstat_tag tags[cell_count]; - for (int i = 0; i < cell_count; i++) { - tags[i] = TEST_TAG_INT; - tags[i].value_longlong = i; - } - // getchar(); - struct fieldstat *instance = fieldstat_new(); - fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, cell_count); - fieldstat_register_counter(instance, 0, "test", 0); - - clock_t start = clock(); - for (size_t i = 0; i < 1000000; i++) { - int cell_id = fieldstat_cube_add(instance, 0, &tags[i % cell_count], 1, 1); - if (cell_id < 0) { - continue; - } - fieldstat_counter_incrby(instance, 0, 0, cell_id, 1); - } - clock_t end = clock(); - double seconds = (double)(end - start) / CLOCKS_PER_SEC; - printf("performance_test_on_60000_cells_comprehensive_1000000_times time cost: %f\n", seconds); - EXPECT_TRUE(seconds < 1); // 1 = 0.000001 * 1000000 - fieldstat_free(instance); -} - -TEST(metric_test_counter, performance_test_on_1000_cells_topk_1000000_times) -{ - int cell_count = 60000; - struct fieldstat_tag tags[cell_count]; - for (int i = 0; i < cell_count; i++) { - tags[i] = TEST_TAG_INT; - // tags[i].value_longlong = rand() % 10000; - if (rand()%2) - tags[i].value_longlong = i; - else - tags[i].value_longlong = rand() % 1000; - } - struct fieldstat *instance = fieldstat_new(); - fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_TOPK, 1000); - fieldstat_register_counter(instance, 0, "test", 0); - - // getchar(); - clock_t start = clock(); - for (size_t i = 0; i < 1000000; i++) { - int cell_id = fieldstat_cube_add(instance, 0, &tags[i % cell_count], 1, 1); - if (cell_id != -1) - fieldstat_counter_incrby(instance, 0, 0, cell_id, 1); - } - clock_t end = clock(); - double seconds = (double)(end - start) / CLOCKS_PER_SEC; - // exit(0); - - EXPECT_TRUE(seconds < 1); // 1 = 0.000001 * 1000000 - printf("performance_test_on_1000_cells_topk_1000000_times time cost: %f\n", seconds); - - fieldstat_free(instance); -} +// todo: add on wrong cube/metric/cell int main(int argc, char *argv[]) { diff --git a/test/test_metric_histogram.cpp b/test/test_metric_histogram.cpp index 14c9833..996b1ba 100644 --- a/test/test_metric_histogram.cpp +++ b/test/test_metric_histogram.cpp @@ -18,7 +18,7 @@ struct fieldstat *test_init_standard_instance_one_cube_one_metric_one_cell_hdr() return instance; } -void test_assert_standard_instance(struct fieldstat *instance) +void test_assert_standard_instance(const struct fieldstat *instance) { int *ret_cube_id_arr = NULL; int n_cube = 0; @@ -120,6 +120,10 @@ TEST(metric_test_histogram, serialization_and_merge_twice_with_reset) fieldstat_free(instance_total); } +// todo: add on wrong cube/metric/cell + +// todo:测base 64序列化 + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); diff --git a/test/test_metric_hll.cpp b/test/test_metric_hll.cpp index dbe8456..0441238 100644 --- a/test/test_metric_hll.cpp +++ b/test/test_metric_hll.cpp @@ -18,7 +18,7 @@ struct fieldstat *test_init_standard_instance_one_cube_one_metric_one_cell_hll(b return instance; } -void test_assert_standard_instance(struct fieldstat *instance) +void test_assert_standard_instance(const struct fieldstat *instance) { int *ret_cube_id_arr = NULL; int n_cube = 0; @@ -119,7 +119,7 @@ TEST(metric_test_hll, serialization_and_merge_twice_with_reset) } // todo:测base 64序列化 - +// todo: add on wrong cube/metric/cell int main(int argc, char *argv[]) { diff --git a/test/test_performance.cpp b/test/test_performance.cpp new file mode 100644 index 0000000..721c339 --- /dev/null +++ b/test/test_performance.cpp @@ -0,0 +1,204 @@ + + + +#include <gtest/gtest.h> +#include "fieldstat.h" +#include "utils.hpp" + + +TEST(test_performance, merge_performance_when_comprehensive_sampling) +{ + const int INSTANCE_NUM = 100; + const int MAX_CELL_NUM = 65535; + const int DIMENSION_TOTAL = 100000; + // const int INSTANCE_NUM = 2; + // const int MAX_CELL_NUM = 1000; + // const int DIMENSION_TOTAL = 1024; + Fieldstat_tag_list_wrapper *tags[DIMENSION_TOTAL]; + for (int i = 0; i < DIMENSION_TOTAL; i++) + { + tags[i] = new Fieldstat_tag_list_wrapper("my key", i); + } + + struct fieldstat *instances[INSTANCE_NUM]; + for (int i = 0; i < INSTANCE_NUM; i++) { + struct fieldstat *tmp_i = fieldstat_new(); + int cube_id = fieldstat_register_cube(tmp_i, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, MAX_CELL_NUM); + int metric_id = fieldstat_register_counter(tmp_i, cube_id, "metric name", false); + for (int j = 0; j < MAX_CELL_NUM; j++) { + int cell_id = fieldstat_cube_add(tmp_i, cube_id, tags[rand() % DIMENSION_TOTAL]->get_tag(), 1, 1); + + fieldstat_counter_incrby(tmp_i, cube_id, metric_id, cell_id, 1); + } + instances[i] = tmp_i; + } + + struct fieldstat *instance_dest = fieldstat_new(); + printf("prepare done\n"); + + clock_t start = clock(); + // getchar(); + for (int i = 0; i < INSTANCE_NUM; i++) { + fieldstat_merge(instance_dest, instances[i]); + } + // exit(0); + clock_t end = clock(); + + double elapsed_secs = double(end - start) / CLOCKS_PER_SEC; + printf("performance_test_when_comprehensive_sampling elapsed_secs: %f\n", elapsed_secs); + EXPECT_TRUE(elapsed_secs < 0.1); + + fieldstat_free(instance_dest); + for (int i = 0; i < INSTANCE_NUM; i++) { + fieldstat_free(instances[i]); + } + for (int i = 0; i < DIMENSION_TOTAL; i++) { + delete tags[i]; + } +} + +TEST(test_performance, merge_performance_when_topk_sampling){ + const int INSTANCE_NUM = 100; + const int MAX_CELL_NUM = 1024; + const int DIMENSION_TOTAL = 100000; + const int CUBE_NUM = 100; + + Fieldstat_tag_list_wrapper *tags[DIMENSION_TOTAL]; + for (int i = 0; i < DIMENSION_TOTAL; i++) + { + if (i%2) + tags[i] = new Fieldstat_tag_list_wrapper("my key", i); + else + tags[i] = new Fieldstat_tag_list_wrapper("elephant", i % 1024); + } + + Fieldstat_tag_list_wrapper *shared_tags[CUBE_NUM]; + for (int i = 0; i < CUBE_NUM; i++) + { + shared_tags[i] = new Fieldstat_tag_list_wrapper("shared key", i); + } + + struct fieldstat *instances[INSTANCE_NUM]; + for (int i = 0; i < INSTANCE_NUM; i++) { + for (int j = 0; j < CUBE_NUM; j++) { + struct fieldstat *tmp_i = fieldstat_new(); + int cube_id = fieldstat_register_cube(tmp_i, shared_tags[j]->get_tag(), 1, SAMPLING_MODE_TOPK, MAX_CELL_NUM); + int metric_id = fieldstat_register_counter(tmp_i, cube_id, "metric name", false); + for (int j = 0; j < MAX_CELL_NUM; j++) { + int cell_id = fieldstat_cube_add(tmp_i, cube_id, tags[rand() % DIMENSION_TOTAL]->get_tag(), 1, 1); + if (cell_id == -1) { + continue; + } + fieldstat_counter_incrby(tmp_i, cube_id, metric_id, cell_id, 1); + } + instances[i] = tmp_i; + } + } + + struct fieldstat *instance_dest = fieldstat_new(); + printf("prepare done\n"); + clock_t start = clock(); + // getchar(); + for (int i = 0; i < INSTANCE_NUM; i++) { + fieldstat_merge(instance_dest, instances[i]); + } + // exit(0); + clock_t end = clock(); + + double elapsed_secs = double(end - start) / CLOCKS_PER_SEC; + printf("performance_test_when_comprehensive_sampling elapsed_secs: %f\n", elapsed_secs); + EXPECT_TRUE(elapsed_secs < 0.2); + + fieldstat_free(instance_dest); + for (int i = 0; i < INSTANCE_NUM; i++) { + fieldstat_free(instances[i]); + } + for (int i = 0; i < DIMENSION_TOTAL; i++) { + delete tags[i]; + } +} + +TEST(metric_test_counter, performance_test_on_1_cells_1000000_times) +{ + // getchar(); + struct fieldstat *instance = fieldstat_new(); + fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, 10); + fieldstat_register_counter(instance, 0, "test", 0); + int cell_id = fieldstat_cube_add(instance, 0, &TEST_TAG_DOUBLE, 1, 1); + clock_t start = clock(); + for (size_t i = 0; i < 1000000; i++) { + fieldstat_counter_incrby(instance, 0, 0, cell_id, 1); + } + clock_t end = clock(); + double seconds = (double)(end - start) / CLOCKS_PER_SEC; + printf("performance_test_on_1_cells_1000000_times time cost: %f\n", seconds); + EXPECT_TRUE(seconds < 1); // 1 = 0.000001 * 1000000 + fieldstat_free(instance); +} + +TEST(metric_test_counter, performance_test_on_60000_cells_comprehensive_1000000_times) +{ + int cell_count = 60000; + struct fieldstat_tag tags[cell_count]; + for (int i = 0; i < cell_count; i++) { + tags[i] = TEST_TAG_INT; + tags[i].value_longlong = i; + } + // getchar(); + struct fieldstat *instance = fieldstat_new(); + fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, cell_count); + fieldstat_register_counter(instance, 0, "test", 0); + + clock_t start = clock(); + for (size_t i = 0; i < 1000000; i++) { + int cell_id = fieldstat_cube_add(instance, 0, &tags[i % cell_count], 1, 1); + if (cell_id < 0) { + continue; + } + fieldstat_counter_incrby(instance, 0, 0, cell_id, 1); + } + clock_t end = clock(); + double seconds = (double)(end - start) / CLOCKS_PER_SEC; + printf("performance_test_on_60000_cells_comprehensive_1000000_times time cost: %f\n", seconds); + EXPECT_TRUE(seconds < 1); // 1 = 0.000001 * 1000000 + fieldstat_free(instance); +} + +TEST(metric_test_counter, performance_test_on_1000_cells_topk_1000000_times) +{ + int cell_count = 60000; + struct fieldstat_tag tags[cell_count]; + for (int i = 0; i < cell_count; i++) { + tags[i] = TEST_TAG_INT; + // tags[i].value_longlong = rand() % 10000; + if (rand()%2) + tags[i].value_longlong = i; + else + tags[i].value_longlong = rand() % 1000; + } + struct fieldstat *instance = fieldstat_new(); + fieldstat_register_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_TOPK, 1000); + fieldstat_register_counter(instance, 0, "test", 0); + + // getchar(); + clock_t start = clock(); + for (size_t i = 0; i < 1000000; i++) { + int cell_id = fieldstat_cube_add(instance, 0, &tags[i % cell_count], 1, 1); + if (cell_id != -1) + fieldstat_counter_incrby(instance, 0, 0, cell_id, 1); + } + clock_t end = clock(); + double seconds = (double)(end - start) / CLOCKS_PER_SEC; + // exit(0); + + EXPECT_TRUE(seconds < 1); // 1 = 0.000001 * 1000000 + printf("performance_test_on_1000_cells_topk_1000000_times time cost: %f\n", seconds); + + fieldstat_free(instance); +} + +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}
\ No newline at end of file diff --git a/test/test_serialize.cpp b/test/test_serialize.cpp index 5b114fb..421862b 100644 --- a/test/test_serialize.cpp +++ b/test/test_serialize.cpp @@ -106,6 +106,9 @@ TEST(unit_test_serialize, serialize_and_deserialize_fieldstat_instance_topk) fieldstat_tag_list_arr_free(tag_list, n_cell); } +// todo: tag 重新排序 + + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); diff --git a/test/unit_test_cell_manager.cpp b/test/unit_test_cell_manager.cpp index 3517ff5..1b3656a 100644 --- a/test/unit_test_cell_manager.cpp +++ b/test/unit_test_cell_manager.cpp @@ -27,10 +27,6 @@ struct tag_hash_key *gen_key(const char *key, int value) return tag_key; } -bool sortByValue(const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) { - return a.second > b.second; -} - double cal_accuracy(vector<struct tag_hash_key *> &expected_keys, vector<struct tag_hash_key *> &test_result) { map<string, int> countMap; for (size_t i = 0; i < expected_keys.size(); i++) { @@ -39,7 +35,9 @@ double cal_accuracy(vector<struct tag_hash_key *> &expected_keys, vector<struct } std::vector<std::pair<std::string, int>> countVector(countMap.begin(), countMap.end()); - std::sort(countVector.begin(), countVector.end(), sortByValue); + std::sort(countVector.begin(), countVector.end(), [](const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) { + return a.second > b.second; + }); std::set<std::string> myset; int min_in_max_count = 0; diff --git a/test/utils.cpp b/test/utils.cpp index 6c72ac1..d54d701 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -182,6 +182,32 @@ void Fieldstat_tag_list_wrapper::print_tag_list() const printf("print end\n"); } +string Fieldstat_tag_list_wrapper::to_string() const +{ + string str = ""; + for (size_t i = 0; i < tag_list_c.n_tag; i++) + { + str += tag_list_c.tag[i].key; + str += ":"; + switch (tag_list_c.tag[i].type) + { + case TAG_INTEGER: + str += std::to_string(tag_list_c.tag[i].value_longlong); + break; + case TAG_DOUBLE: + str += std::to_string(tag_list_c.tag[i].value_double); + break; + case TAG_CSTRING: + str += tag_list_c.tag[i].value_str; + break; + default: + break; + } + str += ","; + } + return str; +} + bool Fieldstat_tag_list_wrapper::operator==(const Fieldstat_tag_list_wrapper &tag_list_wrapper) const { const struct fieldstat_tag_list *tag_list = tag_list_wrapper.get_c_struct(); diff --git a/test/utils.hpp b/test/utils.hpp index a9dde3c..25f33ab 100644 --- a/test/utils.hpp +++ b/test/utils.hpp @@ -17,6 +17,7 @@ public: explicit Fieldstat_tag_list_wrapper(const char * key, int value); explicit Fieldstat_tag_list_wrapper(const char * key, const char *value); explicit Fieldstat_tag_list_wrapper(std::uniform_int_distribution<int> &dist, int tag_count); + std::string to_string() const; // std::uniform_int_distribution<int> dist(1,100) explicit Fieldstat_tag_list_wrapper(); Fieldstat_tag_list_wrapper(const Fieldstat_tag_list_wrapper &tag_list_wrapper); |
