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 | |
| parent | 559de7cfddaeeea7ac6e9c45100c5eb723ea1484 (diff) | |
new-ver cppcheck, new tests. push for testing CI
| -rw-r--r-- | CMakeLists.txt | 79 | ||||
| -rw-r--r-- | src/fieldstat.c | 79 | ||||
| -rw-r--r-- | src/metrics/metric.c | 38 | ||||
| -rw-r--r-- | src/metrics/metric.h | 2 | ||||
| -rw-r--r-- | src/metrics/st_hyperloglog.c | 2 | ||||
| -rw-r--r-- | src/tags/heavy_keeper.c | 52 | ||||
| -rw-r--r-- | src/tags/heavy_keeper.h | 6 | ||||
| -rw-r--r-- | src/tags/my_ut_hash.c | 30 | ||||
| -rw-r--r-- | src/tags/my_ut_hash_inner.h | 2 | ||||
| -rw-r--r-- | src/tags/sorted_set.c | 30 | ||||
| -rw-r--r-- | src/tags/sorted_set.h | 2 | ||||
| -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 |
22 files changed, 517 insertions, 386 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c23af18..f9308fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,44 +28,47 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "/opt/MESA" CACHE PATH "default install path" FORCE)
endif()
-# set(CPP_BIN_PATH "/home/chenzizhan/cppcheck/bin")
-# find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck HINTS ${CPP_BIN_PATH})
-# if (CMAKE_CXX_CPPCHECK)
-# list(
-# APPEND CMAKE_CXX_CPPCHECK
-# "--enable=all"
-# "--error-exitcode=1"
-# "--suppress=unreachableCode"
-# "--suppress=unusedFunction"
-# "--suppress=missingInclude"
-# "--suppress=uselessAssignmentPtrArg"
-# "--suppress=unmatchedSuppression"
-# "--suppress=internalAstError"
-# "--suppress=unmatchedSuppression"
-# "--suppress=unreadVariable"
-# "--suppress=memleakOnRealloc"
-# "--suppress=redundantAssignment"
-# "--suppress=constParameter"
-# "--suppress=unsignedLessThanZero"
-# "--suppress=variableScope"
-# "--suppress=cstyleCast"
-# "--suppress=objectIndex"
-# "--suppress=shadowVariable"
-# "--suppress=nullPointerRedundantCheck"
-# "--suppress=ctunullpointer"
-# "--suppress=shadowFunction"
-# "--suppress=shiftTooManyBitsSigned"
-# "--suppress=preprocessorErrorDirective"
-# "--suppress=*:${PROJECT_SOURCE_DIR}/vendors/mpack/mpack.c"
-# "--suppress=*:${PROJECT_SOURCE_DIR}/test/utils.hpp"
-# "--suppress=*:${PROJECT_SOURCE_DIR}/test/unit_test_serialize.cpp"
-# "--suppress=*:${PROJECT_SOURCE_DIR}/test/test_fuzz_test.cpp"
-# # "--suppress=*:${PROJECT_SOURCE_DIR}/test/unit_test_fast_hash.cpp"
-# )
-# set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK})
-# else()
-# message(FATAL_ERROR "Could not find the program cppcheck.")
-# endif()
+set(CPP_BIN_PATH "/home/chenzizhan/cppcheck/cfg")
+find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck HINTS ${CPP_BIN_PATH})
+if (CMAKE_CXX_CPPCHECK)
+ list(
+ APPEND CMAKE_CXX_CPPCHECK
+ "--enable=all"
+ "--suppress=missingIncludeSystem"
+ "--error-exitcode=1"
+ "--suppress=unreachableCode"
+ "--suppress=unusedFunction"
+ "--suppress=missingInclude"
+ "--suppress=uselessAssignmentPtrArg"
+ "--suppress=unmatchedSuppression"
+ "--suppress=internalAstError"
+ "--suppress=unmatchedSuppression"
+ "--suppress=unreadVariable"
+ "--suppress=memleakOnRealloc"
+ "--suppress=redundantAssignment"
+ "--suppress=constParameter"
+ "--suppress=unsignedLessThanZero"
+ "--suppress=variableScope"
+ "--suppress=cstyleCast"
+ "--suppress=objectIndex"
+ "--suppress=shadowVariable"
+ "--suppress=nullPointerRedundantCheck"
+ "--suppress=ctunullpointer"
+ "--suppress=shadowFunction"
+ "--suppress=shiftTooManyBitsSigned"
+ "--suppress=preprocessorErrorDirective"
+ "--suppress=constParameterCallback:${PROJECT_SOURCE_DIR}/src/tags/sorted_set.c"
+ "--suppress=constParameterPointer:${PROJECT_SOURCE_DIR}/src/tags/sorted_set.c"
+ "--suppress=*:${PROJECT_SOURCE_DIR}/vendors/*"
+ "--suppress=*:${PROJECT_SOURCE_DIR}/test/utils.hpp"
+ "--suppress=*:${PROJECT_SOURCE_DIR}/test/unit_test_serialize.cpp"
+ "--suppress=*:${PROJECT_SOURCE_DIR}/test/test_fuzz_test.cpp"
+ # "--suppress=*:${PROJECT_SOURCE_DIR}/test/unit_test_fast_hash.cpp"
+ )
+ set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK})
+else()
+ message(FATAL_ERROR "Could not find the program cppcheck.")
+endif()
#for ASAN
set(ASAN_OPTION "OFF" CACHE STRING " set asan type chosen by the user, using OFF as default")
diff --git a/src/fieldstat.c b/src/fieldstat.c index 3c976bb..5769011 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -240,7 +240,6 @@ int fieldstat_register_cube(struct fieldstat *instance, const struct fieldstat_t return cube_id; } -// todo: 仅添加的话不用更新维护id tag map,merge 的时候和序列化的时候才需要 int fieldstat_cube_add(struct fieldstat *instance, int cube_id, const struct fieldstat_tag *tags, size_t n_tag, long long increment) { struct fs_cube *cube = instance->cube[cube_id]; @@ -582,7 +581,7 @@ struct fieldstat *fieldstat_deserialize(const char *blob, size_t blob_size) struct fs_cube *fieldstat_cube_dup(const struct fs_cube *cube) { struct fs_cube *cube_dup = fieldstat_cube_info_init(cube->shared_tags, cube->n_shared_tags, cube->sampling_mode, cube->max_n_cell); - struct cell_manager *cm_src = cube->cell_manager; + const struct cell_manager *cm_src = cube->cell_manager; struct cell_manager *cm_dup = cell_manager_copy(cm_src); cube_dup->cell_manager = cm_dup; @@ -594,20 +593,19 @@ struct fs_cube *fieldstat_cube_dup(const struct fs_cube *cube) return cube_dup; } -int fieldstat_cube_merge_comprehensive(struct fs_cube *dest, const struct fs_cube *src, int *metric_id_src_dest_map) +void fieldstat_cube_merge_comprehensive(struct fs_cube *dest, const struct fs_cube *src, const int *metric_id_src_dest_map) { - struct cell_manager *cell_manager_src = src->cell_manager; + const struct cell_manager *cell_manager_src = src->cell_manager; struct cell_manager *cell_manager_dest = dest->cell_manager; - // merge cell manager int arr_len_src = 0; const struct tag_hash_key **tag_arr_src = cell_manager_dump(cell_manager_src, &arr_len_src); 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("ERR: tag_src is NULL\n"); - // the tag_src is continuous - break; + // the tag_src is continuous, so there may not be NULL in the middle + printf("ERR: tag_src is NULL, id: %d\n", cell_id_src); + continue; } int cell_id_final = cell_manager_add_cell(cell_manager_dest, tag_src); @@ -619,23 +617,19 @@ int fieldstat_cube_merge_comprehensive(struct fs_cube *dest, const struct fs_cub int tmp_ret = metric_merge_or_copy_cell(dest->metrics[metric_id_dest], src->metrics[metric_id_src], cell_id_final, cell_id_src); if (tmp_ret == -1) { printf("ERR: metric_merge_or_copy_cell failed\n"); - } else if (tmp_ret == 1) { - printf("metric_id_src: %d has no cell: %d\n", metric_id_src, cell_id_src); } } } - - return 0; } -int fieldstat_cube_merge_topk(struct fs_cube *dest, const struct fs_cube *src, int *metric_id_src_dest_map) +void fieldstat_cube_merge_topk(struct fs_cube *dest, const struct fs_cube *src, const int *metric_id_src_dest_map) { - struct cell_manager *cell_manager_src = src->cell_manager; + const struct cell_manager *cell_manager_src = src->cell_manager; struct cell_manager *cell_manager_dest = dest->cell_manager; - int *cell_id_added; - int *cell_id_old; + int *cell_id_added = NULL; + int *cell_id_old = NULL; int n_cell_id_added = 0; - int *cell_id_popped = 0; + int *cell_id_popped = NULL; int n_cell_id_popped = 0; cell_manager_merge_topk(cell_manager_dest, cell_manager_src, &cell_id_popped, &n_cell_id_popped, &cell_id_old, &cell_id_added, &n_cell_id_added); @@ -649,9 +643,8 @@ int fieldstat_cube_merge_topk(struct fs_cube *dest, const struct fs_cube *src, i metric_merge_or_copy_cell(dest->metrics[metric_id_dest], src->metrics[j], tmp_id_dest, tmp_id_src); } } - // chances are that: a cell exists in both dest and src, but it is not in final. In this case, these cells are merged and popped. + // chances are that: a cell exists in both dest and src, but it is not in final. In this case, these cells are both in cell_id_added and cell_id_popped. // Since all cells are counter, which is easy to merge, we just delete these cells after merging them. - // todo: 返回的值包括新添加和old, 并且把merge or copy 分开 for (int i = 0; i < n_cell_id_popped;i++) { int id = cell_id_popped[i]; @@ -659,15 +652,18 @@ int fieldstat_cube_merge_topk(struct fs_cube *dest, const struct fs_cube *src, i metric_delete_cell(dest->metrics[j], id); } } - - free(cell_id_popped); - free(cell_id_added); - free(cell_id_old); - - return 0; + if (cell_id_popped != NULL) { + free(cell_id_popped); + } + if (cell_id_added != NULL) { + free(cell_id_added); + } + if (cell_id_old != NULL) { + free(cell_id_old); + } } -int fieldstat_cube_merge(struct fs_cube *dest, const struct fs_cube *src) +void fieldstat_cube_merge(struct fs_cube *dest, const struct fs_cube *src) { struct metric_name_id_map *name_id_map_dest = dest->metric_name_id_map; if (name_id_map_dest == NULL) { @@ -687,11 +683,10 @@ int fieldstat_cube_merge(struct fs_cube *dest, const struct fs_cube *src) metric_id_src_dest_map[metric_id_src] = metric_id_dest; } - if (dest->sampling_mode == SAMPLING_MODE_COMPREHENSIVE) { return fieldstat_cube_merge_comprehensive(dest, src, metric_id_src_dest_map); } - return fieldstat_cube_merge_topk(dest, src, metric_id_src_dest_map); + fieldstat_cube_merge_topk(dest, src, metric_id_src_dest_map); } int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) @@ -705,7 +700,7 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) instance->max_n_cube + src->max_n_cube); // Just make sure shared_tag_cube_manager is big enough. instance->shared_tag_cube_manager = tag_cube_id_map; for (int i = 0; i < n_cube_dest; i++) { - struct fs_cube *cube_dest = instance->cube[i]; + const struct fs_cube *cube_dest = instance->cube[i]; if (cube_dest == NULL) { continue; } @@ -717,7 +712,7 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) int ret = 0; for (int i = 0; i < n_cube_src; i++) { - struct fs_cube *cube_src = src->cube[i]; + const struct fs_cube *cube_src = src->cube[i]; if (cube_src == NULL) { continue; } @@ -735,9 +730,7 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) tag_hash_key_free(shared_tag_key_src); continue; } - if (fieldstat_cube_merge(instance->cube[cube_id_tmp], cube_src) == -1) { - ret = -1; - } + fieldstat_cube_merge(instance->cube[cube_id_tmp], cube_src); } tag_hash_key_free(shared_tag_key_src); } @@ -759,7 +752,7 @@ void fieldstat_get_cubes(const struct fieldstat *instance, int **cube_ids, int * int *tmp_ids = (int *)malloc(sizeof(int) * instance->valid_cube_arr_length); for (int i = 0; i < instance->valid_cube_arr_length; i++) { - struct fs_cube *tmp_cube = instance->cube[i]; + const struct fs_cube *tmp_cube = instance->cube[i]; if (tmp_cube == NULL) { continue; } @@ -782,7 +775,7 @@ void fieldstat_get_cubes(const struct fieldstat *instance, int **cube_ids, int * int fieldstat_get_max_metric_id(const struct fieldstat *instance, int cube_id) { - struct fs_cube *cube = instance->cube[cube_id]; + const struct fs_cube *cube = instance->cube[cube_id]; if (cube == NULL) { printf("ERR: fieldstat_get_max_metric_id cube is not registered yet\n"); return -1; @@ -793,7 +786,7 @@ int fieldstat_get_max_metric_id(const struct fieldstat *instance, int cube_id) void fieldstat_get_cells(const struct fieldstat *instance, int cube_id, int metric_id, int **cell_ids, struct fieldstat_tag_list **tag_list, size_t *n_cell) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL) { printf("ERR: metric or cube is not registered yet\n"); return; @@ -857,7 +850,7 @@ struct fieldstat_tag_list *fieldstat_get_shared_tags(const struct fieldstat *ins long long fieldstat_counter_get(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_COUNTER) { printf("ERR: fieldstat_counter_get metrics is not registered yet\n"); return -1; @@ -868,7 +861,7 @@ long long fieldstat_counter_get(const struct fieldstat *instance, int cube_id, i double fieldstat_hll_get(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HLL) { printf("ERR: fieldstat_hll_get metrics is not registered yet\n"); return -1; @@ -879,7 +872,7 @@ double fieldstat_hll_get(const struct fieldstat *instance, int cube_id, int metr long long fieldstat_histogram_value_at_percentile(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id, double percentile) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HISTOGRAM) { printf("ERR: fieldstat_histogram_value_at_percentile metrics is not registered yet\n"); return -1; @@ -890,7 +883,7 @@ long long fieldstat_histogram_value_at_percentile(const struct fieldstat *instan long long fieldstat_histogram_count_le_value(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id, long long value) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL || metric_get_type(metric) != METRIC_TYPE_HISTOGRAM) { printf("ERR: fieldstat_histogram_value_le_value metrics is not registered yet\n"); return -1; @@ -901,7 +894,7 @@ long long fieldstat_histogram_count_le_value(const struct fieldstat *instance, i void fieldstat_get_serialized_blob(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id, char **blob, size_t *blob_size) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL) { printf("ERR: fieldstat_get_serialized_blob metrics is not registered yet\n"); return; @@ -928,7 +921,7 @@ void fieldstat_tag_list_arr_free(struct fieldstat_tag_list *tag_list, size_t n_c const char *fieldstat_get_metric_name(const struct fieldstat *instance, int cube_id, int metric_id) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL) { printf("ERR: metrics is not registered yet\n"); return NULL; @@ -939,7 +932,7 @@ const char *fieldstat_get_metric_name(const struct fieldstat *instance, int cube enum metric_type fieldstat_get_metric_type(const struct fieldstat *instance, int cube_id, int metric_id) { - struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); + const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id); if (metric == NULL) { printf("ERR: metrics is not registered yet\n"); return -1; diff --git a/src/metrics/metric.c b/src/metrics/metric.c index 2b1ec33..eacd4b9 100644 --- a/src/metrics/metric.c +++ b/src/metrics/metric.c @@ -147,7 +147,7 @@ static void metric_scheme_counter_free(struct metric_measure_data *data) */ static void metric_scheme_counter_serialize(const struct metric_measure_data *data, char **blob, size_t *blob_size) { - struct metric_counter_or_gauge *counter = data->counter; + const struct metric_counter_or_gauge *counter = data->counter; mpack_writer_t writer; mpack_writer_init_growable(&writer, blob, blob_size); mpack_build_map(&writer); @@ -190,7 +190,7 @@ static int metric_scheme_counter_merge(struct metric_measure_data *pthis, const static int metric_scheme_counter_copy(struct metric_measure_data *pthis, const struct metric_measure_data *from) { struct metric_counter_or_gauge *counter = pthis->counter; - struct metric_counter_or_gauge *from_counter = from->counter; + const struct metric_counter_or_gauge *from_counter = from->counter; counter->value = from_counter->value; counter->type = from_counter->type; return 0; @@ -234,24 +234,17 @@ static void metric_scheme_hll_free(struct metric_measure_data *data) static void metric_scheme_hll_serialize(const struct metric_measure_data *data, char **blob, size_t *blob_size) { - struct ST_hyperloglog *hll = data->hll; - ST_hyperloglog_serialize(hll, blob, blob_size); + ST_hyperloglog_serialize(data->hll, blob, blob_size); } static int metric_scheme_hll_merge(struct metric_measure_data *pthis, const struct metric_measure_data *from) { - struct ST_hyperloglog *hll = pthis->hll; - struct ST_hyperloglog *from_hll = from->hll; - - return ST_hyperloglog_merge(hll, from_hll); + return ST_hyperloglog_merge(pthis->hll, from->hll); } static int metric_scheme_hll_copy(struct metric_measure_data *pthis, const struct metric_measure_data *from) { - struct ST_hyperloglog *hll = pthis->hll; - struct ST_hyperloglog *from_hll = from->hll; - - return ST_hyperloglog_merge(hll, from_hll); + return ST_hyperloglog_merge(pthis->hll, from->hll); } struct metric_measure_data *metric_scheme_hll_deserialize(const char *blob, size_t blob_size) @@ -295,18 +288,13 @@ static void metric_scheme_histogram_serialize(const struct metric_measure_data * static int metric_scheme_histogram_merge(struct metric_measure_data *pthis, const struct metric_measure_data *from) { - struct hdr_histogram *hdr = pthis->hdr; - struct hdr_histogram *from_hdr = from->hdr; - - (void)hdr_add(hdr, from_hdr); + (void)hdr_add(pthis->hdr, from->hdr); return 0; } static int metric_scheme_histogram_copy(struct metric_measure_data *pthis, const struct metric_measure_data *from) { - struct hdr_histogram *hdr = pthis->hdr; - struct hdr_histogram *from_hdr = from->hdr; - (void)hdr_add(hdr, from_hdr); + (void)hdr_add(pthis->hdr, from->hdr); return 0; } @@ -733,7 +721,7 @@ void metric_get_plain_blob(const struct metric *pthis, int cell_id, char **blob, pthis->scheme->serialize(metric_find_one_cell(pthis, cell_id), blob, blob_size); } -void metric_rearrange_cell_id(struct metric *pthis, int *original_id, int *final_id, size_t n_id) +void metric_rearrange_cell_id(struct metric *pthis, const int *original_id, const int *final_id, size_t n_id) { if (n_id == 0) { return; @@ -817,12 +805,12 @@ int metric_counter_set(struct metric *pthis, int cell_id, long long value) long long metric_counter_get(const struct metric *pthis, int cell_id) { - struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); + const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); if (data == NULL) { printf("ERR: metric_counter_get cell id %d does not exist\n", cell_id); return -1; } - struct metric_counter_or_gauge *counter = data->counter; + const struct metric_counter_or_gauge *counter = data->counter; return counter->value; } @@ -844,7 +832,7 @@ int metric_hll_add(struct metric *pthis, int cell_id, const char *key, size_t ke double metric_hll_get(const struct metric *pthis, int cell_id) { - struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); + const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); if (data == NULL) { printf("ERR: metric_hll_get cell id %d does not exist\n", cell_id); return -1; @@ -874,7 +862,7 @@ int metric_histogram_record(struct metric *pthis, int cell_id, long long value) long long metric_histogram_value_at_percentile(const struct metric *pthis, int cell_id, double percentile) { - struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); + const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); if (data == NULL) { printf("ERR: metric_histogram_value_at_percentile cell id %d does not exist\n", cell_id); return -1; @@ -897,7 +885,7 @@ static long long hdr_count_le_value(const struct hdr_histogram* h, long long val long long metric_histogram_count_le_value(const struct metric *pthis, int cell_id, long long value) { - struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); + const struct metric_measure_data *data = metric_find_one_cell(pthis, cell_id); if (data == NULL) { printf("ERR: metric_histogram_count_le_value cell id %d does not exist\n", cell_id); return -1; diff --git a/src/metrics/metric.h b/src/metrics/metric.h index 5a9787d..af601e1 100644 --- a/src/metrics/metric.h +++ b/src/metrics/metric.h @@ -17,7 +17,7 @@ struct metric *metric_deserialize(const char *blob, size_t blob_size); //return -1 when merge error. 0 when success. 1 when src has no cell. int metric_merge_or_copy_cell(struct metric *dest, const struct metric *src, int dest_cell_id, int src_cell_id); -void metric_rearrange_cell_id(struct metric *pthis, int *original_id, int *final_id, size_t n_id); +void metric_rearrange_cell_id(struct metric *pthis, const int *original_id, const int *final_id, size_t n_id); void metric_get_plain_blob(const struct metric *pthis, int cell_id, char **blob, size_t *blob_size); void metric_delete_cell(struct metric *pthis, int cell_id); diff --git a/src/metrics/st_hyperloglog.c b/src/metrics/st_hyperloglog.c index 10e6dc1..5aebd57 100644 --- a/src/metrics/st_hyperloglog.c +++ b/src/metrics/st_hyperloglog.c @@ -380,7 +380,7 @@ static double bias_estimate(const struct ST_hyperloglog *h, double raw_est) { } // Get the proper arrays based on precision - double *estimates = *(g_rawEstimateData+(precision-4)); + const double *estimates = *(g_rawEstimateData+(precision-4)); double *biases = *(g_biasData+(precision-4)); // Get the matching biases diff --git a/src/tags/heavy_keeper.c b/src/tags/heavy_keeper.c index 9755009..ac83628 100644 --- a/src/tags/heavy_keeper.c +++ b/src/tags/heavy_keeper.c @@ -59,7 +59,7 @@ void params_set_to_default(struct heavy_keeper_options *p, int K) { } } -void params_set_to(struct heavy_keeper_options *p, struct heavy_keeper_options *src) { +void params_set_to(struct heavy_keeper_options *p, const struct heavy_keeper_options *src) { if (src->array_num != 0) { p->array_num = src->array_num; } @@ -71,7 +71,7 @@ void params_set_to(struct heavy_keeper_options *p, struct heavy_keeper_options * } } -struct heavy_keeper *heavy_keeper_new(int max_query_num, struct heavy_keeper_options *params) { +struct heavy_keeper *heavy_keeper_new(int max_query_num, const struct heavy_keeper_options *params) { struct heavy_keeper *hk = (struct heavy_keeper *)malloc(sizeof(struct heavy_keeper)); hk->K = max_query_num; @@ -230,7 +230,7 @@ int heavy_keeper_add(struct heavy_keeper *hk, struct tag_hash_key *tag, unsigned return 1; } -struct heavy_keeper_result *heavy_keeper_query(struct heavy_keeper *hk) { +struct heavy_keeper_result *heavy_keeper_query(const struct heavy_keeper *hk) { size_t query_entry_num = sorted_set_cardinality(hk->top_K_heap); struct heavy_keeper_result *stats = (struct heavy_keeper_result *)malloc(sizeof(struct heavy_keeper_result)); stats->count = (unsigned *)malloc(query_entry_num * sizeof(unsigned)); @@ -250,7 +250,7 @@ struct heavy_keeper_result *heavy_keeper_query(struct heavy_keeper *hk) { return stats; } -int heavy_keeper_query_one(struct heavy_keeper *hk, const struct tag_hash_key *tag, unsigned int *count_out, int *cell_id_out) +int heavy_keeper_query_one(const struct heavy_keeper *hk, const struct tag_hash_key *tag, unsigned int *count_out, int *cell_id_out) { if (count_out != NULL) { unsigned int count = sorted_set_get_count(hk->top_K_heap, tag); @@ -284,7 +284,7 @@ static void heavy_keeper_merge_sketch(struct heavy_keeper *dest, const struct he for (int array_id = 0; array_id < d; array_id++) { for (int bucket_id = 0; bucket_id < w; bucket_id++) { struct Bucket *bucket_dest = &(dest->sketch[array_id * w + bucket_id]); - struct Bucket *bucket_src = &(src->sketch[array_id * w + bucket_id]); + const struct Bucket *bucket_src = &(src->sketch[array_id * w + bucket_id]); if (bucket_dest->finger_print == bucket_src->finger_print) { bucket_dest->count += bucket_src->count; @@ -322,7 +322,7 @@ void heavy_keeper_merge_sorted_set_recording_id_details(struct heavy_keeper *des sorted_set_hook_function_of_free_user_data(new_rec, free); struct sorted_set *ss_dest = dest->top_K_heap; // original ss in dest - struct sorted_set *ss_src = src->top_K_heap; // the ss to be merged + const struct sorted_set *ss_src = src->top_K_heap; // the ss to be merged int size_dest = sorted_set_cardinality(ss_dest); int size_src = sorted_set_cardinality(ss_src); int max_size = size_dest > size_src ? size_dest : size_src; @@ -344,7 +344,7 @@ void heavy_keeper_merge_sorted_set_recording_id_details(struct heavy_keeper *des for (int i = 0; i < size_dest; i++) { unsigned maxv = find_maxv_in_sketch(dest, tag_arr[i]); struct tag_hash_key *tag_final = tag_hash_key_copy(tag_arr[i]); - int *pst_cell_id_dest = user_datas[i]; + const int *pst_cell_id_dest = user_datas[i]; sorted_set_insert(new_rec, tag_final, maxv); int **pst_cell_id_final = (int **)sorted_set_get_pointer_to_user_data(new_rec, tag_final); @@ -362,7 +362,7 @@ void heavy_keeper_merge_sorted_set_recording_id_details(struct heavy_keeper *des new_alloc_id++; // the new id should be larger than the largest id in dest for (int i = 0; i < size_src; i++) { - struct tag_hash_key *tag_src = tag_arr[i]; + const struct tag_hash_key *tag_src = tag_arr[i]; int **pst_cell_id_dest = (int **)sorted_set_get_pointer_to_user_data(ss_dest, tag_src); int cell_id_src = *user_datas[i]; @@ -399,18 +399,30 @@ void heavy_keeper_merge_sorted_set_recording_id_details(struct heavy_keeper *des free(count_arr); free(tag_arr); free(user_datas); - sorted_set_free(dest->top_K_heap); + sorted_set_free(ss_dest); dest->top_K_heap = new_rec; - *cell_id_popped_from_dest_when_merge_src_out = (int *)malloc(cell_id_popped_from_dest_when_merge_src_len * sizeof(int)); - memcpy(*cell_id_popped_from_dest_when_merge_src_out, cell_id_popped_from_dest_when_merge_src, cell_id_popped_from_dest_when_merge_src_len * sizeof(int)); - *cell_id_popped_from_dest_when_merge_src_len_out = cell_id_popped_from_dest_when_merge_src_len; - *cell_id_in_src_before_merge_out = (int *)malloc(cell_id_in_src_before_merge_len * sizeof(int)); - memcpy(*cell_id_in_src_before_merge_out, cell_id_in_src_before_merge, cell_id_in_src_before_merge_len * sizeof(int)); - *cell_id_in_src_before_merge_len_out = cell_id_in_src_before_merge_len; - *cell_id_in_dest_corresponding_out = (int *)malloc(cell_id_in_src_before_merge_len * sizeof(int)); - memcpy(*cell_id_in_dest_corresponding_out, cell_id_in_dest_corresponding, cell_id_in_src_before_merge_len * sizeof(int)); + if (cell_id_popped_from_dest_when_merge_src_len == 0) { + *cell_id_popped_from_dest_when_merge_src_out = NULL; + *cell_id_popped_from_dest_when_merge_src_len_out = 0; + } else { + *cell_id_popped_from_dest_when_merge_src_out = (int *)malloc(cell_id_popped_from_dest_when_merge_src_len * sizeof(int)); + memcpy(*cell_id_popped_from_dest_when_merge_src_out, cell_id_popped_from_dest_when_merge_src, cell_id_popped_from_dest_when_merge_src_len * sizeof(int)); + *cell_id_popped_from_dest_when_merge_src_len_out = cell_id_popped_from_dest_when_merge_src_len; + } + + if (cell_id_in_src_before_merge_len == 0) { + *cell_id_in_src_before_merge_out = NULL; + *cell_id_in_src_before_merge_len_out = 0; + *cell_id_in_dest_corresponding_out = NULL; + } else { + *cell_id_in_src_before_merge_out = (int *)malloc(cell_id_in_src_before_merge_len * sizeof(int)); + memcpy(*cell_id_in_src_before_merge_out, cell_id_in_src_before_merge, cell_id_in_src_before_merge_len * sizeof(int)); + *cell_id_in_src_before_merge_len_out = cell_id_in_src_before_merge_len; + *cell_id_in_dest_corresponding_out = (int *)malloc(cell_id_in_src_before_merge_len * sizeof(int)); + memcpy(*cell_id_in_dest_corresponding_out, cell_id_in_dest_corresponding, cell_id_in_src_before_merge_len * sizeof(int)); + } } @@ -465,7 +477,7 @@ struct heavy_keeper *heavy_keeper_copy(const struct heavy_keeper *src) } } ***********************************************************************************/ -int heavy_keeper_sorted_set_member_serialization(struct tag_hash_key *tag, unsigned int score, unsigned int cell_id, char **blob, size_t *blob_sz) { +int heavy_keeper_sorted_set_member_serialization(const struct tag_hash_key *tag, unsigned int score, unsigned int cell_id, char **blob, size_t *blob_sz) { mpack_writer_t writer; mpack_writer_init_growable(&writer, blob, blob_sz); mpack_build_map(&writer); @@ -560,7 +572,7 @@ void heavy_keeper_serialization(const struct heavy_keeper *hk, char **blob, size } } -void heavy_keeper_sorted_set_deserialization(mpack_node_t *root, struct sorted_set *out) +void heavy_keeper_sorted_set_deserialization(const mpack_node_t *root, struct sorted_set *out) { mpack_node_t item; mpack_node_t array_sorted_set = mpack_node_map_cstr(*root, "ss"); @@ -607,7 +619,7 @@ struct heavy_keeper *heavy_keeper_deserialization(const char *blob, size_t size) return hk; } -int heavy_keeper_get_config_K(struct heavy_keeper *hk) +int heavy_keeper_get_config_K(const struct heavy_keeper *hk) { return hk->K; } diff --git a/src/tags/heavy_keeper.h b/src/tags/heavy_keeper.h index 25a6663..5cd9644 100644 --- a/src/tags/heavy_keeper.h +++ b/src/tags/heavy_keeper.h @@ -59,9 +59,9 @@ int heavy_keeper_add(struct heavy_keeper *hk, struct tag_hash_key *tag, unsigned * @param hk the pointer to the heavy keeper. * @return a pointer to the heavy keeper query result. User should free it after use by calling heavy_keeper_result_free. */ -struct heavy_keeper_result *heavy_keeper_query(struct heavy_keeper *hk); +struct heavy_keeper_result *heavy_keeper_query(const struct heavy_keeper *hk); -int heavy_keeper_query_one(struct heavy_keeper *hk, const struct tag_hash_key *tag, unsigned int *count_out, int *cube_id_out); +int heavy_keeper_query_one(const struct heavy_keeper *hk, const struct tag_hash_key *tag, unsigned int *count_out, int *cube_id_out); /** * @brief free a heavy keeper query result. @@ -98,7 +98,7 @@ struct heavy_keeper *heavy_keeper_deserialization(const char *blob, size_t size) /** * @brief Get the "K" parameter of the heavy keeper. */ -int heavy_keeper_get_config_K(struct heavy_keeper *hk); +int heavy_keeper_get_config_K(const struct heavy_keeper *hk); void heavy_keeper_get_config_metric_header(struct heavy_keeper *hk, char **metric_names, size_t *n_metric, size_t *primary_metric_index); diff --git a/src/tags/my_ut_hash.c b/src/tags/my_ut_hash.c index 8793730..7e800c4 100644 --- a/src/tags/my_ut_hash.c +++ b/src/tags/my_ut_hash.c @@ -12,9 +12,6 @@ // #define USING_BASE_HASH HASH_JEN #define USING_BASE_HASH HASH_SFH -int simple_hash_cnt = 0; -int hash_cnt = 0; - void build_dynamic_cell_key(const struct fieldstat_tag tags[], size_t n_tags, char **out_key, size_t *out_key_size) { int i = 0; @@ -87,10 +84,8 @@ struct tag_hash_key { unsigned int hashv; }; -// todo: 这个函数弃用 -unsigned int tag_hash_key_cal_hash_value(struct tag_hash_key *my_hash_key) +unsigned int tag_hash_key_cal_hash_value(const struct tag_hash_key *my_hash_key) { - simple_hash_cnt++; return my_hash_key->hashv; } @@ -144,8 +139,8 @@ void fieldtag_list_free(struct fieldstat_tag *tags, size_t n_tags) int my_cmp_tag(const void *a, const void *b) { - struct fieldstat_tag *aa = (struct fieldstat_tag *)a; - struct fieldstat_tag *bb = (struct fieldstat_tag *)b; + const struct fieldstat_tag *aa = (struct fieldstat_tag *)a; + const struct fieldstat_tag *bb = (struct fieldstat_tag *)b; int key_cmp = strcmp(aa->key, bb->key); if (key_cmp != 0) { return key_cmp; @@ -277,7 +272,6 @@ struct tag_hash_key *tag_hash_key_construct_with_fieldstat_tag(const struct fiel ret_hash_key->n_my_tag = n_src; USING_BASE_HASH(compound_key, compound_key_len, ret_hash_key->hashv); - hash_cnt++; return ret_hash_key; } @@ -400,24 +394,6 @@ struct tag_hash_key *tag_hash_key_deserialize(const char *blob, size_t blob_size return ret_hash_key; } -// todo: delete them - -int tmp_get_hash_count() -{ - return hash_cnt; -} - -int tmp_get_simple_hash_count() -{ - return simple_hash_cnt; -} - -void reset_hash_count() -{ - hash_cnt = 0; - simple_hash_cnt = 0; -} - const char *tag_hash_key_get_compound_key(const struct tag_hash_key *tag) { return tag->compound_key; diff --git a/src/tags/my_ut_hash_inner.h b/src/tags/my_ut_hash_inner.h index 90c6a31..f3de3a2 100644 --- a/src/tags/my_ut_hash_inner.h +++ b/src/tags/my_ut_hash_inner.h @@ -11,7 +11,7 @@ extern "C" struct tag_hash_key; -unsigned int tag_hash_key_cal_hash_value(struct tag_hash_key *my_hash_key); +unsigned int tag_hash_key_cal_hash_value(const struct tag_hash_key *my_hash_key); // return 1 if not equal, 0 if equal int tag_hash_key_cmp(const struct tag_hash_key *a, const struct tag_hash_key *b); diff --git a/src/tags/sorted_set.c b/src/tags/sorted_set.c index 93082ab..c45f6e1 100644 --- a/src/tags/sorted_set.c +++ b/src/tags/sorted_set.c @@ -40,7 +40,7 @@ struct entry_data *entry_data_construct(struct tag_hash_key *tag) return entry_data;
}
-void entry_data_destroy(struct entry_data *entry_data, void (*free_method)(void *))
+void entry_data_destroy(struct entry_data *entry_data, void const(* free_method)(void *))
{
if (entry_data == NULL) {
return;
@@ -52,17 +52,17 @@ void entry_data_destroy(struct entry_data *entry_data, void (*free_method)(void free(entry_data);
}
-unsigned sorted_set_entry_get_count(heap_entry *entry)
+unsigned sorted_set_entry_get_count(const heap_entry *entry)
{
return *(unsigned *)entry->key;
}
-struct entry_data *sorted_set_entry_get_data(heap_entry *entry)
+struct entry_data *sorted_set_entry_get_data(const heap_entry *entry)
{
return entry->value;
}
-static bool cmp_int(void *aa, void *bb)
+static bool cmp_int(void *aa, void *bb) // cppcheck-suppress constParameterCallback
{
if (sorted_set_entry_get_count(aa) < sorted_set_entry_get_count(bb)) {
return true;
@@ -241,9 +241,9 @@ unsigned sorted_set_get_min_count(const struct sorted_set *ss) if (heap->cur_size == 0) {
return 0;
}
- heap_entry *ret = (heap_entry *)(heap->nodes[0]);
+ const heap_entry *ret = (heap_entry *)(heap->nodes[0]);
- unsigned* key = ret->key;
+ const unsigned* key = ret->key;
return *key;
}
@@ -252,7 +252,7 @@ unsigned sorted_set_get_count(const struct sorted_set *ss, const struct tag_hash if (sorted_set_cardinality(ss) == 0) {
return 0;
}
- heap_entry *entry = sorted_set_find_entry(ss, tag);
+ const heap_entry *entry = sorted_set_find_entry(ss, tag);
if (entry == NULL) {
return 0;
}
@@ -288,9 +288,9 @@ void sorted_set_hook_function_of_free_user_data(struct sorted_set *ss, void (*fr ss->free_user_data_func = free_method;
}
-void **sorted_set_get_pointer_to_user_data(struct sorted_set *ss, const struct tag_hash_key *tag)
+void **sorted_set_get_pointer_to_user_data(const struct sorted_set *ss, const struct tag_hash_key *tag)
{
- heap_entry *entry = sorted_set_find_entry(ss, tag);
+ const heap_entry *entry = sorted_set_find_entry(ss, tag);
if (entry == NULL) {
return NULL;
}
@@ -301,7 +301,7 @@ void **sorted_set_get_pointer_to_user_data(struct sorted_set *ss, const struct t int sorted_set_insert_user_data(struct sorted_set *ss, const struct tag_hash_key *tag, void *user_data)
{
- heap_entry *entry = sorted_set_find_entry(ss, tag);
+ const heap_entry *entry = sorted_set_find_entry(ss, tag);
if (entry == NULL) {
return -1;
}
@@ -317,8 +317,8 @@ typedef struct { int cmp_tmp_heap_node(const void *a, const void *b)
{
- tmp_heap_node *aa = (tmp_heap_node *)a;
- tmp_heap_node *bb = (tmp_heap_node *)b;
+ const tmp_heap_node *aa = (tmp_heap_node *)a;
+ const tmp_heap_node *bb = (tmp_heap_node *)b;
if (aa->key < bb->key) {
return 1;
@@ -334,7 +334,7 @@ void sorted_set_dump(const struct sorted_set *ss, unsigned *scores_out, struct t struct minheap *h = ss->heap;
tmp_heap_node *tmp_nodes = (tmp_heap_node *)malloc(sizeof(tmp_heap_node) * h->cur_size);
for (int i = 0; i < h->cur_size; i++) {
- heap_entry *entry = (heap_entry *)h->nodes[i];
+ const heap_entry *entry = (heap_entry *)h->nodes[i];
tmp_nodes[i].key = sorted_set_entry_get_count(entry);
tmp_nodes[i].val = sorted_set_entry_get_data(entry);
}
@@ -353,8 +353,8 @@ struct sorted_set *sorted_set_copy(const struct sorted_set *ss) sorted_set_hook_function_of_free_user_data(ret, ss->free_user_data_func);
for (int i = 0; i < ss->heap->cur_size; i++) {
- heap_entry *entry = (heap_entry *)ss->heap->nodes[i];
- struct entry_data *data = sorted_set_entry_get_data(entry);
+ const heap_entry *entry = (heap_entry *)ss->heap->nodes[i];
+ const struct entry_data *data = sorted_set_entry_get_data(entry);
struct tag_hash_key *new_key = tag_hash_key_copy(data->key);
sorted_set_insert(ret, new_key, sorted_set_entry_get_count(entry));
int *new_user_data = (int *)malloc(sizeof(int));
diff --git a/src/tags/sorted_set.h b/src/tags/sorted_set.h index d82ca39..78cf5b4 100644 --- a/src/tags/sorted_set.h +++ b/src/tags/sorted_set.h @@ -58,7 +58,7 @@ int sorted_set_insert_user_data(struct sorted_set *ss, const struct tag_hash_key * @brief get the pointer to the user data of the given member. So that the user can modify the user data.
* @return the pointer to the user data. return NULL if the member is not in the sorted set.
*/
-void **sorted_set_get_pointer_to_user_data(struct sorted_set *ss, const struct tag_hash_key *tag);
+void **sorted_set_get_pointer_to_user_data(const struct sorted_set *ss, const struct tag_hash_key *tag);
/**
* @brief Increase the score of the given member.
* @param score By the value of which increase member.
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); |
