From 9732cb4a92a883c7e9f7dcd928b6e22976a797ca Mon Sep 17 00:00:00 2001 From: Jacob Erlbeck Date: Thu, 1 Oct 2015 20:43:53 +0200 Subject: stats: Add stat_item for value monitoring This commit adds instrumentation function to gather measurement and statistical values similar to counter groups. Multiple values can be stored per item, which can be retrieved in FIFO order. Getting values from the item does not modify its state to allow for multiple independant backends (e.g. VTY and statd). When a new value is set, the oldest value gets silently overwritten. Lost values are skipped when getting values from the item. Sponsored-by: On-Waves ehf --- tests/stats/stats_test.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++ tests/stats/stats_test.ok | 0 2 files changed, 198 insertions(+) create mode 100644 tests/stats/stats_test.c create mode 100644 tests/stats/stats_test.ok (limited to 'tests/stats') diff --git a/tests/stats/stats_test.c b/tests/stats/stats_test.c new file mode 100644 index 00000000..b4143853 --- /dev/null +++ b/tests/stats/stats_test.c @@ -0,0 +1,198 @@ +/* tests for statistics */ +/* + * (C) 2015 Sysmocom s.m.f.c. GmbH + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include +#include +#include + +#include + +static void stat_test(void) +{ + enum test_items { + TEST_A_ITEM, + TEST_B_ITEM, + }; + + static const struct stat_item_desc item_description[] = { + { "item.a", "The A value", "ma", 4, -1 }, + { "item.b", "The B value", "kb", 7, -1 }, + }; + + static const struct 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 stat_item_group *statg = + stat_item_group_alloc(NULL, &statg_desc, 0); + + struct stat_item_group *sgrp2; + const struct stat_item *sitem1, *sitem2; + int rc; + int32_t value; + int32_t rd_a = 0; + int32_t rd_b = 0; + int i; + + OSMO_ASSERT(statg != NULL); + + sgrp2 = stat_item_get_group_by_name_idx("test.one", 0); + OSMO_ASSERT(sgrp2 == statg); + + sgrp2 = stat_item_get_group_by_name_idx("test.one", 1); + OSMO_ASSERT(sgrp2 == NULL); + + sgrp2 = stat_item_get_group_by_name_idx("test.two", 0); + OSMO_ASSERT(sgrp2 == NULL); + + sitem1 = stat_item_get_by_name(statg, "item.c"); + OSMO_ASSERT(sitem1 == NULL); + + sitem1 = stat_item_get_by_name(statg, "item.a"); + OSMO_ASSERT(sitem1 != NULL); + OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]); + + sitem2 = stat_item_get_by_name(statg, "item.b"); + OSMO_ASSERT(sitem2 != NULL); + OSMO_ASSERT(sitem2 != sitem1); + OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]); + + value = stat_item_get_last(statg->items[TEST_A_ITEM]); + OSMO_ASSERT(value == -1); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 0); + + stat_item_set(statg->items[TEST_A_ITEM], 1); + + value = stat_item_get_last(statg->items[TEST_A_ITEM]); + OSMO_ASSERT(value == 1); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 1); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 0); + + for (i = 2; i <= 32; i++) { + stat_item_set(statg->items[TEST_A_ITEM], i); + stat_item_set(statg->items[TEST_B_ITEM], 1000 + i); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == i); + + rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 1000 + i); + } + + /* Keep 2 in FIFO */ + stat_item_set(statg->items[TEST_A_ITEM], 33); + stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33); + + for (i = 34; i <= 64; i++) { + stat_item_set(statg->items[TEST_A_ITEM], i); + stat_item_set(statg->items[TEST_B_ITEM], 1000 + i); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == i-1); + + rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 1000 + i-1); + } + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 64); + + rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 1000 + 64); + + /* Overrun FIFOs */ + for (i = 65; i <= 96; i++) { + stat_item_set(statg->items[TEST_A_ITEM], i); + stat_item_set(statg->items[TEST_B_ITEM], 1000 + i); + } + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 93 - 65 + 1); + OSMO_ASSERT(value == 93); + + for (i = 94; i <= 96; i++) { + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == i); + } + + rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); + OSMO_ASSERT(rc == 90 - 65 + 1); + OSMO_ASSERT(value == 1000 + 90); + + for (i = 91; i <= 96; i++) { + rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 1000 + i); + } + + /* Test Discard */ + stat_item_set(statg->items[TEST_A_ITEM], 97); + rc = stat_item_discard(statg->items[TEST_A_ITEM], &rd_a); + OSMO_ASSERT(rc == 1); + + rc = stat_item_discard(statg->items[TEST_A_ITEM], &rd_a); + OSMO_ASSERT(rc == 0); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 0); + + stat_item_set(statg->items[TEST_A_ITEM], 98); + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 1); + OSMO_ASSERT(value == 98); + + rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); + OSMO_ASSERT(rc == 0); + + stat_item_group_free(statg); + + sgrp2 = stat_item_get_group_by_name_idx("test.one", 0); + OSMO_ASSERT(sgrp2 == NULL); +} + +int main(int argc, char **argv) +{ + static const struct log_info log_info = {}; + log_init(&log_info, NULL); + + stat_item_init(NULL); + + stat_test(); + return 0; +} diff --git a/tests/stats/stats_test.ok b/tests/stats/stats_test.ok new file mode 100644 index 00000000..e69de29b -- cgit v1.2.3