diff options
| author | chenzizhan <[email protected]> | 2023-05-29 10:27:56 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-05-29 10:27:56 +0800 |
| commit | 16251f9b2255ad4faf77aef5b75ebc489aabcc33 (patch) | |
| tree | 7e90309774ea218a7c2a9fee372c01bddfeb7b68 | |
| parent | a1f417a75fe3361b7645cd49389a6e4a98d12308 (diff) | |
renaming gtest, my gtest file, some annots
| -rw-r--r-- | src/fieldstat.cpp | 5 | ||||
| -rw-r--r-- | src/fieldstat_internal.h | 2 | ||||
| -rw-r--r-- | src/line_protocol_output.cpp | 4 | ||||
| -rw-r--r-- | test/src/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | test/src/gtest_fieldstat_counter_output.cpp (renamed from test/src/gtest_fieldstat_output.cpp) | 0 | ||||
| -rw-r--r-- | test/src/gtest_fieldstat_output_file_instance.cpp | 556 | ||||
| -rw-r--r-- | test/src/gtest_fieldstat_topk.cpp | 135 | ||||
| -rw-r--r-- | test/src/test_utils.cpp | 4 | ||||
| -rw-r--r-- | test/src/test_utils.hpp | 5 |
9 files changed, 370 insertions, 350 deletions
diff --git a/src/fieldstat.cpp b/src/fieldstat.cpp index ee07ac6..cacf6b9 100644 --- a/src/fieldstat.cpp +++ b/src/fieldstat.cpp @@ -450,7 +450,8 @@ long long get_metric_unit_val(struct metric *metric,enum field_calc_algo calc_ty target = &(metric->gauge); break; default: - break; + assert(0); + return 0; } value = threadsafe_counter_read(&(target->changing)); @@ -471,7 +472,7 @@ long long get_metric_unit_val(struct metric *metric,enum field_calc_algo calc_ty value=target->previous_changed; break; default: - assert(0); + assert(0); break; } return value; diff --git a/src/fieldstat_internal.h b/src/fieldstat_internal.h index 934a99d..0f2fd48 100644 --- a/src/fieldstat_internal.h +++ b/src/fieldstat_internal.h @@ -246,7 +246,7 @@ struct fieldstat_dynamic_instance struct dynamic_metric **n_thread_dynamic_metric; int n_thread; - int output_type; // 0b0000:not output, 0b1000:output file, 0b0100:output line_protocol, 0b0010: output statsd, 0b0001: output prometheus + int output_type; // 0b0000:not output 0 , 0b1000:output file 8, 0b0100:output line_protocol 4, 0b0010: output statsd 2, 0b0001: output prometheus 1 }; diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp index a753ea9..9646fdd 100644 --- a/src/line_protocol_output.cpp +++ b/src/line_protocol_output.cpp @@ -88,7 +88,7 @@ static long long read_single_metric_value(struct metric *metric, int output_type { long long value = 0; int is_refer = 1; - (output_type >= 4 && output_type < 8) + (output_type >= 4 && output_type < 8) // line protocol output, but no file output ?is_refer = 0 :is_refer = 1; metric->field_type == FIELD_TYPE_GAUGE @@ -316,6 +316,8 @@ int line_protocol_output(struct fieldstat_instance *instance) int n_cur_metric = instance->metric_cnt; //print current time instance end + // FIXME: TODO: 带histogram的话这个会直接挂吧 + output_line_protocol_single_metric(instance, n_cur_metric); output_line_protocol_table_row(instance, n_cur_table, tables_row_cnt); diff --git a/test/src/CMakeLists.txt b/test/src/CMakeLists.txt index 1fae028..7dafefd 100644 --- a/test/src/CMakeLists.txt +++ b/test/src/CMakeLists.txt @@ -9,8 +9,8 @@ add_definitions(-std=c++11) # add_executable(gtest_fieldstat_interface ${SRC} gtest_fieldstat_interface.cpp) # target_link_libraries(gtest_fieldstat_interface gtest-static heavykeeper) -# add_executable(gtest_fieldstat_output ${SRC} gtest_fieldstat_output.cpp) -# target_link_libraries(gtest_fieldstat_output gtest-static heavykeeper) +add_executable(gtest_fieldstat_counter_output ${SRC} gtest_fieldstat_counter_output.cpp) +target_link_libraries(gtest_fieldstat_counter_output gtest-static heavykeeper) # add_executable(gtest_dynamic_fieldstat ${SRC} gtest_dynamic_fieldstat.cpp) # target_link_libraries(gtest_dynamic_fieldstat gtest-static heavykeeper) @@ -18,5 +18,8 @@ add_definitions(-std=c++11) # add_executable(gtest_dynamic_fieldstat_output ${SRC} gtest_dynamic_fieldstat_output.cpp) # target_link_libraries(gtest_dynamic_fieldstat_output gtest-static heavykeeper) -add_executable(gtest_fieldstat_output_file_instance ${SRC} gtest_fieldstat_output_file_instance.cpp test_utils.cpp) +add_executable(gtest_fieldstat_output_file_instance ${SRC} gtest_fieldstat_output_file_instance.cpp) target_link_libraries(gtest_fieldstat_output_file_instance gtest-static heavykeeper) + +add_executable(gtest_fieldstat_topk ${SRC} gtest_fieldstat_topk.cpp test_utils.cpp) +target_link_libraries(gtest_fieldstat_topk gtest-static heavykeeper)
\ No newline at end of file diff --git a/test/src/gtest_fieldstat_output.cpp b/test/src/gtest_fieldstat_counter_output.cpp index 634017d..634017d 100644 --- a/test/src/gtest_fieldstat_output.cpp +++ b/test/src/gtest_fieldstat_counter_output.cpp diff --git a/test/src/gtest_fieldstat_output_file_instance.cpp b/test/src/gtest_fieldstat_output_file_instance.cpp index 59b8c21..17de4c4 100644 --- a/test/src/gtest_fieldstat_output_file_instance.cpp +++ b/test/src/gtest_fieldstat_output_file_instance.cpp @@ -10,348 +10,228 @@ extern struct prometheus_endpoint_instance g_prometheus_endpoint_instance; -// TEST(FeildStatOutput, OutputMaatTable) -// { -// int ret = 0; -// struct fieldstat_instance * instance = NULL; -// int table_id = -1; -// int output_metric_ids[54] = {0}; -// FILE *fp; -// int n_line = 0; -// int n_loops = 48; -// char line[2048] = {0}; - -// const char *output_file_path = "/tmp/fieldstat.txt"; -// const char *table_column_name[] = {"rule", "reg", "v6", "stream", "scan_cnt", "scan_bytes", "scan_cpu_time", "hit_cnt"}; -// const char *compared_table_row = "PXY_PROFILE_RUN_SCRIPT" -// " 9223372036854775807 0 0 0" -// " 0 0 0 0\n"; - -// enum field_type table_column_type[8] = {FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE}; - -// instance = fieldstat_instance_new("maat"); -// EXPECT_STREQ("maat", instance->name); - -// table_id = fieldstat_register_table(instance, "maat_info", table_column_name, table_column_type, sizeof(table_column_name)/sizeof(table_column_name[0])); -// EXPECT_EQ(0, table_id); -// ret = fieldstat_register_table_row(instance, table_id, "Sum", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_CTRL_COMPILE", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "GROUP_PXY_CTRL_COMPILE_RELATION", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "GROUP_GROUP_RELATION", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_IP", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_URL", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_URL", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_FQDN", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_FQDN_CAT", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_HOST", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_HOST_CAT", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_HTTP_SIGNATURE", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_REQ_HDR", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_RES_HDR", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_KEYWORDS", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_REQ_BODY", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_RES_BODY", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_SUBSCRIBER_ID", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_APP_ID", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_SOURCE_ADDR", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_DESTINATION_ADDR", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_COMPILE", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_GROUP", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_HTTP_URL", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_HTTP_COOKIE", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_TRUSTED_CA_CERT", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_OBJ_TRUSTED_CA_CRL", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_PROFILE_RESPONSE_PAGES", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_HIJACK_FILES", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_INSERT_SCRIPTS", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_COMPILE", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_PROFILE_TRAFFIC_MIRROR", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_PROFILE_DECRYPTION", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_AS_NUMBER", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_SOURCE_ASN", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_DESTINATION_ASN", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_GEO_LOCATION", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_SOURCE_LOCATION", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_DESTINATION_LOCATION", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_DOH_QNAME", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_DOH_HOST", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_DOH_HOST_CAT", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_SSL_FINGERPRINT", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); -// ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_RUN_SCRIPTS", NULL, 0, output_metric_ids); -// EXPECT_EQ(0, ret); - -// ret = fieldstat_set_local_output(instance, output_file_path, "default"); -// EXPECT_EQ(0, ret); -// fieldstat_instance_start(instance); -// sleep(1); -// fieldstat_value_incrby(instance, output_metric_ids[0], 9223372036854775807); -// sleep(2); - -// fp = fopen("/tmp/fieldstat.txt", "r"); -// EXPECT_NE(nullptr, fp); - -// while(!feof(fp)) -// { -// if(NULL == fgets(line, sizeof(line), fp)) -// { -// continue; -// } -// if(n_line == 46) -// { -// EXPECT_STREQ(compared_table_row, line); -// } -// n_line++; -// } -// fclose(fp); -// EXPECT_EQ(n_line, n_loops); - -// fieldstat_instance_free(instance); -// } - -// TEST(FeildStatOutput, OutputMaatGaugeType) -// { -// int ret = 0; -// struct fieldstat_instance * instance = NULL; -// int metric_id = -1; -// FILE *fp; -// int n_line = 0; -// int n_loops = 6; -// char line[2048] = {0}; - -// const char *output_file_path = "/tmp/fieldstat.txt"; -// const char *compared_row = " version: 922 threads: 0 tables: 0 plug_cached: 0 plug_acc: 0 group: 0 \n"; - -// instance = fieldstat_instance_new("maat"); -// EXPECT_STREQ("maat", instance->name); - -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "version", NULL, 0); -// EXPECT_EQ(0, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "threads", NULL, 0); -// EXPECT_EQ(1, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "tables", NULL, 0); -// EXPECT_EQ(2, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "plug_cached", NULL, 0); -// EXPECT_EQ(3, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "plug_acc", NULL, 0); -// EXPECT_EQ(4, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "group", NULL, 0); -// EXPECT_EQ(5, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "not_grp", NULL, 0); -// EXPECT_EQ(6, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "compile", NULL, 0); -// EXPECT_EQ(7, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "garbage_num", NULL, 0); -// EXPECT_EQ(8, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "outer_mid", NULL, 0); -// EXPECT_EQ(9, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "z_stream", NULL, 0); -// EXPECT_EQ(10, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "nt_grp_hit", NULL, 0); -// EXPECT_EQ(11, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "scan_bytes", NULL, 0); -// EXPECT_EQ(12, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "scan_times", NULL, 0); -// EXPECT_EQ(13, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "update_err", NULL, 0); -// EXPECT_EQ(14, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "scan_error", NULL, 0); -// EXPECT_EQ(15, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "cmd_commit", NULL, 0); -// EXPECT_EQ(16, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "cmd_in_q", NULL, 0); -// EXPECT_EQ(17, metric_id); -// metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "line_cmd/s", NULL, 0); -// EXPECT_EQ(18, metric_id); - -// ret = fieldstat_set_local_output(instance, output_file_path, "default"); -// EXPECT_EQ(0, ret); -// fieldstat_instance_start(instance); -// sleep(1); -// fieldstat_value_incrby(instance, 0, 922); -// sleep(2); - -// fp = fopen("/tmp/fieldstat.txt", "r"); -// EXPECT_NE(nullptr, fp); - -// while(!feof(fp)) -// { -// if(NULL == fgets(line, sizeof(line), fp)) -// { -// continue; -// } -// if(n_line == 1) -// { -// EXPECT_STREQ(compared_row, line); -// } -// n_line++; -// } -// fclose(fp); -// EXPECT_EQ(n_line, n_loops); - -// fieldstat_instance_free(instance); -// } - - - -#include <regex> - -std::vector<std::string> add_to_heavy_keeper(struct fieldstat_instance * instance, int metric_id, std::vector<std::pair<std::string, int>> &input) -{ - for (auto item : input) { - fieldstat_value_incrby_topK(instance, metric_id, item.first.c_str(), item.first.size(), item.second); - } - - //rank by count - std::sort(input.begin(), input.end(), [](std::pair<std::string, int> a, std::pair<std::string, int> b) { - return a.second > b.second; - }); - - std::vector<std::string> patterns; - for (auto item : input) { - std::string pattern = ".*" + item.first + ".*" + std::to_string(item.second) + ".*"; - patterns.push_back(pattern); - } - return patterns; -} - -std::string convert_default_pattern_to_json(std::vector<std::string> &patterns) -{ - std::string pattern; - for (size_t i = 0; i < patterns.size(); i++) { - pattern += patterns[i]; - } - return pattern; -} - -Heavy_keeper_tester *Construct_default_tester(int output_window = 1) -{ - Heavy_keeper_tester *tester = new Heavy_keeper_tester("heavy_keeper_test"); - - tester->register_metric("field1", 10, output_window); - tester->register_metric("field2", 10, output_window); - - tester->take_action("field1", "hello", 10); - tester->take_action("field1", "HELLO", 5); - tester->take_action("field1", "123123123123123123jjkasdlfjasldjkfkjjk", 9999); - - tester->take_action("field2", "world", 10); - tester->take_action("field2", "WORLD", 42); - - printf("Construct_default_tester done\n"); - - return tester; -} - -TEST(FeildStatOutput, output_topk_default) -{ - Heavy_keeper_tester *tester = Construct_default_tester(); - std::string pattern_expect = tester->get_expected_pattern("default"); - std::string file_content = tester->write_to_file("/tmp/czzdefault.txt", "default"); - - EXPECT_TRUE(std::regex_match(file_content, std::regex(pattern_expect))); - - delete tester; -} - -TEST(FeildStatOutput, output_topk_json) +TEST(FeildStatOutput, OutputMaatTable) { - Heavy_keeper_tester *tester = Construct_default_tester(); - std::string pattern_expect = tester->get_expected_pattern("json"); - std::string file_content = tester->write_to_file("/tmp/czzjson.txt", "json"); - EXPECT_TRUE(std::regex_match(file_content, std::regex(pattern_expect))); - delete tester; + int ret = 0; + struct fieldstat_instance * instance = NULL; + int table_id = -1; + int output_metric_ids[54] = {0}; + FILE *fp; + int n_line = 0; + int n_loops = 48; + char line[2048] = {0}; + + const char *output_file_path = "/tmp/fieldstat.txt"; + const char *table_column_name[] = {"rule", "reg", "v6", "stream", "scan_cnt", "scan_bytes", "scan_cpu_time", "hit_cnt"}; + const char *compared_table_row = "PXY_PROFILE_RUN_SCRIPT" + " 9223372036854775807 0 0 0" + " 0 0 0 0\n"; + + enum field_type table_column_type[8] = {FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE}; + + instance = fieldstat_instance_new("maat"); + EXPECT_STREQ("maat", instance->name); + + table_id = fieldstat_register_table(instance, "maat_info", table_column_name, table_column_type, sizeof(table_column_name)/sizeof(table_column_name[0])); + EXPECT_EQ(0, table_id); + ret = fieldstat_register_table_row(instance, table_id, "Sum", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_CTRL_COMPILE", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "GROUP_PXY_CTRL_COMPILE_RELATION", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "GROUP_GROUP_RELATION", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_IP", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_URL", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_URL", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_FQDN", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_FQDN_CAT", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_HOST", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_HOST_CAT", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_HTTP_SIGNATURE", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_REQ_HDR", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_RES_HDR", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_KEYWORDS", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_REQ_BODY", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_HTTP_RES_BODY", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_SUBSCRIBER_ID", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_APP_ID", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_SOURCE_ADDR", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_DESTINATION_ADDR", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_COMPILE", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_GROUP", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_HTTP_URL", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_CACHE_HTTP_COOKIE", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_TRUSTED_CA_CERT", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_OBJ_TRUSTED_CA_CRL", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_PROFILE_RESPONSE_PAGES", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_HIJACK_FILES", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_INSERT_SCRIPTS", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_COMPILE", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_PROFILE_TRAFFIC_MIRROR", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_PROFILE_DECRYPTION", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_AS_NUMBER", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_SOURCE_ASN", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_DESTINATION_ASN", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_OBJ_GEO_LOCATION", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_SOURCE_LOCATION", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_SECURITY_DESTINATION_LOCATION", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_DOH_QNAME", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_DOH_HOST", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "TSG_FIELD_DOH_HOST_CAT", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_SSL_FINGERPRINT", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + ret = fieldstat_register_table_row(instance, table_id, "PXY_PROFILE_RUN_SCRIPTS", NULL, 0, output_metric_ids); + EXPECT_EQ(0, ret); + + ret = fieldstat_set_local_output(instance, output_file_path, "default"); + EXPECT_EQ(0, ret); + fieldstat_instance_start(instance); + sleep(1); + fieldstat_value_incrby(instance, output_metric_ids[0], 9223372036854775807); + sleep(2); + + fp = fopen("/tmp/fieldstat.txt", "r"); + EXPECT_NE(nullptr, fp); + + while(!feof(fp)) + { + if(NULL == fgets(line, sizeof(line), fp)) + { + continue; + } + if(n_line == 46) + { + EXPECT_STREQ(compared_table_row, line); + } + n_line++; + } + fclose(fp); + EXPECT_EQ(n_line, n_loops); + + fieldstat_instance_free(instance); } -std::vector<std::string> splitString(const std::string& str) +TEST(FeildStatOutput, OutputMaatGaugeType) { - std::vector<std::string> tokens; - - std::string::size_type pos = 0; - std::string::size_type prev = 0; - while ((pos = str.find('\n', prev)) != std::string::npos) { - tokens.push_back(str.substr(prev, pos - prev)); - prev = pos + 1; - } - tokens.push_back(str.substr(prev)); - - return tokens; -} - -const std::regex CONTENT_PATTERN("^([0-9]+).*"); -TEST(FeildStatOutput, output_topk_default_flushed_up) -{ - Heavy_keeper_tester *tester = Construct_default_tester(1); - std::string pattern_expect = tester->get_expected_pattern("default"); - std::string file_content1 = tester->write_to_file("/tmp/czzdefault.txt", "default"); - std::string file_content2 = tester->write_to_file("/tmp/czzdefault.txt", "default"); - - EXPECT_TRUE(std::regex_match(file_content1, std::regex(pattern_expect))); - std::vector<std::string> lines2 = splitString(file_content2); - for (auto line : lines2) { // only have headers - EXPECT_FALSE(std::regex_match(line, std::regex(CONTENT_PATTERN))); - } - - delete tester; -} - -TEST(FeildStatOutput, output_topk_default_accumulated) -{ - Heavy_keeper_tester *tester = Construct_default_tester(0); - - (void)tester->write_to_file("/tmp/czzdefault.txt", "default"); - - tester->take_action("field1", "hello", 20); // hello set to 30 - tester->take_action("field1", "Hello_new", 20); - std::string expected2 = tester->get_expected_pattern("default"); - - std::string file_content2 = tester->write_to_file("/tmp/czzdefault.txt", "default"); - - EXPECT_TRUE(std::regex_match(file_content2, std::regex(expected2))); - - delete tester; + int ret = 0; + struct fieldstat_instance * instance = NULL; + int metric_id = -1; + FILE *fp; + int n_line = 0; + int n_loops = 6; + char line[2048] = {0}; + + const char *output_file_path = "/tmp/fieldstat.txt"; + const char *compared_row = " version: 922 threads: 0 tables: 0 plug_cached: 0 plug_acc: 0 group: 0 \n"; + + instance = fieldstat_instance_new("maat"); + EXPECT_STREQ("maat", instance->name); + + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "version", NULL, 0); + EXPECT_EQ(0, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "threads", NULL, 0); + EXPECT_EQ(1, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "tables", NULL, 0); + EXPECT_EQ(2, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "plug_cached", NULL, 0); + EXPECT_EQ(3, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "plug_acc", NULL, 0); + EXPECT_EQ(4, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "group", NULL, 0); + EXPECT_EQ(5, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "not_grp", NULL, 0); + EXPECT_EQ(6, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "compile", NULL, 0); + EXPECT_EQ(7, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "garbage_num", NULL, 0); + EXPECT_EQ(8, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "outer_mid", NULL, 0); + EXPECT_EQ(9, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "z_stream", NULL, 0); + EXPECT_EQ(10, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "nt_grp_hit", NULL, 0); + EXPECT_EQ(11, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "scan_bytes", NULL, 0); + EXPECT_EQ(12, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "scan_times", NULL, 0); + EXPECT_EQ(13, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "update_err", NULL, 0); + EXPECT_EQ(14, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "scan_error", NULL, 0); + EXPECT_EQ(15, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "cmd_commit", NULL, 0); + EXPECT_EQ(16, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "cmd_in_q", NULL, 0); + EXPECT_EQ(17, metric_id); + metric_id = fieldstat_register(instance, FIELD_TYPE_GAUGE, "line_cmd/s", NULL, 0); + EXPECT_EQ(18, metric_id); + + ret = fieldstat_set_local_output(instance, output_file_path, "default"); + EXPECT_EQ(0, ret); + fieldstat_instance_start(instance); + sleep(1); + fieldstat_value_incrby(instance, 0, 922); + sleep(2); + + fp = fopen("/tmp/fieldstat.txt", "r"); + EXPECT_NE(nullptr, fp); + + while(!feof(fp)) + { + if(NULL == fgets(line, sizeof(line), fp)) + { + continue; + } + if(n_line == 1) + { + EXPECT_STREQ(compared_row, line); + } + n_line++; + } + fclose(fp); + EXPECT_EQ(n_line, n_loops); + + fieldstat_instance_free(instance); } int main(int argc, char *argv[]) diff --git a/test/src/gtest_fieldstat_topk.cpp b/test/src/gtest_fieldstat_topk.cpp new file mode 100644 index 0000000..6a7df2b --- /dev/null +++ b/test/src/gtest_fieldstat_topk.cpp @@ -0,0 +1,135 @@ +#include <stdio.h> +#include <string.h> + +#include <regex> +#include <iostream> + +#include <gtest/gtest.h> + +#include "fieldstat.h" +#include "fieldstat_internal.h" + +#include "test_utils.hpp" + + +std::vector<std::string> add_to_heavy_keeper(struct fieldstat_instance * instance, int metric_id, std::vector<std::pair<std::string, int>> &input) +{ + for (auto item : input) { + fieldstat_value_incrby_topK(instance, metric_id, item.first.c_str(), item.first.size(), item.second); + } + + //rank by count + std::sort(input.begin(), input.end(), [](std::pair<std::string, int> a, std::pair<std::string, int> b) { + return a.second > b.second; + }); + + std::vector<std::string> patterns; + for (auto item : input) { + std::string pattern = ".*" + item.first + ".*" + std::to_string(item.second) + ".*"; + patterns.push_back(pattern); + } + return patterns; +} + +std::string convert_default_pattern_to_json(std::vector<std::string> &patterns) +{ + std::string pattern; + for (size_t i = 0; i < patterns.size(); i++) { + pattern += patterns[i]; + } + return pattern; +} + +Heavy_keeper_tester *Construct_default_tester(int output_window = 1) +{ + Heavy_keeper_tester *tester = new Heavy_keeper_tester("heavy_keeper_test"); + + tester->register_metric("field1", 10, output_window); + tester->register_metric("field2", 10, output_window); + + tester->take_action("field1", "hello", 10); + tester->take_action("field1", "HELLO", 5); + tester->take_action("field1", "123123123123123123jjkasdlfjasldjkfkjjk", 9999); + + tester->take_action("field2", "world", 10); + tester->take_action("field2", "WORLD", 42); + + printf("Construct_default_tester done\n"); + + return tester; +} + +TEST(FeildStatOutput, output_topk_default) +{ + Heavy_keeper_tester *tester = Construct_default_tester(); + std::string pattern_expect = tester->get_expected_pattern("default"); + std::string file_content = tester->write_to_file("/tmp/czzdefault.txt", "default"); + + EXPECT_TRUE(std::regex_match(file_content, std::regex(pattern_expect))); + + delete tester; +} + +TEST(FeildStatOutput, output_topk_json) +{ + Heavy_keeper_tester *tester = Construct_default_tester(); + std::string pattern_expect = tester->get_expected_pattern("json"); + std::string file_content = tester->write_to_file("/tmp/czzjson.txt", "json"); + EXPECT_TRUE(std::regex_match(file_content, std::regex(pattern_expect))); + delete tester; +} + +std::vector<std::string> splitString(const std::string& str) +{ + std::vector<std::string> tokens; + + std::string::size_type pos = 0; + std::string::size_type prev = 0; + while ((pos = str.find('\n', prev)) != std::string::npos) { + tokens.push_back(str.substr(prev, pos - prev)); + prev = pos + 1; + } + tokens.push_back(str.substr(prev)); + + return tokens; +} + +const std::regex CONTENT_PATTERN("^([0-9]+).*"); +TEST(FeildStatOutput, output_topk_default_flushed_up) +{ + Heavy_keeper_tester *tester = Construct_default_tester(1); + std::string pattern_expect = tester->get_expected_pattern("default"); + std::string file_content1 = tester->write_to_file("/tmp/czzdefault.txt", "default"); // changing not flushed + std::string file_content2 = tester->write_to_file("/tmp/czzdefault.txt", "default"); // changing deleted + + EXPECT_TRUE(std::regex_match(file_content1, std::regex(pattern_expect))); + std::vector<std::string> lines2 = splitString(file_content2); + for (auto line : lines2) { // only have headers + EXPECT_FALSE(std::regex_match(line, std::regex(CONTENT_PATTERN))); + } + + delete tester; +} + +TEST(FeildStatOutput, output_topk_default_accumulated) +{ + Heavy_keeper_tester *tester = Construct_default_tester(0); + + (void)tester->write_to_file("/tmp/czzdefault.txt", "default"); + + tester->take_action("field1", "hello", 20); // hello set to 30 + tester->take_action("field1", "Hello_new", 20); + std::string expected2 = tester->get_expected_pattern("default"); + + std::string file_content2 = tester->write_to_file("/tmp/czzdefault.txt", "default"); + + EXPECT_TRUE(std::regex_match(file_content2, std::regex(expected2))); + + delete tester; +} + +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}
\ No newline at end of file diff --git a/test/src/test_utils.cpp b/test/src/test_utils.cpp index 05490d6..f1fa21a 100644 --- a/test/src/test_utils.cpp +++ b/test/src/test_utils.cpp @@ -86,7 +86,6 @@ vector<pair<string, int>> Heavy_keeper_tester::get_expected_topk(const std::stri std::string Heavy_keeper_tester::get_expected_pattern(std::string method) { - // get a pattern sort_map(metric_map); if (string(method) == "json") { @@ -128,7 +127,10 @@ std::string Heavy_keeper_tester::get_expected_pattern(std::string method) cout << "method not supported" << endl; return ""; } +} +void Heavy_keeper_tester::new_window() +{ actions.clear(); } diff --git a/test/src/test_utils.hpp b/test/src/test_utils.hpp index cc678bb..23fb72e 100644 --- a/test/src/test_utils.hpp +++ b/test/src/test_utils.hpp @@ -7,10 +7,6 @@ #include "fieldstat.h" #include "fieldstat_internal.h" - -// TODO: 1 写创建Tester,写一个instance, 对应的析构 - - class Reg_str { public: @@ -70,6 +66,7 @@ class Heavy_keeper_tester void take_action(const char *metric_name, const char *member, int value); std::string write_to_file(const char *output_file_path, const char *method); std::string get_expected_pattern(std::string method); + void new_window(); private: struct action { |
