blob: 707f606fd03c4e0bb3a14cc5bdda131a4859feea [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
Oliver Smithd89d35e2021-03-17 15:29:55 +010025#include <osmocom/core/application.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020026#include <osmocom/core/logging.h>
27#include <osmocom/core/utils.h>
28#include <osmocom/core/stat_item.h>
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010029#include <osmocom/core/rate_ctr.h>
30#include <osmocom/core/stats.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020031
32#include <stdio.h>
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +010033#include <inttypes.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020034
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010035enum test_ctr {
36 TEST_A_CTR,
37 TEST_B_CTR,
38};
39
40static const struct rate_ctr_desc ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080041 [TEST_A_CTR] = { "ctr:a", "The A counter value"},
42 [TEST_B_CTR] = { "ctr:b", "The B counter value"},
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010043};
44
45static const struct rate_ctr_group_desc ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080046 .group_name_prefix = "ctr-test:one",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010047 .group_description = "Counter test number 1",
48 .num_ctr = ARRAY_SIZE(ctr_description),
49 .ctr_desc = ctr_description,
50 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
51};
52
Harald Welte04c88122017-10-03 18:34:48 +080053static const struct rate_ctr_desc ctr_description_dot[] = {
54 [TEST_A_CTR] = { "ctr.a", "The A counter value with ."},
55 [TEST_B_CTR] = { "ctr.b", "The B counter value with ."},
56};
57
58static const struct rate_ctr_group_desc ctrg_desc_dot = {
59 .group_name_prefix = "ctr-test.one_dot",
60 .group_description = "Counter test number 1dot",
61 .num_ctr = ARRAY_SIZE(ctr_description_dot),
62 .ctr_desc = ctr_description_dot,
63 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
64};
65
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010066enum test_items {
67 TEST_A_ITEM,
68 TEST_B_ITEM,
69};
70
71static const struct osmo_stat_item_desc item_description[] = {
72 [TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 },
73 [TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 },
74};
75
76static const struct osmo_stat_item_group_desc statg_desc = {
77 .group_name_prefix = "test.one",
78 .group_description = "Test number 1",
79 .num_items = ARRAY_SIZE(item_description),
80 .item_desc = item_description,
81 .class_id = OSMO_STATS_CLASS_PEER,
82};
83
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020084static void stat_test(void)
85{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010086 struct osmo_stat_item_group *statg =
87 osmo_stat_item_group_alloc(NULL, &statg_desc, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020088
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010089 struct osmo_stat_item_group *sgrp2;
90 const struct osmo_stat_item *sitem1, *sitem2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020091 int rc;
92 int32_t value;
93 int32_t rd_a = 0;
94 int32_t rd_b = 0;
95 int i;
96
97 OSMO_ASSERT(statg != NULL);
98
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010099 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200100 OSMO_ASSERT(sgrp2 == statg);
101
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100102 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200103 OSMO_ASSERT(sgrp2 == NULL);
104
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200106 OSMO_ASSERT(sgrp2 == NULL);
107
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108 sitem1 = osmo_stat_item_get_by_name(statg, "item.c");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200109 OSMO_ASSERT(sitem1 == NULL);
110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111 sitem1 = osmo_stat_item_get_by_name(statg, "item.a");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200112 OSMO_ASSERT(sitem1 != NULL);
113 OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]);
114
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100115 sitem2 = osmo_stat_item_get_by_name(statg, "item.b");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200116 OSMO_ASSERT(sitem2 != NULL);
117 OSMO_ASSERT(sitem2 != sitem1);
118 OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]);
119
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120 value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200121 OSMO_ASSERT(value == -1);
122
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100123 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200124 OSMO_ASSERT(rc == 0);
125
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100126 osmo_stat_item_set(statg->items[TEST_A_ITEM], 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200127
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100128 value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200129 OSMO_ASSERT(value == 1);
130
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100131 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200132 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200133 OSMO_ASSERT(value == 1);
134
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100135 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200136 OSMO_ASSERT(rc == 0);
137
138 for (i = 2; i <= 32; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100139 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
140 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200141
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100142 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200143 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200144 OSMO_ASSERT(value == i);
145
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100146 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200147 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200148 OSMO_ASSERT(value == 1000 + i);
149 }
150
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200151 /* check if dec & inc is working */
152 osmo_stat_item_set(statg->items[TEST_A_ITEM], 42);
153 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
154 OSMO_ASSERT(rc > 0);
155 OSMO_ASSERT(value == 42);
156
157 osmo_stat_item_dec(statg->items[TEST_A_ITEM], 21);
158 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
159 OSMO_ASSERT(rc > 0);
160 OSMO_ASSERT(value == 21);
161
162 osmo_stat_item_inc(statg->items[TEST_A_ITEM], 21);
163 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
164 OSMO_ASSERT(rc > 0);
165 OSMO_ASSERT(value == 42);
166
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200167 /* Keep 2 in FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100168 osmo_stat_item_set(statg->items[TEST_A_ITEM], 33);
169 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200170
171 for (i = 34; i <= 64; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100172 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
173 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200174
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100175 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200176 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200177 OSMO_ASSERT(value == i-1);
178
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100179 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200180 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200181 OSMO_ASSERT(value == 1000 + i-1);
182 }
183
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100184 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200185 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200186 OSMO_ASSERT(value == 64);
187
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100188 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200189 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200190 OSMO_ASSERT(value == 1000 + 64);
191
192 /* Overrun FIFOs */
193 for (i = 65; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100194 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
195 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200196 }
197
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100198 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200199 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200200 OSMO_ASSERT(value == 93);
201
202 for (i = 94; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100203 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200204 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200205 OSMO_ASSERT(value == i);
206 }
207
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100208 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200209 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200210 OSMO_ASSERT(value == 1000 + 90);
211
212 for (i = 91; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100213 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200214 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200215 OSMO_ASSERT(value == 1000 + i);
216 }
217
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200218 /* Test Discard (single item) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100219 osmo_stat_item_set(statg->items[TEST_A_ITEM], 97);
220 rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200221 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200222
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100223 rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200224 OSMO_ASSERT(rc == 0);
225
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100226 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200227 OSMO_ASSERT(rc == 0);
228
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100229 osmo_stat_item_set(statg->items[TEST_A_ITEM], 98);
230 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 Erlbeck9732cb42015-10-01 20:43:53 +0200232 OSMO_ASSERT(value == 98);
233
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100234 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200235 OSMO_ASSERT(rc == 0);
236
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200237 /* Test Discard (all items) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100238 osmo_stat_item_set(statg->items[TEST_A_ITEM], 99);
239 osmo_stat_item_set(statg->items[TEST_A_ITEM], 100);
240 osmo_stat_item_set(statg->items[TEST_A_ITEM], 101);
241 osmo_stat_item_set(statg->items[TEST_B_ITEM], 99);
242 osmo_stat_item_set(statg->items[TEST_B_ITEM], 100);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200243
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100244 rc = osmo_stat_item_discard_all(&rd_a);
245 rc = osmo_stat_item_discard_all(&rd_b);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200246
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100247 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200248 OSMO_ASSERT(rc == 0);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100249 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200250 OSMO_ASSERT(rc == 0);
251
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100252 osmo_stat_item_group_free(statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200253
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100254 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200255 OSMO_ASSERT(sgrp2 == NULL);
256}
257
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100258/*** stats reporter tests ***/
259
260/* define a special stats reporter for testing */
261
262static int send_count;
263
264enum {
265 OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1,
266};
267
268static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep,
269 const struct rate_ctr_group *ctrg,
270 const struct rate_ctr_desc *desc,
271 int64_t value, int64_t delta)
272{
273 const char *group_name = ctrg ? ctrg->desc->group_name_prefix : "";
274
Oliver Smithd89d35e2021-03-17 15:29:55 +0100275 fprintf(stderr, " %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100276 srep->name,
277 srep->name_prefix ? srep->name_prefix : "",
278 group_name, ctrg ? ctrg->idx : 0,
279 desc->name, (long long)value, (long long)delta);
280
281 send_count += 1;
282 return 0;
283}
284
285static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep,
286 const struct osmo_stat_item_group *statg,
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +0100287 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100288{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100289 fprintf(stderr, " %s: item p=%s g=%s i=%u n=%s v=%"PRId64" u=%s\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100290 srep->name,
291 srep->name_prefix ? srep->name_prefix : "",
292 statg->desc->group_name_prefix, statg->idx,
293 desc->name, value, desc->unit ? desc->unit : "");
294
295 send_count += 1;
296 return 0;
297}
298
299static int stats_reporter_test_open(struct osmo_stats_reporter *srep)
300{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100301 fprintf(stderr, " %s: open\n", srep->name);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100302 return 0;
303}
304
305static int stats_reporter_test_close(struct osmo_stats_reporter *srep)
306{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100307 fprintf(stderr, " %s: close\n", srep->name);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100308 return 0;
309}
310
311static struct osmo_stats_reporter *stats_reporter_create_test(const char *name)
312{
313 struct osmo_stats_reporter *srep;
314 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name);
315
316 srep->have_net_config = 0;
317
318 srep->open = stats_reporter_test_open;
319 srep->close = stats_reporter_test_close;
320 srep->send_counter = stats_reporter_test_send_counter;
321 srep->send_item = stats_reporter_test_send_item;
322
323 return srep;
324}
325
326
327static void test_reporting()
328{
329 struct osmo_stats_reporter *srep1, *srep2, *srep;
330 struct osmo_stat_item_group *statg1, *statg2;
Max3ef14a22017-12-15 20:19:10 +0100331 struct rate_ctr_group *ctrg1, *ctrg2, *ctrg3, *ctrg_dup;
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100332 void *stats_ctx = talloc_named_const(NULL, 1, "stats test context");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100333
334 int rc;
335
Oliver Smithd89d35e2021-03-17 15:29:55 +0100336 fprintf(stderr, "Start test: %s\n", __func__);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100337
338 /* Allocate counters and items */
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100339 statg1 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100340 OSMO_ASSERT(statg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100341 statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100342 OSMO_ASSERT(statg2 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100343 ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100344 OSMO_ASSERT(ctrg1 && ctrg1->idx == 1);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100345 ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100346 OSMO_ASSERT(ctrg2 && ctrg2->idx == 2);
Max3ef14a22017-12-15 20:19:10 +0100347
348 ctrg_dup = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100349 OSMO_ASSERT(ctrg_dup && ctrg_dup->idx == 3);
350 rate_ctr_group_free(ctrg_dup);
Max3ef14a22017-12-15 20:19:10 +0100351
Harald Welte04c88122017-10-03 18:34:48 +0800352 ctrg3 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc_dot, 3);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100353 OSMO_ASSERT(ctrg3 && ctrg3->idx == 3);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100354
355 srep1 = stats_reporter_create_test("test1");
356 OSMO_ASSERT(srep1 != NULL);
357
358 srep2 = stats_reporter_create_test("test2");
359 OSMO_ASSERT(srep2 != NULL);
360
361 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1");
362 OSMO_ASSERT(srep == srep1);
363 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2");
364 OSMO_ASSERT(srep == srep2);
365
366 rc = osmo_stats_reporter_enable(srep1);
367 OSMO_ASSERT(rc >= 0);
368 OSMO_ASSERT(srep1->force_single_flush);
369 rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
370 OSMO_ASSERT(rc >= 0);
371
372 rc = osmo_stats_reporter_enable(srep2);
373 OSMO_ASSERT(rc >= 0);
374 OSMO_ASSERT(srep2->force_single_flush);
375 rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER);
376 OSMO_ASSERT(rc >= 0);
377
Oliver Smithd89d35e2021-03-17 15:29:55 +0100378 fprintf(stderr, "report (initial):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100379 send_count = 0;
380 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800381 OSMO_ASSERT(send_count == 20);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100382
Oliver Smithd89d35e2021-03-17 15:29:55 +0100383 fprintf(stderr, "report (srep1 global):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100384 /* force single flush */
385 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL);
386 srep1->force_single_flush = 1;
387 srep2->force_single_flush = 1;
388 send_count = 0;
389 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800390 OSMO_ASSERT(send_count == 10);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100391
Oliver Smithd89d35e2021-03-17 15:29:55 +0100392 fprintf(stderr, "report (srep1 peer):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100393 /* force single flush */
394 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER);
395 srep1->force_single_flush = 1;
396 srep2->force_single_flush = 1;
397 send_count = 0;
398 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800399 OSMO_ASSERT(send_count == 14);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100400
Oliver Smithd89d35e2021-03-17 15:29:55 +0100401 fprintf(stderr, "report (srep1 subscriber):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100402 /* force single flush */
403 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
404 srep1->force_single_flush = 1;
405 srep2->force_single_flush = 1;
406 send_count = 0;
407 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800408 OSMO_ASSERT(send_count == 20);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100409
Oliver Smithd89d35e2021-03-17 15:29:55 +0100410 fprintf(stderr, "report (srep2 disabled):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100411 /* force single flush */
412 srep1->force_single_flush = 1;
413 srep2->force_single_flush = 1;
414 rc = osmo_stats_reporter_disable(srep2);
415 OSMO_ASSERT(rc >= 0);
416 send_count = 0;
417 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800418 OSMO_ASSERT(send_count == 10);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100419
Oliver Smithd89d35e2021-03-17 15:29:55 +0100420 fprintf(stderr, "report (srep2 enabled, no flush forced):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100421 rc = osmo_stats_reporter_enable(srep2);
422 OSMO_ASSERT(rc >= 0);
423 send_count = 0;
424 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800425 OSMO_ASSERT(send_count == 10);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100426
Oliver Smithd89d35e2021-03-17 15:29:55 +0100427 fprintf(stderr, "report (should be empty):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100428 send_count = 0;
429 osmo_stats_report();
430 OSMO_ASSERT(send_count == 0);
431
Oliver Smithd89d35e2021-03-17 15:29:55 +0100432 fprintf(stderr, "report (group 1, counter 1 update):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100433 rate_ctr_inc(&ctrg1->ctr[TEST_A_CTR]);
434 send_count = 0;
435 osmo_stats_report();
436 OSMO_ASSERT(send_count == 2);
437
Oliver Smithd89d35e2021-03-17 15:29:55 +0100438 fprintf(stderr, "report (group 1, item 1 update):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100439 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
440 send_count = 0;
441 osmo_stats_report();
442 OSMO_ASSERT(send_count == 2);
443
Oliver Smithd89d35e2021-03-17 15:29:55 +0100444 fprintf(stderr, "report (group 1, item 1 update twice):\n");
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100445 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
446 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
447 send_count = 0;
448 osmo_stats_report();
449 OSMO_ASSERT(send_count == 2);
450
Oliver Smithd89d35e2021-03-17 15:29:55 +0100451 fprintf(stderr, "report (group 1, item 1 update twice, check max):\n");
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100452 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 20);
453 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
454 send_count = 0;
455 osmo_stats_report();
456 OSMO_ASSERT(send_count == 2);
457
Oliver Smithd89d35e2021-03-17 15:29:55 +0100458 fprintf(stderr, "report (remove statg1, ctrg1):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100459 /* force single flush */
460 srep1->force_single_flush = 1;
461 srep2->force_single_flush = 1;
462 osmo_stat_item_group_free(statg1);
463 rate_ctr_group_free(ctrg1);
464 send_count = 0;
465 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800466 OSMO_ASSERT(send_count == 12);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100467
Oliver Smithd89d35e2021-03-17 15:29:55 +0100468 fprintf(stderr, "report (remove srep1):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100469 /* force single flush */
470 srep1->force_single_flush = 1;
471 srep2->force_single_flush = 1;
472 osmo_stats_reporter_free(srep1);
473 send_count = 0;
474 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800475 OSMO_ASSERT(send_count == 6);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100476
Oliver Smithd89d35e2021-03-17 15:29:55 +0100477 fprintf(stderr, "report (remove statg2):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100478 /* force single flush */
479 srep2->force_single_flush = 1;
480 osmo_stat_item_group_free(statg2);
481 send_count = 0;
482 osmo_stats_report();
Harald Welte04c88122017-10-03 18:34:48 +0800483 OSMO_ASSERT(send_count == 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100484
Oliver Smithd89d35e2021-03-17 15:29:55 +0100485 fprintf(stderr, "report (remove srep2):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100486 /* force single flush */
487 srep2->force_single_flush = 1;
488 osmo_stats_reporter_free(srep2);
489 send_count = 0;
490 osmo_stats_report();
491 OSMO_ASSERT(send_count == 0);
492
Oliver Smithd89d35e2021-03-17 15:29:55 +0100493 fprintf(stderr, "report (remove ctrg2, should be empty):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100494 rate_ctr_group_free(ctrg2);
495 send_count = 0;
496 osmo_stats_report();
497 OSMO_ASSERT(send_count == 0);
498
Harald Welte04c88122017-10-03 18:34:48 +0800499 rate_ctr_group_free(ctrg3);
500
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100501 /* Leak check */
502 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
503 talloc_free(stats_ctx);
504
Oliver Smithd89d35e2021-03-17 15:29:55 +0100505 fprintf(stderr, "End test: %s\n", __func__);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100506}
507
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200508int main(int argc, char **argv)
509{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100510 void *ctx = talloc_named_const(NULL, 0, "main");
511 osmo_init_logging2(ctx, NULL);
512
513 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
514 log_set_print_level(osmo_stderr_target, 1);
515 log_set_print_category(osmo_stderr_target, 1);
516 log_set_print_category_hex(osmo_stderr_target, 0);
517 log_set_use_color(osmo_stderr_target, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200518
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100519 osmo_stat_item_init(NULL);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200520
521 stat_test();
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100522 test_reporting();
Oliver Smithd89d35e2021-03-17 15:29:55 +0100523 talloc_free(ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200524 return 0;
525}