summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2024-04-07 11:45:15 +0800
committerchenzizhan <[email protected]>2024-04-07 11:45:15 +0800
commit934cd428a063367c76a1fbff73ce1a2391f4be96 (patch)
tree64f6e71dfba388e901800ecc6fd6ebf78e6007b7
parent3269ef27d012b244093fa623697a9db7ff59157d (diff)
modify:check max_thread_num before new fsev4.5.5
-rw-r--r--src/fieldstat_easy.c3
-rw-r--r--test/test_register_and_reset.cpp54
2 files changed, 30 insertions, 27 deletions
diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c
index 879a06f..1b56688 100644
--- a/src/fieldstat_easy.c
+++ b/src/fieldstat_easy.c
@@ -115,6 +115,9 @@ void *fs_easy_output_thread(void *arg) // return void * for pthread_create check
}
struct fieldstat_easy *fieldstat_easy_new(int max_thread_num, const char *name, const struct fieldstat_tag *tags, size_t n_tag) {
+ if (max_thread_num <= 0) {
+ return NULL;
+ }
struct fieldstat_easy *fse = calloc(1, sizeof(struct fieldstat_easy));
fse->fsu = malloc(sizeof(struct fs_easy_thread) * max_thread_num);
fse->max_thread_num = max_thread_num;
diff --git a/test/test_register_and_reset.cpp b/test/test_register_and_reset.cpp
index a01e031..57c947e 100644
--- a/test/test_register_and_reset.cpp
+++ b/test/test_register_and_reset.cpp
@@ -115,30 +115,30 @@ TEST(test_register, reset_and_new_cell)
fieldstat_free(instance);
}
-// TEST(test_register, register_many_cubes)
-// {
-// struct fieldstat *instance = fieldstat_new();
-// int registered_cube = 10000; // will trigger realloc many times
-// struct fieldstat_tag shared_tag = TEST_SHARED_TAG;
-// for (int i = 0; i < registered_cube; i++) {
-// shared_tag.value_longlong = i;
-// int cube_id = fieldstat_create_cube(instance, &shared_tag, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
-// EXPECT_EQ(cube_id, i);
-// }
-// // try to use the cube
-// int metric_id = fieldstat_register_counter(instance, "counter");
-// for (int i = 0; i < registered_cube; i++) {
-// fieldstat_counter_incrby(instance, i, metric_id, &TEST_TAG_INT, 1, i);
-// }
-
-// for (int i = 0; i < registered_cube; i++) {
-// long long result;
-// fieldstat_counter_get(instance, i, 0, &TEST_TAG_LIST_INT, &result);
-// EXPECT_EQ(result, i);
-// }
-
-// fieldstat_free(instance);
-// }
+TEST(test_register, register_many_cubes)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int registered_cube = 10000; // will trigger realloc many times
+ struct fieldstat_tag shared_tag = TEST_SHARED_TAG;
+ for (int i = 0; i < registered_cube; i++) {
+ shared_tag.value_longlong = i;
+ int cube_id = fieldstat_create_cube(instance, &shared_tag, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
+ EXPECT_EQ(cube_id, i);
+ }
+ // try to use the cube
+ int metric_id = fieldstat_register_counter(instance, "counter");
+ for (int i = 0; i < registered_cube; i++) {
+ fieldstat_counter_incrby(instance, i, metric_id, &TEST_TAG_INT, 1, i);
+ }
+
+ for (int i = 0; i < registered_cube; i++) {
+ long long result;
+ fieldstat_counter_get(instance, i, 0, &TEST_TAG_LIST_INT, &result);
+ EXPECT_EQ(result, i);
+ }
+
+ fieldstat_free(instance);
+}
TEST(test_register, add_many_tagged_cells)
{
@@ -182,17 +182,17 @@ TEST(test_register, add_long_tagged_cells)
free(long_string);
}
-TEST(test_register, register_too_many_metrics)
+TEST(test_register, register_many_metrics)
{
struct fieldstat *instance = fieldstat_new();
int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
int metric_id = 0;
- for (int i = 0; i < 50; i++) {
+ for (int i = 0; i < 200; i++) {
metric_id = fieldstat_register_counter(instance, (std::string("counter ") + std::to_string(i)).c_str());
EXPECT_EQ(metric_id, i);
}
- for (int i = 0; i < 50; i++) {
+ for (int i = 0; i < 200; i++) {
EXPECT_EQ(fieldstat_counter_incrby(instance, cube_id, i, &TEST_TAG_INT, 1, 1), 0);
}