summaryrefslogtreecommitdiff
path: root/test/test_easy_fs.cpp
blob: d6f91774ba9fb4a1c60c63aa51f94254c5f5b7f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <gtest/gtest.h>
#include <stub/stub.h>
#include <fstream>
#include <thread>

#include "utils.hpp"
#include "cjson/cJSON.h"

#include "fieldstat_easy.h"

#define FILENAME "./test_easy_fieldstat.json"

TEST(test_easy_fieldstat, new_and_free)
{
    struct fieldstat_easy *fse = fieldstat_easy_new(10, "instance name1231231231", &TEST_TAG_STRING, 1);
    fieldstat_easy_free(fse);
}

TEST(test_easy_fieldstat, output_to_buff)
{
    struct fieldstat_easy *fse = fieldstat_easy_new(10, NULL, &TEST_TAG_STRING, 1);
    fieldstat_easy_register_counter(fse, "metric counter");
    fieldstat_easy_counter_incrby(fse, 0, 0, &TEST_TAG_INT, 1, 1);
    // read file, should be empty
    char *buff = NULL;
    size_t buff_len = 0;
    fieldstat_easy_output(fse, &buff, &buff_len);
    cJSON *root = cJSON_Parse(buff);
    cJSON *cell = cJSON_GetArrayItem(root, 0);
    cJSON *metric = cJSON_GetObjectItem(cell, "fields");
    long long value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
    EXPECT_EQ(value, 1);
    cJSON *tags = cJSON_GetObjectItem(cell, "tags");
    EXPECT_EQ(cJSON_GetObjectItem(tags, TEST_TAG_INT.key)->valueint, TEST_TAG_INT.value_longlong);
    EXPECT_STREQ(cJSON_GetObjectItem(tags, TEST_TAG_STRING.key)->valuestring, TEST_TAG_STRING.value_str);

    cJSON_Delete(root);
    free(buff);
    fieldstat_easy_free(fse);
}

cJSON *read_file()
{
    std::ifstream ifs(FILENAME);
    if (!ifs.is_open()) {
        return NULL;
    }
    std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
    printf("content: %s\n", content.c_str());
    cJSON *root = cJSON_Parse(content.c_str());
    ifs.close();
    return root;
}

TEST(test_easy_fieldstat, output_to_file)
{
    struct fieldstat_easy *fse = fieldstat_easy_new(10, NULL, NULL, 0);
    int counter_id = fieldstat_easy_register_counter(fse, "metric counter");
    fieldstat_easy_enable_auto_output(fse, FILENAME, 1);
    sleep(3); // long enough to output
    // 1st interval: read file, should be empty
    printf("1st interval\n");
    cJSON *root = read_file();
    EXPECT_EQ(cJSON_GetArraySize(root), 0); // is empty and valid
    cJSON_Delete(root);

    fieldstat_easy_counter_incrby(fse, 0, counter_id, &TEST_TAG_INT, 1, 1);
    fieldstat_easy_counter_incrby(fse, 1, counter_id, &TEST_TAG_INT, 1, 10);
    fieldstat_easy_counter_incrby(fse, 2, counter_id, &TEST_TAG_INT, 1, 100);
    sleep(2);

    // 2nd interval: merge 3 thread's data, and output

    printf("2nd interval\n");
    root = read_file();
    EXPECT_EQ(cJSON_GetArraySize(root), 1);

    cJSON *tagged_by_int = cJSON_GetArrayItem(root, 0);
    cJSON *metric = cJSON_GetObjectItem(tagged_by_int, "fields");
    EXPECT_TRUE(metric != NULL);

    long long value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
    EXPECT_EQ(value, 111);
    cJSON_Delete(root);

    // 3nd interval: no new data, just output again
    sleep(1);
    printf("3rd interval\n");
    root = read_file();
    EXPECT_EQ(cJSON_GetArraySize(root), 1);
    tagged_by_int = cJSON_GetArrayItem(root, 0);
    metric = cJSON_GetObjectItem(tagged_by_int, "fields");
    value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
    EXPECT_EQ(value, 111); // should not change
    cJSON_Delete(root);

    // 4th interval: new data, output again
    fieldstat_easy_counter_incrby(fse, 0, counter_id, &TEST_TAG_DOUBLE, 1, 10086);
    sleep(2);
    printf("4th interval\n");
    root = read_file();
    cJSON *tagged_by_double = cJSON_GetArrayItem(root, 1);
    metric = cJSON_GetObjectItem(tagged_by_double, "fields");
    value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
    EXPECT_EQ(value, 10086);
    cJSON_Delete(root);

    fieldstat_easy_free(fse);
    remove(FILENAME);
}

TEST(test_easy_fieldstat, empty_tag)
{
    struct fieldstat_easy *fse = fieldstat_easy_new(10, "ha", NULL, 0);
    int counter_id = fieldstat_easy_register_counter(fse, "metric counter");
    fieldstat_easy_counter_incrby(fse, 0, counter_id, NULL, 0, 1);
    fieldstat_easy_enable_auto_output(fse, FILENAME, 2);
    sleep(3); // long enough to output, but only once
    fieldstat_easy_free(fse);

    cJSON *root = read_file();
    EXPECT_EQ(cJSON_GetArraySize(root), 1);

    cJSON *tagged_by_int = cJSON_GetArrayItem(root, 0);
    cJSON *metric = cJSON_GetObjectItem(tagged_by_int, "fields");
    EXPECT_TRUE(metric != NULL);
    cJSON *metric_delta = cJSON_GetObjectItem(tagged_by_int, "fields_delta");
    EXPECT_TRUE(metric_delta != NULL);

    long long value = cJSON_GetObjectItem(metric, "metric counter")->valueint;
    EXPECT_EQ(value, 1);
    long long value_delta = cJSON_GetObjectItem(metric_delta, "metric counter")->valueint;
    EXPECT_EQ(value_delta, 1);

    cJSON_Delete(root);
    remove(FILENAME);
}

extern "C" {
    extern char *fs_easy_output_to_json(struct fieldstat_easy *fs, struct timeval *timestamp);
}

int g_output_count = 0;
char *fs_easy_output_to_json_stub(struct fieldstat_easy *fs, struct timeval *timestamp)
{
    g_output_count++;
    return NULL;
}

TEST(test_easy_fieldstat, output_interval_ok)
{
    Stub stub;
    stub.set(fs_easy_output_to_json, fs_easy_output_to_json_stub);

    struct fieldstat_easy *fse = fieldstat_easy_new(10, NULL, NULL, 0);
    fieldstat_easy_register_histogram(fse, "metric histogram", 1, 100000, 3); // a pretty time consuming metric
    fieldstat_easy_histogram_record(fse, 0, 0, &TEST_TAG_INT, 1, 1);
    fieldstat_easy_histogram_record(fse, 0, 0, &TEST_TAG_DOUBLE, 1, 10);
    fieldstat_easy_histogram_record(fse, 0, 0, &TEST_TAG_STRING, 1, 110);

    fieldstat_easy_enable_auto_output(fse, FILENAME, 1);

    sleep(30); // output 30s/1s = 20 times
    fieldstat_easy_free(fse);

    EXPECT_TRUE(g_output_count == 30 || g_output_count == 29); // missing only one is ok and expected
}

TEST(test_easy_fieldstat, ensure_data_racing_of_two_output_and_of_incyby)
{
    const int N_THREADS = 3;
    struct fieldstat_tag global_tags[1];
    struct fieldstat_tag tmptag;
    tmptag.key = "app id";
    tmptag.type = TAG_INTEGER;
    tmptag.value_longlong = 1;
    global_tags[0] = tmptag;
    
    struct fieldstat_easy *fse = fieldstat_easy_new(N_THREADS, NULL, global_tags, 1);
    int counter_id = fieldstat_easy_register_counter(fse, "incoming bytes");
    int hdr_id = fieldstat_easy_register_histogram(fse, "delay", 1, 10000, 1);
    fieldstat_easy_enable_auto_output(fse, FILENAME, 1);

    std::thread threads[N_THREADS];
    for (int thread_id = 0; thread_id < N_THREADS; thread_id++) {
        threads[thread_id] = std::thread([fse, counter_id, hdr_id, thread_id]() {
            for (int i = 0; i < 1000000; i++) { // loop million times to ensure no core dump
                fieldstat_easy_counter_incrby(fse, thread_id, counter_id, &TEST_TAG_INT, 1, 1);
                fieldstat_easy_histogram_record(fse, thread_id, hdr_id, &TEST_TAG_INT, 1, rand() % 10000);
                usleep(1); // 1us * 1000000 = 1s, just long enough to output
            }
        });
    }
    for (int i = 0; i < N_THREADS; i++) {
        threads[i].join();
    }
    fieldstat_easy_free(fse);

    remove(FILENAME);
}

TEST(test_easy_fieldstat, reset)
{
    struct fieldstat_easy *fse = fieldstat_easy_new(3, NULL, NULL, 0);
    int counter_id = fieldstat_easy_register_counter(fse, "metric counter");
    fieldstat_easy_counter_incrby(fse, 0, counter_id, &TEST_TAG_INT, 1, 1);
    fieldstat_easy_counter_incrby(fse, 1, counter_id, &TEST_TAG_INT, 1, 1);
    
    fieldstat_easy_reset(fse);
    char *buff = NULL;
    size_t buff_len = 0;
    fieldstat_easy_output(fse, &buff, &buff_len);
    EXPECT_STREQ(buff, "[]");

    free(buff);
    fieldstat_easy_free(fse);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}