summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-09-27 17:45:22 +0800
committerchenzizhan <[email protected]>2023-09-27 17:45:22 +0800
commit4119061929db2eda0ad4c6233b97e8e46819a1ad (patch)
treecf04822d3cc17a5807668c8b5de0ccf0f5f89374
parent40c57ecb4b8489d82f667d9909ce095aa131a103 (diff)
more tests
-rw-r--r--src/fieldstat.c8
-rw-r--r--src/tags/heavy_keeper.c14
-rw-r--r--test/test_merge.cpp63
-rw-r--r--test/test_metric_counter.cpp94
-rw-r--r--test/test_metric_histogram.cpp18
5 files changed, 182 insertions, 15 deletions
diff --git a/src/fieldstat.c b/src/fieldstat.c
index cf5b8a2..2520458 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -618,7 +618,6 @@ int fieldstat_counter_set(struct fieldstat *instance, int cube_id, int metric_id
int cell_id = -1;
// get the occurrence
- // todo: 干脆查个值然后直接调用incrby
long long occurrence = 0;
if (cube->sampling_mode == SAMPLING_MODE_TOPK && cube->primary_metric_id == metric_id) {
struct tag_hash_key *tag_key = tag_hash_key_construct_with_fieldstat_tag(tags, n_tag);
@@ -631,6 +630,8 @@ int fieldstat_counter_set(struct fieldstat *instance, int cube_id, int metric_id
} else {
occurrence = value;
}
+ } else {
+ occurrence = value;
}
tag_hash_key_free(tag_key);
if (occurrence < 0) {
@@ -1078,6 +1079,8 @@ void fieldstat_get_metrics(const struct fieldstat *instance, int **metric_id_out
void fieldstat_get_cells_used_by_metric(const struct fieldstat *instance, int cube_id, int metric_id,
struct fieldstat_tag_list **tag_list, size_t *n_cell)
{
+ *tag_list = NULL;
+ *n_cell = 0;
int ret = FS_OK;
const struct metric *metric = fieldstat_find_metric(instance, cube_id, metric_id, &ret);
if (metric == NULL) {
@@ -1087,11 +1090,10 @@ void fieldstat_get_cells_used_by_metric(const struct fieldstat *instance, int cu
size_t n_cell_ret = 0;
int *cell_ids = NULL;
metric_get_cell_ids(metric, &cell_ids, &n_cell_ret);
- *n_cell = n_cell_ret;
if (n_cell_ret == 0) {
- *tag_list = NULL;
return;
}
+ *n_cell = n_cell_ret;
struct fieldstat_tag_list *tag_list_ret = (struct fieldstat_tag_list *)malloc(sizeof(struct fieldstat_tag_list) * n_cell_ret);
*tag_list = tag_list_ret;
diff --git a/src/tags/heavy_keeper.c b/src/tags/heavy_keeper.c
index d53e576..2790cf1 100644
--- a/src/tags/heavy_keeper.c
+++ b/src/tags/heavy_keeper.c
@@ -242,20 +242,20 @@ static int heavy_keeper_add_by_recording_popped_data(struct heavy_keeper *heavy_
int heavy_keeper_add(struct heavy_keeper *hk, const struct tag_hash_key *tag, unsigned int count, int cell_id, int *popped_id_out)
{
*popped_id_out = -1;
- int ret = heavy_keeper_add_by_recording_popped_data(hk, tag, count, cell_id, popped_id_out);
+ heavy_keeper_add_by_recording_popped_data(hk, tag, count, cell_id, popped_id_out);
- int cell_id_tmp = sorted_set_get_cell_id(hk->top_K_heap, tag); // todo: 之后删掉
+ int cell_id_tmp = sorted_set_get_cell_id(hk->top_K_heap, tag);
if (cell_id_tmp >= 0) {
- if (cell_id_tmp == cell_id) {
- assert(ret == 0 || ret == 2);
+ if (cell_id_tmp == cell_id) { // insert success
+ return 0;
} else {
- assert(ret == 1);
+ return 1; // insert success, but use the different cell_id
}
} else {
- assert(ret == -1);
+ return -1;
}
- return ret;
+ return -1; // should not reach here
}
struct tag_hash_key *heavy_keeper_get_true_pst_tag(const struct heavy_keeper *hk, const struct tag_hash_key *tag)
diff --git a/test/test_merge.cpp b/test/test_merge.cpp
index 70eb497..2ac7e32 100644
--- a/test/test_merge.cpp
+++ b/test/test_merge.cpp
@@ -480,8 +480,67 @@ TEST(unit_test_merge, merge_accuracy_test_gen_dest_full_some_inserted_and_some_m
}
}
-// todo: primary key 不同的情况
-// todo: primary key 上一个操作都没有
+TEST(unit_test_merge, primary_metric_has_no_value)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2);
+ int metric_primary = fieldstat_register_counter(instance, "primary");
+ int metric_operated = fieldstat_register_counter(instance, "operated");
+ fieldstat_counter_incrby(instance, cube_id, metric_operated, &TEST_TAG_STRING, 1, 1);
+ struct fieldstat *instance_dest = fieldstat_new();
+ fieldstat_merge(instance_dest, instance);
+ fieldstat_merge(instance_dest, instance);
+
+ struct fieldstat_tag_list *tag_list = NULL;
+ size_t n_cell = 0;
+ fieldstat_get_cells_used_by_metric(instance_dest, 0, metric_primary, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 0);
+
+ fieldstat_get_cells_used_by_metric(instance_dest, 0, metric_operated, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 1);
+ EXPECT_EQ(merge_test_fieldstat_counter_get(instance_dest, 0, metric_operated, &tag_list[0]), 2);
+ fieldstat_tag_list_arr_free(tag_list, n_cell);
+
+ fieldstat_free(instance);
+ fieldstat_free(instance_dest);
+}
+
+TEST(unit_test_merge, primary_metric_id_different)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2);
+ int metric_primary = fieldstat_register_counter(instance, "primary");
+ int metric_2 = fieldstat_register_counter(instance, "2");
+ fieldstat_counter_incrby(instance, cube_id, metric_primary, &TEST_TAG_STRING, 1, 100);
+ fieldstat_counter_incrby(instance, cube_id, metric_2, &TEST_TAG_STRING, 1, 1);
+
+ struct fieldstat *instance_dst = fieldstat_new();
+ int cube_id_dst = fieldstat_create_cube(instance_dst, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 2);
+ fieldstat_register_counter(instance_dst, "2");
+ int metric_primary_dst = fieldstat_register_counter(instance_dst, "primary");
+ fieldstat_cube_set_primary_metric(instance_dst, cube_id_dst, metric_primary_dst);
+
+ fieldstat_merge(instance_dst, instance);
+
+ struct fieldstat_tag_list *tag_list = NULL;
+ size_t n_cell = 0;
+ fieldstat_get_cells_used_by_metric(instance_dst, 0, metric_primary, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 1);
+ int *metric_ids;
+ size_t n_metrics;
+ fieldstat_get_metrics(instance_dst, &metric_ids, &n_metrics);
+ EXPECT_EQ(n_metrics, 2);
+ EXPECT_STREQ(fieldstat_get_metric_name(instance_dst, metric_ids[0]), "2");
+ EXPECT_STREQ(fieldstat_get_metric_name(instance_dst, metric_ids[1]), "primary");
+
+ EXPECT_EQ(merge_test_fieldstat_counter_get(instance_dst, 0, metric_ids[1], &tag_list[0]), 100);
+ EXPECT_EQ(merge_test_fieldstat_counter_get(instance_dst, 0, metric_ids[0], &tag_list[0]), 1);
+ fieldstat_tag_list_arr_free(tag_list, n_cell);
+ free(metric_ids);
+ fieldstat_free(instance);
+ fieldstat_free(instance_dst);
+}
+
int main(int argc, char *argv[])
{
diff --git a/test/test_metric_counter.cpp b/test/test_metric_counter.cpp
index 0382f00..97b83ae 100644
--- a/test/test_metric_counter.cpp
+++ b/test/test_metric_counter.cpp
@@ -143,8 +143,6 @@ TEST(metric_test_counter, topk_add_and_test_accuracy)
EXPECT_STREQ(key.c_str(), "elephant ");
- printf("topk_add_and_test_accuracy %s %s, count: %lld\n", key.c_str(), value.c_str(), my_fieldstat_counter_get(instance, 0, 0, &tag_list[i]));
-
error += abs(my_fieldstat_counter_get(instance, 0, 0, &tag_list[i]) - flow_cnt[key + value]);
}
printf("topk_add_and_test_accuracy Mean ratio e: %lld\n", error);
@@ -212,7 +210,97 @@ TEST(metric_test_counter, add_with_wrong_metric_id_expecting_fail)
fieldstat_free(instance);
}
-//todo: set + topk
+TEST(metric_test_counter, add_and_query_on_dummy_cell_of_topk)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10);
+ fieldstat_register_counter(instance, "primary"); // also the dummy one
+ int metric_id = fieldstat_register_counter(instance, "using");
+
+ fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1);
+
+ // add success
+ long long measure = my_fieldstat_counter_get(instance, cube_id, metric_id);
+ EXPECT_EQ(measure, 1);
+ // cannot query dummy
+ int *metric_ids = NULL;
+ size_t n_metric = 0;
+ fieldstat_get_metrics_used_by_cube(instance, cube_id, &metric_ids, &n_metric);
+ EXPECT_EQ(n_metric, 1);
+ EXPECT_EQ(metric_ids[0], 1);
+ free(metric_ids);
+
+ struct fieldstat_tag_list *tag_list = NULL;
+ size_t n_cell;
+ fieldstat_get_cells_used_by_metric(instance, cube_id, 0, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 0);
+
+ fieldstat_free(instance);
+}
+
+TEST(metric_test_counter, set_on_primary_metric_going_smaller)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10);
+ int metric_id = fieldstat_register_counter(instance, "primary");
+
+ int ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 10);
+ EXPECT_EQ(ret, FS_OK);
+ ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 2);
+ EXPECT_EQ(ret, FS_ERR_INVALID_PARAM);
+
+ fieldstat_free(instance);
+}
+
+TEST(metric_test_counter, set_on_primary_metric_going_bigger)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10);
+ int metric_id = fieldstat_register_counter(instance, "primary");
+
+ int ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 10);
+ EXPECT_EQ(ret, FS_OK);
+ ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 20);
+ EXPECT_EQ(ret, FS_OK);
+
+ EXPECT_EQ(my_fieldstat_counter_get(instance, cube_id, metric_id), 20);
+
+ fieldstat_free(instance);
+}
+
+TEST(metric_test_counter, topk_set_and_test_accuracy)
+{
+ struct fieldstat *instance = fieldstat_new();
+ fieldstat_create_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_TOPK, 6);
+ fieldstat_register_counter(instance, "test");
+
+ struct fieldstat_tag tag = TEST_TAG_INT;
+ // tag : [0, 1, 2, 3, 4 ,5]
+ // value: [0, 1, 2, 3, 4 ,5]
+ for (int i = 0; i < 6; i++) {
+ tag.value_longlong = i;
+ EXPECT_EQ(fieldstat_counter_set(instance, 0, 0, &tag, 1, i), FS_OK);
+ }
+ // tag : [0, 1, 2, 3, 4 ,5, 6, 7, 8]
+ // value: [0, 1, 2, 100, 100, 100, 100, 100, 100]
+ for (int i = 0; i < 6; i++) {
+ tag.value_longlong = i + 3;
+ EXPECT_EQ(fieldstat_counter_set(instance, 0, 0, &tag, 1, 100), FS_OK);
+ }
+
+ struct fieldstat_tag_list *tag_list = NULL;
+ size_t n_cell = 0;
+ fieldstat_get_cells_used_by_metric(instance, 0, 0, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 6);
+ for (size_t i = 0; i < n_cell; i++) {
+ EXPECT_EQ(tag_list[i].tag[0].value_longlong, i + 3);
+ EXPECT_EQ(my_fieldstat_counter_get(instance, 0, 0, &tag_list[i]), 100);
+ }
+
+ fieldstat_tag_list_arr_free(tag_list, n_cell);
+ fieldstat_free(instance);
+}
+
int main(int argc, char *argv[])
{
diff --git a/test/test_metric_histogram.cpp b/test/test_metric_histogram.cpp
index 81e1e50..6983d02 100644
--- a/test/test_metric_histogram.cpp
+++ b/test/test_metric_histogram.cpp
@@ -159,6 +159,24 @@ TEST(metric_test_histogram, encode_decode_b64)
hdr_close(h2);
}
+TEST(metric_test_histogram, can_add_0value) // histogram only allow min_val > 0, but it can accept value == 0
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
+ int metric_id = fieldstat_register_hist(instance, "czz_test", 1, 600000, 3);
+
+ int ret = fieldstat_hist_record(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 0);
+ EXPECT_EQ(ret, 0);
+ ret = fieldstat_hist_record(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 0);
+ EXPECT_EQ(ret, 0);
+ ret = fieldstat_hist_record(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 500);
+
+ EXPECT_EQ(fieldstat_hist_value_at_percentile(instance, cube_id, metric_id, &TEST_TAG_LIST_INT, 60.0), 0);
+
+ fieldstat_free(instance);
+}
+
+
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);