summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-05-29 17:25:35 +0800
committerchenzizhan <[email protected]>2023-05-29 17:25:35 +0800
commit6e830288db241d1d316498df3981c2cda7fb6dc7 (patch)
treec937e742fbbdf1b37db2c14fbb6979b6bc196c5f
parent9e9617aa26e5487fa785e9e570e60a9e0ca31d3e (diff)
test for truncate
-rw-r--r--src/file_output.cpp4
-rw-r--r--src/line_protocol_output.cpp16
-rw-r--r--test/src/gtest_fieldstat_topk.cpp41
-rw-r--r--test/src/test_utils.hpp5
4 files changed, 51 insertions, 15 deletions
diff --git a/src/file_output.cpp b/src/file_output.cpp
index 6e98a66..5165155 100644
--- a/src/file_output.cpp
+++ b/src/file_output.cpp
@@ -784,7 +784,6 @@ void output_file_type_topk_default(struct fieldstat_instance *instance, int curr
}
if (metric_num == 0) {
- printf("return because metric_num == 0\n");
return;
}
@@ -808,8 +807,6 @@ void output_file_type_topk_default(struct fieldstat_instance *instance, int curr
used_len += snprintf(print_buf + used_len, size - used_len, "\n%s\n", draw_line);
}
- printf("buffer: %s\n", print_buf);
-
fwrite(print_buf, used_len, 1, instance->local_output_fp);
fflush(instance->local_output_fp);
}
@@ -889,7 +886,6 @@ int file_output(struct fieldstat_instance *instance,long long interval_ms)
output_file_type_topk_default(instance, current_metric_cnt); // directly output to file. topk is long, so use another buffer to hold it.
}
- printf("file_output done\n");
return 0;
}
diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp
index 661053d..2e49ed1 100644
--- a/src/line_protocol_output.cpp
+++ b/src/line_protocol_output.cpp
@@ -244,15 +244,12 @@ static int build_table_row_line_buf(char *instance_name, int output_type, struct
int add_one_topk_field(char *key, size_t key_len, unsigned value, char *line_buf, unsigned int line_buf_size)
{
if (key_len >= line_buf_size - 3) { // "=", "\n", and at least one char for value
- printf("add_one_topk_field, 1\n");
return -1;
}
int used_len = snprintf(line_buf, line_buf_size, "%s=%u", key, value);
- printf("add_one_topk_field, buffer: %s\n", line_buf);
if (used_len < 0 || used_len >= (int)line_buf_size - 1) { // -1: line buf should end with '\n' // TODO: what if timestamp or anything else are inserted
- printf("add_one_topk_field, 2\n");
-
line_buf[0] = '\0';
+ return -1;
}
return used_len;
}
@@ -260,21 +257,22 @@ int add_one_topk_field(char *key, size_t key_len, unsigned value, char *line_buf
int add_field_set_for_topk(struct heavy_keeper *hk, char *line_buf, unsigned int line_buf_size)
{
struct heavy_keeper_result *result = heavy_keeper_query(hk);
- printf("n_key: %zu\n", result->n_key);
int used_len = 0;
for (size_t i = 0; i < result->n_key; i++) {
int tmp_ret = add_one_topk_field(result->key[i], result->key_len[i], result->count[i], line_buf + used_len, line_buf_size - used_len);
if (tmp_ret < 0) {
+ printf("add_field_set_for_topk, Buffer not long enough(1).\n");
break;
}
used_len += tmp_ret;
if (i < result->n_key - 1) { // not the last one
- int tmp_ret = snprintf(line_buf + used_len, line_buf_size - used_len, ",");
- if (tmp_ret < 0 || used_len >= (int)line_buf_size - 1) {
- line_buf[0] = '\0';
+ int tmp_ret = snprintf(line_buf + used_len, line_buf_size - used_len - 1, ",");
+ if (tmp_ret < 0) {
+ printf("add_field_set_for_topk, Buffer not long enough(2).\n");
+ line_buf[used_len] = '\0';
break;
}
used_len++;
@@ -320,8 +318,6 @@ void output_line_protocol_topk(struct fieldstat_instance *instance, struct metri
}
used_len += snprintf(line_buf + used_len, line_buf_size - used_len, "\n"); // TODO: 这个回车是干嘛的?
- printf("the output buffer : \n%s\n---------------------\n", line_buf);
-
send_line_buf(&instance->line_protocol_output, line_buf, used_len);
}
diff --git a/test/src/gtest_fieldstat_topk.cpp b/test/src/gtest_fieldstat_topk.cpp
index ecfb428..332a5ea 100644
--- a/test/src/gtest_fieldstat_topk.cpp
+++ b/test/src/gtest_fieldstat_topk.cpp
@@ -150,8 +150,47 @@ TEST(TopkOutput, output_topk_line_protocol_accumulated)
delete tester;
}
+TEST(TopkOutput, output_topk_line_protocol_test_truncate_when_packet_too_long)
+{
+ Heavy_keeper_tester *tester = new Heavy_keeper_tester("heavy_keeper_test");
+ tester->register_metric("field1", 50, 1);
+ std::string my_fields_name = "a_lo-ng_s-trin-g_wi-th_4-0_ch-arac-ters";
+
+ for (int i = 0; i < 50; i++) {
+ tester->take_action("field1", (my_fields_name + std::to_string(i)).c_str(), 100 + i);
+ }
+ clear_file(TEST_LINE_PROTOCOL_FILE_PATH);
+
+ // std::string expected = tester->get_expected_pattern("json");
+
+ std::string content = tester->read_from_line_protocol(TEST_LINE_PROTOCOL_FILE_PATH);
+ // std::cout << content << std::endl;
+
+ cJSON *cjson_metric = cJSON_Parse(content.c_str());
+ EXPECT_NE(nullptr, cjson_metric); // a valid json
+
+ cJSON *cjson_fields = cJSON_GetObjectItem(cjson_metric, "fields");
+ EXPECT_NE(nullptr, cjson_fields);
+
+ char *cjson_metric_str = cJSON_PrintUnformatted(cjson_fields);
+ // the entries ranking at top will be output, while the others are truncated because of the length limit
+ for (int i = 0; i < 50; i++) {
+ Reg_str one_field_p;
+ one_field_p.add_text().add(my_fields_name).add(i).add_text().add(i + 100).add_any();
+ if (i < 20) {
+ EXPECT_FALSE(std::regex_search(cjson_metric_str, std::regex(one_field_p.get_str())));
+ } else if (i > 20) {
+ EXPECT_TRUE(std::regex_search(cjson_metric_str, std::regex(one_field_p.get_str())));
+ }
+ }
+
+ cJSON_Delete(cjson_metric);
+
+ 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.hpp b/test/src/test_utils.hpp
index e0c5665..e040e8e 100644
--- a/test/src/test_utils.hpp
+++ b/test/src/test_utils.hpp
@@ -3,6 +3,7 @@
#include <unordered_map>
#include <vector>
#include <type_traits>
+#include <algorithm>
#include "fieldstat.h"
#include "fieldstat_internal.h"
@@ -45,6 +46,10 @@ class Reg_str
str_ += t;
return *this;
}
+ Reg_str &add(int t) {
+ str_ += std::to_string(t);
+ return *this;
+ }
template <typename T>
Reg_str &add_num(T t) {
str_ += std::to_string(t);