stats/test: Add test for reporting

This tests uses a dedicated test reported to check several aspects of
the value reporting.
  - addition/removal of stats reporter
  - addition/removal of counters/items
  - setting of max_class
  - initial value flush
  - updating single counters/items
  - reporter retrieval
  - enable/disable

Sponsored-by: On-Waves ehf
This commit is contained in:
Jacob Erlbeck 2015-11-09 17:25:27 +01:00
parent 2e8f9ed556
commit 46b703d083
2 changed files with 369 additions and 17 deletions

View File

@ -23,28 +23,49 @@
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/stat_item.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/stats.h>
#include <stdio.h>
enum test_ctr {
TEST_A_CTR,
TEST_B_CTR,
};
static const struct rate_ctr_desc ctr_description[] = {
[TEST_A_CTR] = { "ctr.a", "The A counter value"},
[TEST_B_CTR] = { "ctr.b", "The B counter value"},
};
static const struct rate_ctr_group_desc ctrg_desc = {
.group_name_prefix = "ctr-test.one",
.group_description = "Counter test number 1",
.num_ctr = ARRAY_SIZE(ctr_description),
.ctr_desc = ctr_description,
.class_id = OSMO_STATS_CLASS_SUBSCRIBER,
};
enum test_items {
TEST_A_ITEM,
TEST_B_ITEM,
};
static const struct osmo_stat_item_desc item_description[] = {
[TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 },
[TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 },
};
static const struct osmo_stat_item_group_desc statg_desc = {
.group_name_prefix = "test.one",
.group_description = "Test number 1",
.num_items = ARRAY_SIZE(item_description),
.item_desc = item_description,
.class_id = OSMO_STATS_CLASS_PEER,
};
static void stat_test(void)
{
enum test_items {
TEST_A_ITEM,
TEST_B_ITEM,
};
static const struct osmo_stat_item_desc item_description[] = {
{ "item.a", "The A value", "ma", 4, -1 },
{ "item.b", "The B value", "kb", 7, -1 },
};
static const struct osmo_stat_item_group_desc statg_desc = {
.group_name_prefix = "test.one",
.group_description = "Test number 1",
.num_items = ARRAY_SIZE(item_description),
.item_desc = item_description,
};
struct osmo_stat_item_group *statg =
osmo_stat_item_group_alloc(NULL, &statg_desc, 0);
@ -201,6 +222,228 @@ static void stat_test(void)
OSMO_ASSERT(sgrp2 == NULL);
}
/*** stats reporter tests ***/
/* define a special stats reporter for testing */
static int send_count;
enum {
OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1,
};
static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep,
const struct rate_ctr_group *ctrg,
const struct rate_ctr_desc *desc,
int64_t value, int64_t delta)
{
const char *group_name = ctrg ? ctrg->desc->group_name_prefix : "";
printf(" %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n",
srep->name,
srep->name_prefix ? srep->name_prefix : "",
group_name, ctrg ? ctrg->idx : 0,
desc->name, (long long)value, (long long)delta);
send_count += 1;
return 0;
}
static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep,
const struct osmo_stat_item_group *statg,
const struct osmo_stat_item_desc *desc, int value)
{
printf(" %s: item p=%s g=%s i=%u n=%s v=%d u=%s\n",
srep->name,
srep->name_prefix ? srep->name_prefix : "",
statg->desc->group_name_prefix, statg->idx,
desc->name, value, desc->unit ? desc->unit : "");
send_count += 1;
return 0;
}
static int stats_reporter_test_open(struct osmo_stats_reporter *srep)
{
printf(" %s: open\n", srep->name);
return 0;
}
static int stats_reporter_test_close(struct osmo_stats_reporter *srep)
{
printf(" %s: close\n", srep->name);
return 0;
}
static struct osmo_stats_reporter *stats_reporter_create_test(const char *name)
{
struct osmo_stats_reporter *srep;
srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name);
srep->have_net_config = 0;
srep->open = stats_reporter_test_open;
srep->close = stats_reporter_test_close;
srep->send_counter = stats_reporter_test_send_counter;
srep->send_item = stats_reporter_test_send_item;
return srep;
}
static void test_reporting()
{
struct osmo_stats_reporter *srep1, *srep2, *srep;
struct osmo_stat_item_group *statg1, *statg2;
struct rate_ctr_group *ctrg1, *ctrg2;
int rc;
printf("Start test: %s\n", __func__);
/* Allocate counters and items */
statg1 = osmo_stat_item_group_alloc(NULL, &statg_desc, 1);
OSMO_ASSERT(statg1 != NULL);
statg2 = osmo_stat_item_group_alloc(NULL, &statg_desc, 2);
OSMO_ASSERT(statg2 != NULL);
ctrg1 = rate_ctr_group_alloc(NULL, &ctrg_desc, 1);
OSMO_ASSERT(ctrg1 != NULL);
ctrg2 = rate_ctr_group_alloc(NULL, &ctrg_desc, 2);
OSMO_ASSERT(ctrg2 != NULL);
srep1 = stats_reporter_create_test("test1");
OSMO_ASSERT(srep1 != NULL);
srep2 = stats_reporter_create_test("test2");
OSMO_ASSERT(srep2 != NULL);
srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1");
OSMO_ASSERT(srep == srep1);
srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2");
OSMO_ASSERT(srep == srep2);
rc = osmo_stats_reporter_enable(srep1);
OSMO_ASSERT(rc >= 0);
OSMO_ASSERT(srep1->force_single_flush);
rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
OSMO_ASSERT(rc >= 0);
rc = osmo_stats_reporter_enable(srep2);
OSMO_ASSERT(rc >= 0);
OSMO_ASSERT(srep2->force_single_flush);
rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER);
OSMO_ASSERT(rc >= 0);
printf("report (initial):\n");
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 16);
printf("report (srep1 global):\n");
/* force single flush */
osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL);
srep1->force_single_flush = 1;
srep2->force_single_flush = 1;
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 8);
printf("report (srep1 peer):\n");
/* force single flush */
osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER);
srep1->force_single_flush = 1;
srep2->force_single_flush = 1;
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 12);
printf("report (srep1 subscriber):\n");
/* force single flush */
osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
srep1->force_single_flush = 1;
srep2->force_single_flush = 1;
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 16);
printf("report (srep2 disabled):\n");
/* force single flush */
srep1->force_single_flush = 1;
srep2->force_single_flush = 1;
rc = osmo_stats_reporter_disable(srep2);
OSMO_ASSERT(rc >= 0);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 8);
printf("report (srep2 enabled, no flush forced):\n");
rc = osmo_stats_reporter_enable(srep2);
OSMO_ASSERT(rc >= 0);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 8);
printf("report (should be empty):\n");
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 0);
printf("report (group 1, counter 1 update):\n");
rate_ctr_inc(&ctrg1->ctr[TEST_A_CTR]);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 2);
printf("report (group 1, item 1 update):\n");
osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 2);
printf("report (remove statg1, ctrg1):\n");
/* force single flush */
srep1->force_single_flush = 1;
srep2->force_single_flush = 1;
osmo_stat_item_group_free(statg1);
rate_ctr_group_free(ctrg1);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 8);
printf("report (remove srep1):\n");
/* force single flush */
srep1->force_single_flush = 1;
srep2->force_single_flush = 1;
osmo_stats_reporter_free(srep1);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 4);
printf("report (remove statg2):\n");
/* force single flush */
srep2->force_single_flush = 1;
osmo_stat_item_group_free(statg2);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 2);
printf("report (remove srep2):\n");
/* force single flush */
srep2->force_single_flush = 1;
osmo_stats_reporter_free(srep2);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 0);
printf("report (remove ctrg2, should be empty):\n");
rate_ctr_group_free(ctrg2);
send_count = 0;
osmo_stats_report();
OSMO_ASSERT(send_count == 0);
printf("End test: %s\n", __func__);
}
int main(int argc, char **argv)
{
static const struct log_info log_info = {};
@ -209,5 +452,6 @@ int main(int argc, char **argv)
osmo_stat_item_init(NULL);
stat_test();
test_reporting();
return 0;
}

View File

@ -0,0 +1,108 @@
Start test: test_reporting
test1: open
test2: open
report (initial):
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test1: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test1: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
test1: item p= g=test.one i=2 n=item.b v=-1 u=kb
test2: item p= g=test.one i=1 n=item.a v=-1 u=ma
test1: item p= g=test.one i=1 n=item.a v=-1 u=ma
test2: item p= g=test.one i=1 n=item.b v=-1 u=kb
test1: item p= g=test.one i=1 n=item.b v=-1 u=kb
report (srep1 global):
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
test2: item p= g=test.one i=1 n=item.a v=-1 u=ma
test2: item p= g=test.one i=1 n=item.b v=-1 u=kb
report (srep1 peer):
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test1: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
test1: item p= g=test.one i=2 n=item.b v=-1 u=kb
test2: item p= g=test.one i=1 n=item.a v=-1 u=ma
test1: item p= g=test.one i=1 n=item.a v=-1 u=ma
test2: item p= g=test.one i=1 n=item.b v=-1 u=kb
test1: item p= g=test.one i=1 n=item.b v=-1 u=kb
report (srep1 subscriber):
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test1: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test1: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
test1: item p= g=test.one i=2 n=item.b v=-1 u=kb
test2: item p= g=test.one i=1 n=item.a v=-1 u=ma
test1: item p= g=test.one i=1 n=item.a v=-1 u=ma
test2: item p= g=test.one i=1 n=item.b v=-1 u=kb
test1: item p= g=test.one i=1 n=item.b v=-1 u=kb
report (srep2 disabled):
test2: close
test1: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test1: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test1: item p= g=test.one i=2 n=item.a v=-1 u=ma
test1: item p= g=test.one i=2 n=item.b v=-1 u=kb
test1: item p= g=test.one i=1 n=item.a v=-1 u=ma
test1: item p= g=test.one i=1 n=item.b v=-1 u=kb
report (srep2 enabled, no flush forced):
test2: open
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=1 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
test2: item p= g=test.one i=1 n=item.a v=-1 u=ma
test2: item p= g=test.one i=1 n=item.b v=-1 u=kb
report (should be empty):
report (group 1, counter 1 update):
test2: counter p= g=ctr-test.one i=1 n=ctr.a v=1 d=1
test1: counter p= g=ctr-test.one i=1 n=ctr.a v=1 d=1
report (group 1, item 1 update):
test2: item p= g=test.one i=1 n=item.a v=10 u=ma
test1: item p= g=test.one i=1 n=item.a v=10 u=ma
report (remove statg1, ctrg1):
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test1: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test1: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
test1: item p= g=test.one i=2 n=item.b v=-1 u=kb
report (remove srep1):
test1: close
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
test2: item p= g=test.one i=2 n=item.a v=-1 u=ma
test2: item p= g=test.one i=2 n=item.b v=-1 u=kb
report (remove statg2):
test2: counter p= g=ctr-test.one i=2 n=ctr.a v=0 d=0
test2: counter p= g=ctr-test.one i=2 n=ctr.b v=0 d=0
report (remove srep2):
test2: close
report (remove ctrg2, should be empty):
End test: test_reporting