Skip to content

Commit

Permalink
cat: tests: Add test case for histogram/summary concatenation
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 authored and edsiper committed Mar 22, 2023
1 parent ec53c6a commit 41e7a11
Showing 1 changed file with 122 additions and 1 deletion.
123 changes: 122 additions & 1 deletion tests/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,60 @@
#include <cmetrics/cmt_counter.h>
#include <cmetrics/cmt_gauge.h>
#include <cmetrics/cmt_untyped.h>
#include <cmetrics/cmt_histogram.h>
#include <cmetrics/cmt_summary.h>
#include <cmetrics/cmt_encode_text.h>
#include <cmetrics/cmt_cat.h>

#include "cmt_tests.h"

/* values to observe in a histogram */
double hist_observe_values[10] = {
0.0 , 1.02, 2.04, 3.06,
4.08, 5.10, 6.12, 7.14,
8.16, 9.18
};

/*
* histogram bucket values: the values computed in the buckets,
* all of them are uint64_t.
*
* Note that on all examples we use the default buckets values, created manually
* and through the API:
*
* - 11 bucket values
* - 1 +Inf bucket value
*/
uint64_t hist_buckets_values[12] = {1, 1, 1, 1, 1, 1, 1, 1,
3, 5, 10, 10};
/* histogram _count value */
uint64_t hist_count = 10;

/* histogram _sum value */
double hist_sum = 45.9;

void test_cat()
{
int i;
int ret;
uint64_t val;
uint64_t ts;
cfl_sds_t text;
double sum;
uint64_t count;
double q[6];
double r[6];
struct cmt *cmt1;
struct cmt *cmt2;
struct cmt *cmt3;
struct cmt *cmt4;
struct cmt *cmt5;
struct cmt_counter *c;
struct cmt_gauge *g;
struct cmt_untyped *u;
struct cmt_histogram *h;
struct cmt_histogram_buckets *buckets;
struct cmt_summary *s;

/* cmetrics 1 */
cmt1 = cmt_create();
Expand Down Expand Up @@ -99,8 +137,89 @@ void test_cat()
ret = cmt_cat(cmt3, cmt2);
TEST_CHECK(ret == 0);

/* Create buckets */
buckets = cmt_histogram_buckets_create(11,
0.005, 0.01, 0.025, 0.05,
0.1, 0.25, 0.5, 1.0, 2.5,
5.0, 10.0);
TEST_CHECK(buckets != NULL);

cmt4 = cmt_create();
TEST_CHECK(cmt4 != NULL);

/* Create a histogram metric type */
h = cmt_histogram_create(cmt4,
"k8s", "network", "load", "Network load",
buckets,
1, (char *[]) {"my_label"});
TEST_CHECK(h != NULL);

ts = cfl_time_now();
for (i = 0; i < sizeof(hist_observe_values)/(sizeof(double)); i++) {
val = hist_observe_values[i];
cmt_histogram_observe(h, ts, val, 1, (char *[]) {"my_label"});
}

ret = cmt_cat(cmt4, cmt3);
TEST_CHECK(ret == 0);

cmt5 = cmt_create();
TEST_CHECK(cmt5 != NULL);

ts = cfl_time_now();

/* set quantiles */
q[0] = 0.1;
q[1] = 0.2;
q[2] = 0.3;
q[3] = 0.4;
q[4] = 0.5;
q[5] = 1.0;

r[0] = 1;
r[1] = 2;
r[2] = 3;
r[3] = 4;
r[4] = 5;
r[5] = 6;

/* Create a gauge metric type */
s = cmt_summary_create(cmt5,
"spring", "kafka_listener", "seconds", "Kafka Listener Timer",
6, q,
3, (char *[]) {"exception", "name", "result"});
TEST_CHECK(s != NULL);

/* no quantiles, labels */
sum = 0.0;
count = 1;

cmt_summary_set_default(s, ts, NULL, sum, count,
3, (char *[]) {"ListenerExecutionFailedException",
"org.springframework.kafka.KafkaListenerEndpointContainer#0-0",
"failure"});

/* no quantiles, labels */
sum = 0.1;
count = 2;
cmt_summary_set_default(s, ts, NULL, sum, count,
3, (char *[]) {"none",
"org.springframework.kafka.KafkaListenerEndpointContainer#0-0",
"success"});

/* quantiles, labels */
sum = 0.2;
count = 3;
cmt_summary_set_default(s, ts, r, sum, count,
3, (char *[]) {"extra test",
"org.springframework.kafka.KafkaListenerEndpointContainer#0-0",
"success"});

ret = cmt_cat(cmt5, cmt4);
TEST_CHECK(ret == 0);

/* check output */
text = cmt_encode_text_create(cmt3);
text = cmt_encode_text_create(cmt5);
printf("====>\n%s\n", text);

cmt_encode_text_destroy(text);
Expand All @@ -109,6 +228,8 @@ void test_cat()
cmt_destroy(cmt1);
cmt_destroy(cmt2);
cmt_destroy(cmt3);
cmt_destroy(cmt4);
cmt_destroy(cmt5);
}

TEST_LIST = {
Expand Down

0 comments on commit 41e7a11

Please sign in to comment.