blob: 75ddf181b360fd5b73643920b411890421a5d763 [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001/* tests for statistics */
2/*
3 * (C) 2015 Sysmocom s.m.f.c. GmbH
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <osmocom/core/logging.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/core/stat_item.h>
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010026#include <osmocom/core/rate_ctr.h>
27#include <osmocom/core/stats.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020028
29#include <stdio.h>
30
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010031enum test_ctr {
32 TEST_A_CTR,
33 TEST_B_CTR,
34};
35
36static const struct rate_ctr_desc ctr_description[] = {
37 [TEST_A_CTR] = { "ctr.a", "The A counter value"},
38 [TEST_B_CTR] = { "ctr.b", "The B counter value"},
39};
40
41static const struct rate_ctr_group_desc ctrg_desc = {
42 .group_name_prefix = "ctr-test.one",
43 .group_description = "Counter test number 1",
44 .num_ctr = ARRAY_SIZE(ctr_description),
45 .ctr_desc = ctr_description,
46 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
47};
48
49enum test_items {
50 TEST_A_ITEM,
51 TEST_B_ITEM,
52};
53
54static const struct osmo_stat_item_desc item_description[] = {
55 [TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 },
56 [TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 },
57};
58
59static const struct osmo_stat_item_group_desc statg_desc = {
60 .group_name_prefix = "test.one",
61 .group_description = "Test number 1",
62 .num_items = ARRAY_SIZE(item_description),
63 .item_desc = item_description,
64 .class_id = OSMO_STATS_CLASS_PEER,
65};
66
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020067static void stat_test(void)
68{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010069 struct osmo_stat_item_group *statg =
70 osmo_stat_item_group_alloc(NULL, &statg_desc, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020071
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010072 struct osmo_stat_item_group *sgrp2;
73 const struct osmo_stat_item *sitem1, *sitem2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020074 int rc;
75 int32_t value;
76 int32_t rd_a = 0;
77 int32_t rd_b = 0;
78 int i;
79
80 OSMO_ASSERT(statg != NULL);
81
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010082 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020083 OSMO_ASSERT(sgrp2 == statg);
84
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010085 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020086 OSMO_ASSERT(sgrp2 == NULL);
87
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010088 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020089 OSMO_ASSERT(sgrp2 == NULL);
90
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010091 sitem1 = osmo_stat_item_get_by_name(statg, "item.c");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020092 OSMO_ASSERT(sitem1 == NULL);
93
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010094 sitem1 = osmo_stat_item_get_by_name(statg, "item.a");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020095 OSMO_ASSERT(sitem1 != NULL);
96 OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]);
97
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010098 sitem2 = osmo_stat_item_get_by_name(statg, "item.b");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020099 OSMO_ASSERT(sitem2 != NULL);
100 OSMO_ASSERT(sitem2 != sitem1);
101 OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]);
102
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103 value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200104 OSMO_ASSERT(value == -1);
105
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100106 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200107 OSMO_ASSERT(rc == 0);
108
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109 osmo_stat_item_set(statg->items[TEST_A_ITEM], 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111 value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200112 OSMO_ASSERT(value == 1);
113
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200115 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200116 OSMO_ASSERT(value == 1);
117
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200119 OSMO_ASSERT(rc == 0);
120
121 for (i = 2; i <= 32; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
123 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200124
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100125 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200126 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200127 OSMO_ASSERT(value == i);
128
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100129 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200130 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200131 OSMO_ASSERT(value == 1000 + i);
132 }
133
134 /* Keep 2 in FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100135 osmo_stat_item_set(statg->items[TEST_A_ITEM], 33);
136 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200137
138 for (i = 34; i <= 64; 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-1);
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-1);
149 }
150
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100151 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200152 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200153 OSMO_ASSERT(value == 64);
154
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100155 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200156 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200157 OSMO_ASSERT(value == 1000 + 64);
158
159 /* Overrun FIFOs */
160 for (i = 65; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100161 osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
162 osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200163 }
164
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100165 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200166 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200167 OSMO_ASSERT(value == 93);
168
169 for (i = 94; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100170 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200171 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200172 OSMO_ASSERT(value == i);
173 }
174
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100175 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200176 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200177 OSMO_ASSERT(value == 1000 + 90);
178
179 for (i = 91; i <= 96; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100180 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200181 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200182 OSMO_ASSERT(value == 1000 + i);
183 }
184
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200185 /* Test Discard (single item) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100186 osmo_stat_item_set(statg->items[TEST_A_ITEM], 97);
187 rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200188 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200189
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100190 rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200191 OSMO_ASSERT(rc == 0);
192
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100193 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200194 OSMO_ASSERT(rc == 0);
195
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100196 osmo_stat_item_set(statg->items[TEST_A_ITEM], 98);
197 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200198 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200199 OSMO_ASSERT(value == 98);
200
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100201 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200202 OSMO_ASSERT(rc == 0);
203
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200204 /* Test Discard (all items) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100205 osmo_stat_item_set(statg->items[TEST_A_ITEM], 99);
206 osmo_stat_item_set(statg->items[TEST_A_ITEM], 100);
207 osmo_stat_item_set(statg->items[TEST_A_ITEM], 101);
208 osmo_stat_item_set(statg->items[TEST_B_ITEM], 99);
209 osmo_stat_item_set(statg->items[TEST_B_ITEM], 100);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200210
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100211 rc = osmo_stat_item_discard_all(&rd_a);
212 rc = osmo_stat_item_discard_all(&rd_b);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200213
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100214 rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200215 OSMO_ASSERT(rc == 0);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100216 rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200217 OSMO_ASSERT(rc == 0);
218
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100219 osmo_stat_item_group_free(statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200220
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100221 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200222 OSMO_ASSERT(sgrp2 == NULL);
223}
224
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100225/*** stats reporter tests ***/
226
227/* define a special stats reporter for testing */
228
229static int send_count;
230
231enum {
232 OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1,
233};
234
235static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep,
236 const struct rate_ctr_group *ctrg,
237 const struct rate_ctr_desc *desc,
238 int64_t value, int64_t delta)
239{
240 const char *group_name = ctrg ? ctrg->desc->group_name_prefix : "";
241
242 printf(" %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n",
243 srep->name,
244 srep->name_prefix ? srep->name_prefix : "",
245 group_name, ctrg ? ctrg->idx : 0,
246 desc->name, (long long)value, (long long)delta);
247
248 send_count += 1;
249 return 0;
250}
251
252static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep,
253 const struct osmo_stat_item_group *statg,
254 const struct osmo_stat_item_desc *desc, int value)
255{
256 printf(" %s: item p=%s g=%s i=%u n=%s v=%d u=%s\n",
257 srep->name,
258 srep->name_prefix ? srep->name_prefix : "",
259 statg->desc->group_name_prefix, statg->idx,
260 desc->name, value, desc->unit ? desc->unit : "");
261
262 send_count += 1;
263 return 0;
264}
265
266static int stats_reporter_test_open(struct osmo_stats_reporter *srep)
267{
268 printf(" %s: open\n", srep->name);
269 return 0;
270}
271
272static int stats_reporter_test_close(struct osmo_stats_reporter *srep)
273{
274 printf(" %s: close\n", srep->name);
275 return 0;
276}
277
278static struct osmo_stats_reporter *stats_reporter_create_test(const char *name)
279{
280 struct osmo_stats_reporter *srep;
281 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name);
282
283 srep->have_net_config = 0;
284
285 srep->open = stats_reporter_test_open;
286 srep->close = stats_reporter_test_close;
287 srep->send_counter = stats_reporter_test_send_counter;
288 srep->send_item = stats_reporter_test_send_item;
289
290 return srep;
291}
292
293
294static void test_reporting()
295{
296 struct osmo_stats_reporter *srep1, *srep2, *srep;
297 struct osmo_stat_item_group *statg1, *statg2;
298 struct rate_ctr_group *ctrg1, *ctrg2;
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100299 void *stats_ctx = talloc_named_const(NULL, 1, "stats test context");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100300
301 int rc;
302
303 printf("Start test: %s\n", __func__);
304
305 /* Allocate counters and items */
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100306 statg1 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100307 OSMO_ASSERT(statg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100308 statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100309 OSMO_ASSERT(statg2 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100310 ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100311 OSMO_ASSERT(ctrg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100312 ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100313 OSMO_ASSERT(ctrg2 != NULL);
314
315 srep1 = stats_reporter_create_test("test1");
316 OSMO_ASSERT(srep1 != NULL);
317
318 srep2 = stats_reporter_create_test("test2");
319 OSMO_ASSERT(srep2 != NULL);
320
321 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1");
322 OSMO_ASSERT(srep == srep1);
323 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2");
324 OSMO_ASSERT(srep == srep2);
325
326 rc = osmo_stats_reporter_enable(srep1);
327 OSMO_ASSERT(rc >= 0);
328 OSMO_ASSERT(srep1->force_single_flush);
329 rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
330 OSMO_ASSERT(rc >= 0);
331
332 rc = osmo_stats_reporter_enable(srep2);
333 OSMO_ASSERT(rc >= 0);
334 OSMO_ASSERT(srep2->force_single_flush);
335 rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER);
336 OSMO_ASSERT(rc >= 0);
337
338 printf("report (initial):\n");
339 send_count = 0;
340 osmo_stats_report();
341 OSMO_ASSERT(send_count == 16);
342
343 printf("report (srep1 global):\n");
344 /* force single flush */
345 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL);
346 srep1->force_single_flush = 1;
347 srep2->force_single_flush = 1;
348 send_count = 0;
349 osmo_stats_report();
350 OSMO_ASSERT(send_count == 8);
351
352 printf("report (srep1 peer):\n");
353 /* force single flush */
354 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER);
355 srep1->force_single_flush = 1;
356 srep2->force_single_flush = 1;
357 send_count = 0;
358 osmo_stats_report();
359 OSMO_ASSERT(send_count == 12);
360
361 printf("report (srep1 subscriber):\n");
362 /* force single flush */
363 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
364 srep1->force_single_flush = 1;
365 srep2->force_single_flush = 1;
366 send_count = 0;
367 osmo_stats_report();
368 OSMO_ASSERT(send_count == 16);
369
370 printf("report (srep2 disabled):\n");
371 /* force single flush */
372 srep1->force_single_flush = 1;
373 srep2->force_single_flush = 1;
374 rc = osmo_stats_reporter_disable(srep2);
375 OSMO_ASSERT(rc >= 0);
376 send_count = 0;
377 osmo_stats_report();
378 OSMO_ASSERT(send_count == 8);
379
380 printf("report (srep2 enabled, no flush forced):\n");
381 rc = osmo_stats_reporter_enable(srep2);
382 OSMO_ASSERT(rc >= 0);
383 send_count = 0;
384 osmo_stats_report();
385 OSMO_ASSERT(send_count == 8);
386
387 printf("report (should be empty):\n");
388 send_count = 0;
389 osmo_stats_report();
390 OSMO_ASSERT(send_count == 0);
391
392 printf("report (group 1, counter 1 update):\n");
393 rate_ctr_inc(&ctrg1->ctr[TEST_A_CTR]);
394 send_count = 0;
395 osmo_stats_report();
396 OSMO_ASSERT(send_count == 2);
397
398 printf("report (group 1, item 1 update):\n");
399 osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10);
400 send_count = 0;
401 osmo_stats_report();
402 OSMO_ASSERT(send_count == 2);
403
404 printf("report (remove statg1, ctrg1):\n");
405 /* force single flush */
406 srep1->force_single_flush = 1;
407 srep2->force_single_flush = 1;
408 osmo_stat_item_group_free(statg1);
409 rate_ctr_group_free(ctrg1);
410 send_count = 0;
411 osmo_stats_report();
412 OSMO_ASSERT(send_count == 8);
413
414 printf("report (remove srep1):\n");
415 /* force single flush */
416 srep1->force_single_flush = 1;
417 srep2->force_single_flush = 1;
418 osmo_stats_reporter_free(srep1);
419 send_count = 0;
420 osmo_stats_report();
421 OSMO_ASSERT(send_count == 4);
422
423 printf("report (remove statg2):\n");
424 /* force single flush */
425 srep2->force_single_flush = 1;
426 osmo_stat_item_group_free(statg2);
427 send_count = 0;
428 osmo_stats_report();
429 OSMO_ASSERT(send_count == 2);
430
431 printf("report (remove srep2):\n");
432 /* force single flush */
433 srep2->force_single_flush = 1;
434 osmo_stats_reporter_free(srep2);
435 send_count = 0;
436 osmo_stats_report();
437 OSMO_ASSERT(send_count == 0);
438
439 printf("report (remove ctrg2, should be empty):\n");
440 rate_ctr_group_free(ctrg2);
441 send_count = 0;
442 osmo_stats_report();
443 OSMO_ASSERT(send_count == 0);
444
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100445 /* Leak check */
446 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
447 talloc_free(stats_ctx);
448
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100449 printf("End test: %s\n", __func__);
450}
451
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200452int main(int argc, char **argv)
453{
454 static const struct log_info log_info = {};
455 log_init(&log_info, NULL);
456
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100457 osmo_stat_item_init(NULL);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200458
459 stat_test();
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100460 test_reporting();
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200461 return 0;
462}