blob: b302b37468c3b9511df642be8fab061f7dd04215 [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001/* tests for statistics */
2/*
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2015 sysmocom - s.m.f.c. GmbH
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02004 *
5 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <osmocom/core/logging.h>
26#include <osmocom/core/utils.h>
27#include <osmocom/core/stat_item.h>
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010028#include <osmocom/core/rate_ctr.h>
29#include <osmocom/core/stats.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020030
31#include <stdio.h>
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +010032#include <inttypes.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020033
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010034enum test_ctr {
35 TEST_A_CTR,
36 TEST_B_CTR,
37};
38
39static const struct rate_ctr_desc ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080040 [TEST_A_CTR] = { "ctr:a", "The A counter value"},
41 [TEST_B_CTR] = { "ctr:b", "The B counter value"},
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010042};
43
44static const struct rate_ctr_group_desc ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080045 .group_name_prefix = "ctr-test:one",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010046 .group_description = "Counter test number 1",
47 .num_ctr = ARRAY_SIZE(ctr_description),
48 .ctr_desc = ctr_description,
49 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
50};
51
Harald Welte04c88122017-10-03 18:34:48 +080052static const struct rate_ctr_desc ctr_description_dot[] = {
53 [TEST_A_CTR] = { "ctr.a", "The A counter value with ."},
54 [TEST_B_CTR] = { "ctr.b", "The B counter value with ."},
55};
56
57static const struct rate_ctr_group_desc ctrg_desc_dot = {
58 .group_name_prefix = "ctr-test.one_dot",
59 .group_description = "Counter test number 1dot",
60 .num_ctr = ARRAY_SIZE(ctr_description_dot),
61 .ctr_desc = ctr_description_dot,
62 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
63};
64
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010065enum test_items {
66 TEST_A_ITEM,
67 TEST_B_ITEM,
68};
69
70static const struct osmo_stat_item_desc item_description[] = {
71 [TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 },
72 [TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 },
73};
74
75static const struct osmo_stat_item_group_desc statg_desc = {
76 .group_name_prefix = "test.one",
77 .group_description = "Test number 1",
78 .num_items = ARRAY_SIZE(item_description),
79 .item_desc = item_description,
80 .class_id = OSMO_STATS_CLASS_PEER,
81};
82
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020083static void stat_test(void)
84{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010085 struct osmo_stat_item_group *statg =
86 osmo_stat_item_group_alloc(NULL, &statg_desc, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020087
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010088 struct osmo_stat_item_group *sgrp2;
89 const struct osmo_stat_item *sitem1, *sitem2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020090 int rc;
91 int32_t value;
92 int32_t rd_a = 0;
93 int32_t rd_b = 0;
94 int i;
95
96 OSMO_ASSERT(statg != NULL);
97
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010098 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020099 OSMO_ASSERT(sgrp2 == statg);
100
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200102 OSMO_ASSERT(sgrp2 == NULL);
103
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100104 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200105 OSMO_ASSERT(sgrp2 == NULL);
106
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107 sitem1 = osmo_stat_item_get_by_name(statg, "item.c");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200108 OSMO_ASSERT(sitem1 == NULL);
109
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100110 sitem1 = osmo_stat_item_get_by_name(statg, "item.a");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200111 OSMO_ASSERT(sitem1 != NULL);
112 OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]);
113
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114 sitem2 = osmo_stat_item_get_by_name(statg, "item.b");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200115 OSMO_ASSERT(sitem2 != NULL);
116 OSMO_ASSERT(sitem2 != sitem1);
117 OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]);
118
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100119 value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200120 OSMO_ASSERT(value == -1);
121
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200123 OSMO_ASSERT(rc == 0);
124
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100125 osmo_stat_item_set(statg->items[TEST_A_ITEM], 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200126
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100127 value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200128 OSMO_ASSERT(value == 1);
129
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100130 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200131 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200132 OSMO_ASSERT(value == 1);
133
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100134 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200135 OSMO_ASSERT(rc == 0);
136
137 for (i = 2; i <= 32; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100138 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
139 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200140
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100141 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200142 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200143 OSMO_ASSERT(value == i);
144
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100145 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200146 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200147 OSMO_ASSERT(value == 1000 + i);
148 }
149
150 /* Keep 2 in FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100151 osmo_stat_item_set(statg->items[TEST_A_ITEM], 33);
152 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200153
154 for (i = 34; i <= 64; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100155 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
156 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200157
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100158 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200159 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200160 OSMO_ASSERT(value == i-1);
161
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100162 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200163 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200164 OSMO_ASSERT(value == 1000 + i-1);
165 }
166
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100167 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200168 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200169 OSMO_ASSERT(value == 64);
170
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100171 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200172 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200173 OSMO_ASSERT(value == 1000 + 64);
174
175 /* Overrun FIFOs */
176 for (i = 65; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100177 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
178 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200179 }
180
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100181 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200182 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200183 OSMO_ASSERT(value == 93);
184
185 for (i = 94; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100186 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200187 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200188 OSMO_ASSERT(value == i);
189 }
190
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100191 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200192 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200193 OSMO_ASSERT(value == 1000 + 90);
194
195 for (i = 91; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100196 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200197 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200198 OSMO_ASSERT(value == 1000 + i);
199 }
200
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200201 /* Test Discard (single item) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100202 osmo_stat_item_set(statg->items[TEST_A_ITEM], 97);
203 rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200204 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200205
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100206 rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200207 OSMO_ASSERT(rc == 0);
208
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100209 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200210 OSMO_ASSERT(rc == 0);
211
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100212 osmo_stat_item_set(statg->items[TEST_A_ITEM], 98);
213 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200214 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200215 OSMO_ASSERT(value == 98);
216
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100217 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200218 OSMO_ASSERT(rc == 0);
219
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200220 /* Test Discard (all items) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100221 osmo_stat_item_set(statg->items[TEST_A_ITEM], 99);
222 osmo_stat_item_set(statg->items[TEST_A_ITEM], 100);
223 osmo_stat_item_set(statg->items[TEST_A_ITEM], 101);
224 osmo_stat_item_set(statg->items[TEST_B_ITEM], 99);
225 osmo_stat_item_set(statg->items[TEST_B_ITEM], 100);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200226
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100227 rc = osmo_stat_item_discard_all(&rd_a);
228 rc = osmo_stat_item_discard_all(&rd_b);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200229
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100230 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200231 OSMO_ASSERT(rc == 0);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100232 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200233 OSMO_ASSERT(rc == 0);
234
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100235 osmo_stat_item_group_free(statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200236
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100237 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200238 OSMO_ASSERT(sgrp2 == NULL);
239}
240
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100241/*** stats reporter tests ***/
242
243/* define a special stats reporter for testing */
244
245static int send_count;
246
247enum {
248 OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1,
249};
250
251static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep,
252 const struct rate_ctr_group *ctrg,
253 const struct rate_ctr_desc *desc,
254 int64_t value, int64_t delta)
255{
256 const char *group_name = ctrg ? ctrg->desc->group_name_prefix : "";
257
258 printf(" %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n",
259 srep->name,
260 srep->name_prefix ? srep->name_prefix : "",
261 group_name, ctrg ? ctrg->idx : 0,
262 desc->name, (long long)value, (long long)delta);
263
264 send_count += 1;
265 return 0;
266}
267
268static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep,
269 const struct osmo_stat_item_group *statg,
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +0100270 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100271{
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +0100272 printf(" %s: item p=%s g=%s i=%u n=%s v=%"PRId64" u=%s\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100273 srep->name,
274 srep->name_prefix ? srep->name_prefix : "",
275 statg->desc->group_name_prefix, statg->idx,
276 desc->name, value, desc->unit ? desc->unit : "");
277
278 send_count += 1;
279 return 0;
280}
281
282static int stats_reporter_test_open(struct osmo_stats_reporter *srep)
283{
284 printf(" %s: open\n", srep->name);
285 return 0;
286}
287
288static int stats_reporter_test_close(struct osmo_stats_reporter *srep)
289{
290 printf(" %s: close\n", srep->name);
291 return 0;
292}
293
294static struct osmo_stats_reporter *stats_reporter_create_test(const char *name)
295{
296 struct osmo_stats_reporter *srep;
297 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name);
298
299 srep->have_net_config = 0;
300
301 srep->open = stats_reporter_test_open;
302 srep->close = stats_reporter_test_close;
303 srep->send_counter = stats_reporter_test_send_counter;
304 srep->send_item = stats_reporter_test_send_item;
305
306 return srep;
307}
308
309
310static void test_reporting()
311{
312 struct osmo_stats_reporter *srep1, *srep2, *srep;
313 struct osmo_stat_item_group *statg1, *statg2;
Harald Welte04c88122017-10-03 18:34:48 +0800314 struct rate_ctr_group *ctrg1, *ctrg2, *ctrg3;
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100315 void *stats_ctx = talloc_named_const(NULL, 1, "stats test context");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100316
317 int rc;
318
319 printf("Start test: %s\n", __func__);
320
321 /* Allocate counters and items */
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100322 statg1 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100323 OSMO_ASSERT(statg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100324 statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100325 OSMO_ASSERT(statg2 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100326 ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100327 OSMO_ASSERT(ctrg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100328 ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100329 OSMO_ASSERT(ctrg2 != NULL);
Harald Welte04c88122017-10-03 18:34:48 +0800330 ctrg3 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc_dot, 3);
331 OSMO_ASSERT(ctrg3 != NULL);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100332
333 srep1 = stats_reporter_create_test("test1");
334 OSMO_ASSERT(srep1 != NULL);
335
336 srep2 = stats_reporter_create_test("test2");
337 OSMO_ASSERT(srep2 != NULL);
338
339 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1");
340 OSMO_ASSERT(srep == srep1);
341 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2");
342 OSMO_ASSERT(srep == srep2);
343
344 rc = osmo_stats_reporter_enable(srep1);
345 OSMO_ASSERT(rc >= 0);
346 OSMO_ASSERT(srep1->force_single_flush);
347 rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
348 OSMO_ASSERT(rc >= 0);
349
350 rc = osmo_stats_reporter_enable(srep2);
351 OSMO_ASSERT(rc >= 0);
352 OSMO_ASSERT(srep2->force_single_flush);
353 rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER);
354 OSMO_ASSERT(rc >= 0);
355
356 printf("report (initial):\n");
357 send_count = 0;
358 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800359 OSMO_ASSERT(send_count == 20);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100360
361 printf("report (srep1 global):\n");
362 /* force single flush */
363 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL);
364 srep1->force_single_flush = 1;
365 srep2->force_single_flush = 1;
366 send_count = 0;
367 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800368 OSMO_ASSERT(send_count == 10);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100369
370 printf("report (srep1 peer):\n");
371 /* force single flush */
372 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER);
373 srep1->force_single_flush = 1;
374 srep2->force_single_flush = 1;
375 send_count = 0;
376 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800377 OSMO_ASSERT(send_count == 14);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100378
379 printf("report (srep1 subscriber):\n");
380 /* force single flush */
381 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
382 srep1->force_single_flush = 1;
383 srep2->force_single_flush = 1;
384 send_count = 0;
385 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800386 OSMO_ASSERT(send_count == 20);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100387
388 printf("report (srep2 disabled):\n");
389 /* force single flush */
390 srep1->force_single_flush = 1;
391 srep2->force_single_flush = 1;
392 rc = osmo_stats_reporter_disable(srep2);
393 OSMO_ASSERT(rc >= 0);
394 send_count = 0;
395 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800396 OSMO_ASSERT(send_count == 10);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100397
398 printf("report (srep2 enabled, no flush forced):\n");
399 rc = osmo_stats_reporter_enable(srep2);
400 OSMO_ASSERT(rc >= 0);
401 send_count = 0;
402 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800403 OSMO_ASSERT(send_count == 10);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100404
405 printf("report (should be empty):\n");
406 send_count = 0;
407 osmo_stats_report();
408 OSMO_ASSERT(send_count == 0);
409
410 printf("report (group 1, counter 1 update):\n");
411 rate_ctr_inc(&ctrg1->ctr[TEST_A_CTR]);
412 send_count = 0;
413 osmo_stats_report();
414 OSMO_ASSERT(send_count == 2);
415
416 printf("report (group 1, item 1 update):\n");
417 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
418 send_count = 0;
419 osmo_stats_report();
420 OSMO_ASSERT(send_count == 2);
421
422 printf("report (remove statg1, ctrg1):\n");
423 /* force single flush */
424 srep1->force_single_flush = 1;
425 srep2->force_single_flush = 1;
426 osmo_stat_item_group_free(statg1);
427 rate_ctr_group_free(ctrg1);
428 send_count = 0;
429 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800430 OSMO_ASSERT(send_count == 12);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100431
432 printf("report (remove srep1):\n");
433 /* force single flush */
434 srep1->force_single_flush = 1;
435 srep2->force_single_flush = 1;
436 osmo_stats_reporter_free(srep1);
437 send_count = 0;
438 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800439 OSMO_ASSERT(send_count == 6);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100440
441 printf("report (remove statg2):\n");
442 /* force single flush */
443 srep2->force_single_flush = 1;
444 osmo_stat_item_group_free(statg2);
445 send_count = 0;
446 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800447 OSMO_ASSERT(send_count == 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100448
449 printf("report (remove srep2):\n");
450 /* force single flush */
451 srep2->force_single_flush = 1;
452 osmo_stats_reporter_free(srep2);
453 send_count = 0;
454 osmo_stats_report();
455 OSMO_ASSERT(send_count == 0);
456
457 printf("report (remove ctrg2, should be empty):\n");
458 rate_ctr_group_free(ctrg2);
459 send_count = 0;
460 osmo_stats_report();
461 OSMO_ASSERT(send_count == 0);
462
Harald Welte04c88122017-10-03 18:34:48 +0800463 rate_ctr_group_free(ctrg3);
464
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100465 /* Leak check */
466 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
467 talloc_free(stats_ctx);
468
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100469 printf("End test: %s\n", __func__);
470}
471
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200472int main(int argc, char **argv)
473{
474 static const struct log_info log_info = {};
475 log_init(&log_info, NULL);
476
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100477 osmo_stat_item_init(NULL);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200478
479 stat_test();
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100480 test_reporting();
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200481 return 0;
482}