Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 1 | /* 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 Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 26 | #include <osmocom/core/rate_ctr.h> |
| 27 | #include <osmocom/core/stats.h> |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 28 | |
| 29 | #include <stdio.h> |
Neels Hofmeyr | b41b48e | 2017-01-13 00:11:34 +0100 | [diff] [blame] | 30 | #include <inttypes.h> |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 31 | |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 32 | enum test_ctr { |
| 33 | TEST_A_CTR, |
| 34 | TEST_B_CTR, |
| 35 | }; |
| 36 | |
| 37 | static const struct rate_ctr_desc ctr_description[] = { |
| 38 | [TEST_A_CTR] = { "ctr.a", "The A counter value"}, |
| 39 | [TEST_B_CTR] = { "ctr.b", "The B counter value"}, |
| 40 | }; |
| 41 | |
| 42 | static const struct rate_ctr_group_desc ctrg_desc = { |
| 43 | .group_name_prefix = "ctr-test.one", |
| 44 | .group_description = "Counter test number 1", |
| 45 | .num_ctr = ARRAY_SIZE(ctr_description), |
| 46 | .ctr_desc = ctr_description, |
| 47 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
| 48 | }; |
| 49 | |
| 50 | enum test_items { |
| 51 | TEST_A_ITEM, |
| 52 | TEST_B_ITEM, |
| 53 | }; |
| 54 | |
| 55 | static const struct osmo_stat_item_desc item_description[] = { |
| 56 | [TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 }, |
| 57 | [TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 }, |
| 58 | }; |
| 59 | |
| 60 | static const struct osmo_stat_item_group_desc statg_desc = { |
| 61 | .group_name_prefix = "test.one", |
| 62 | .group_description = "Test number 1", |
| 63 | .num_items = ARRAY_SIZE(item_description), |
| 64 | .item_desc = item_description, |
| 65 | .class_id = OSMO_STATS_CLASS_PEER, |
| 66 | }; |
| 67 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 68 | static void stat_test(void) |
| 69 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 70 | struct osmo_stat_item_group *statg = |
| 71 | osmo_stat_item_group_alloc(NULL, &statg_desc, 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 72 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 73 | struct osmo_stat_item_group *sgrp2; |
| 74 | const struct osmo_stat_item *sitem1, *sitem2; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 75 | int rc; |
| 76 | int32_t value; |
| 77 | int32_t rd_a = 0; |
| 78 | int32_t rd_b = 0; |
| 79 | int i; |
| 80 | |
| 81 | OSMO_ASSERT(statg != NULL); |
| 82 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 83 | sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 84 | OSMO_ASSERT(sgrp2 == statg); |
| 85 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 86 | sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 87 | OSMO_ASSERT(sgrp2 == NULL); |
| 88 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 89 | sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 90 | OSMO_ASSERT(sgrp2 == NULL); |
| 91 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 92 | sitem1 = osmo_stat_item_get_by_name(statg, "item.c"); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 93 | OSMO_ASSERT(sitem1 == NULL); |
| 94 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 95 | sitem1 = osmo_stat_item_get_by_name(statg, "item.a"); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 96 | OSMO_ASSERT(sitem1 != NULL); |
| 97 | OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]); |
| 98 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 99 | sitem2 = osmo_stat_item_get_by_name(statg, "item.b"); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 100 | OSMO_ASSERT(sitem2 != NULL); |
| 101 | OSMO_ASSERT(sitem2 != sitem1); |
| 102 | OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]); |
| 103 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 104 | value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 105 | OSMO_ASSERT(value == -1); |
| 106 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 108 | OSMO_ASSERT(rc == 0); |
| 109 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 110 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 1); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 111 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 112 | value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 113 | OSMO_ASSERT(value == 1); |
| 114 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 115 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 116 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 117 | OSMO_ASSERT(value == 1); |
| 118 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 119 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 120 | OSMO_ASSERT(rc == 0); |
| 121 | |
| 122 | for (i = 2; i <= 32; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 123 | osmo_stat_item_set(statg->items[TEST_A_ITEM], i); |
| 124 | osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 125 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 126 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 127 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 128 | OSMO_ASSERT(value == i); |
| 129 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 130 | rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 131 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 132 | OSMO_ASSERT(value == 1000 + i); |
| 133 | } |
| 134 | |
| 135 | /* Keep 2 in FIFO */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 136 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 33); |
| 137 | osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 138 | |
| 139 | for (i = 34; i <= 64; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 140 | osmo_stat_item_set(statg->items[TEST_A_ITEM], i); |
| 141 | osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 142 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 143 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 144 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 145 | OSMO_ASSERT(value == i-1); |
| 146 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 147 | rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 148 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 149 | OSMO_ASSERT(value == 1000 + i-1); |
| 150 | } |
| 151 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 152 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 153 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 154 | OSMO_ASSERT(value == 64); |
| 155 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 156 | rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 157 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 158 | OSMO_ASSERT(value == 1000 + 64); |
| 159 | |
| 160 | /* Overrun FIFOs */ |
| 161 | for (i = 65; i <= 96; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 162 | osmo_stat_item_set(statg->items[TEST_A_ITEM], i); |
| 163 | osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 166 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 167 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 168 | OSMO_ASSERT(value == 93); |
| 169 | |
| 170 | for (i = 94; i <= 96; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 171 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 172 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 173 | OSMO_ASSERT(value == i); |
| 174 | } |
| 175 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 176 | rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 177 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 178 | OSMO_ASSERT(value == 1000 + 90); |
| 179 | |
| 180 | for (i = 91; i <= 96; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 181 | rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 182 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 183 | OSMO_ASSERT(value == 1000 + i); |
| 184 | } |
| 185 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 186 | /* Test Discard (single item) */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 187 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 97); |
| 188 | rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 189 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 190 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 191 | rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 192 | OSMO_ASSERT(rc == 0); |
| 193 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 194 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 195 | OSMO_ASSERT(rc == 0); |
| 196 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 197 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 98); |
| 198 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 199 | OSMO_ASSERT(rc > 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 200 | OSMO_ASSERT(value == 98); |
| 201 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 202 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 203 | OSMO_ASSERT(rc == 0); |
| 204 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 205 | /* Test Discard (all items) */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 206 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 99); |
| 207 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 100); |
| 208 | osmo_stat_item_set(statg->items[TEST_A_ITEM], 101); |
| 209 | osmo_stat_item_set(statg->items[TEST_B_ITEM], 99); |
| 210 | osmo_stat_item_set(statg->items[TEST_B_ITEM], 100); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 211 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 212 | rc = osmo_stat_item_discard_all(&rd_a); |
| 213 | rc = osmo_stat_item_discard_all(&rd_b); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 214 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 215 | rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 216 | OSMO_ASSERT(rc == 0); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 217 | rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 218 | OSMO_ASSERT(rc == 0); |
| 219 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 220 | osmo_stat_item_group_free(statg); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 221 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 222 | sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 223 | OSMO_ASSERT(sgrp2 == NULL); |
| 224 | } |
| 225 | |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 226 | /*** stats reporter tests ***/ |
| 227 | |
| 228 | /* define a special stats reporter for testing */ |
| 229 | |
| 230 | static int send_count; |
| 231 | |
| 232 | enum { |
| 233 | OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1, |
| 234 | }; |
| 235 | |
| 236 | static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep, |
| 237 | const struct rate_ctr_group *ctrg, |
| 238 | const struct rate_ctr_desc *desc, |
| 239 | int64_t value, int64_t delta) |
| 240 | { |
| 241 | const char *group_name = ctrg ? ctrg->desc->group_name_prefix : ""; |
| 242 | |
| 243 | printf(" %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n", |
| 244 | srep->name, |
| 245 | srep->name_prefix ? srep->name_prefix : "", |
| 246 | group_name, ctrg ? ctrg->idx : 0, |
| 247 | desc->name, (long long)value, (long long)delta); |
| 248 | |
| 249 | send_count += 1; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep, |
| 254 | const struct osmo_stat_item_group *statg, |
Neels Hofmeyr | b41b48e | 2017-01-13 00:11:34 +0100 | [diff] [blame] | 255 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 256 | { |
Neels Hofmeyr | b41b48e | 2017-01-13 00:11:34 +0100 | [diff] [blame] | 257 | printf(" %s: item p=%s g=%s i=%u n=%s v=%"PRId64" u=%s\n", |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 258 | srep->name, |
| 259 | srep->name_prefix ? srep->name_prefix : "", |
| 260 | statg->desc->group_name_prefix, statg->idx, |
| 261 | desc->name, value, desc->unit ? desc->unit : ""); |
| 262 | |
| 263 | send_count += 1; |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int stats_reporter_test_open(struct osmo_stats_reporter *srep) |
| 268 | { |
| 269 | printf(" %s: open\n", srep->name); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int stats_reporter_test_close(struct osmo_stats_reporter *srep) |
| 274 | { |
| 275 | printf(" %s: close\n", srep->name); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static struct osmo_stats_reporter *stats_reporter_create_test(const char *name) |
| 280 | { |
| 281 | struct osmo_stats_reporter *srep; |
| 282 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name); |
| 283 | |
| 284 | srep->have_net_config = 0; |
| 285 | |
| 286 | srep->open = stats_reporter_test_open; |
| 287 | srep->close = stats_reporter_test_close; |
| 288 | srep->send_counter = stats_reporter_test_send_counter; |
| 289 | srep->send_item = stats_reporter_test_send_item; |
| 290 | |
| 291 | return srep; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | static void test_reporting() |
| 296 | { |
| 297 | struct osmo_stats_reporter *srep1, *srep2, *srep; |
| 298 | struct osmo_stat_item_group *statg1, *statg2; |
| 299 | struct rate_ctr_group *ctrg1, *ctrg2; |
Jacob Erlbeck | f13de86 | 2015-11-10 11:36:58 +0100 | [diff] [blame] | 300 | void *stats_ctx = talloc_named_const(NULL, 1, "stats test context"); |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 301 | |
| 302 | int rc; |
| 303 | |
| 304 | printf("Start test: %s\n", __func__); |
| 305 | |
| 306 | /* Allocate counters and items */ |
Jacob Erlbeck | f13de86 | 2015-11-10 11:36:58 +0100 | [diff] [blame] | 307 | statg1 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 1); |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 308 | OSMO_ASSERT(statg1 != NULL); |
Jacob Erlbeck | f13de86 | 2015-11-10 11:36:58 +0100 | [diff] [blame] | 309 | statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2); |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 310 | OSMO_ASSERT(statg2 != NULL); |
Jacob Erlbeck | f13de86 | 2015-11-10 11:36:58 +0100 | [diff] [blame] | 311 | ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1); |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 312 | OSMO_ASSERT(ctrg1 != NULL); |
Jacob Erlbeck | f13de86 | 2015-11-10 11:36:58 +0100 | [diff] [blame] | 313 | ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2); |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 314 | OSMO_ASSERT(ctrg2 != NULL); |
| 315 | |
| 316 | srep1 = stats_reporter_create_test("test1"); |
| 317 | OSMO_ASSERT(srep1 != NULL); |
| 318 | |
| 319 | srep2 = stats_reporter_create_test("test2"); |
| 320 | OSMO_ASSERT(srep2 != NULL); |
| 321 | |
| 322 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1"); |
| 323 | OSMO_ASSERT(srep == srep1); |
| 324 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2"); |
| 325 | OSMO_ASSERT(srep == srep2); |
| 326 | |
| 327 | rc = osmo_stats_reporter_enable(srep1); |
| 328 | OSMO_ASSERT(rc >= 0); |
| 329 | OSMO_ASSERT(srep1->force_single_flush); |
| 330 | rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER); |
| 331 | OSMO_ASSERT(rc >= 0); |
| 332 | |
| 333 | rc = osmo_stats_reporter_enable(srep2); |
| 334 | OSMO_ASSERT(rc >= 0); |
| 335 | OSMO_ASSERT(srep2->force_single_flush); |
| 336 | rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER); |
| 337 | OSMO_ASSERT(rc >= 0); |
| 338 | |
| 339 | printf("report (initial):\n"); |
| 340 | send_count = 0; |
| 341 | osmo_stats_report(); |
| 342 | OSMO_ASSERT(send_count == 16); |
| 343 | |
| 344 | printf("report (srep1 global):\n"); |
| 345 | /* force single flush */ |
| 346 | osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL); |
| 347 | srep1->force_single_flush = 1; |
| 348 | srep2->force_single_flush = 1; |
| 349 | send_count = 0; |
| 350 | osmo_stats_report(); |
| 351 | OSMO_ASSERT(send_count == 8); |
| 352 | |
| 353 | printf("report (srep1 peer):\n"); |
| 354 | /* force single flush */ |
| 355 | osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER); |
| 356 | srep1->force_single_flush = 1; |
| 357 | srep2->force_single_flush = 1; |
| 358 | send_count = 0; |
| 359 | osmo_stats_report(); |
| 360 | OSMO_ASSERT(send_count == 12); |
| 361 | |
| 362 | printf("report (srep1 subscriber):\n"); |
| 363 | /* force single flush */ |
| 364 | osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER); |
| 365 | srep1->force_single_flush = 1; |
| 366 | srep2->force_single_flush = 1; |
| 367 | send_count = 0; |
| 368 | osmo_stats_report(); |
| 369 | OSMO_ASSERT(send_count == 16); |
| 370 | |
| 371 | printf("report (srep2 disabled):\n"); |
| 372 | /* force single flush */ |
| 373 | srep1->force_single_flush = 1; |
| 374 | srep2->force_single_flush = 1; |
| 375 | rc = osmo_stats_reporter_disable(srep2); |
| 376 | OSMO_ASSERT(rc >= 0); |
| 377 | send_count = 0; |
| 378 | osmo_stats_report(); |
| 379 | OSMO_ASSERT(send_count == 8); |
| 380 | |
| 381 | printf("report (srep2 enabled, no flush forced):\n"); |
| 382 | rc = osmo_stats_reporter_enable(srep2); |
| 383 | OSMO_ASSERT(rc >= 0); |
| 384 | send_count = 0; |
| 385 | osmo_stats_report(); |
| 386 | OSMO_ASSERT(send_count == 8); |
| 387 | |
| 388 | printf("report (should be empty):\n"); |
| 389 | send_count = 0; |
| 390 | osmo_stats_report(); |
| 391 | OSMO_ASSERT(send_count == 0); |
| 392 | |
| 393 | printf("report (group 1, counter 1 update):\n"); |
| 394 | rate_ctr_inc(&ctrg1->ctr[TEST_A_CTR]); |
| 395 | send_count = 0; |
| 396 | osmo_stats_report(); |
| 397 | OSMO_ASSERT(send_count == 2); |
| 398 | |
| 399 | printf("report (group 1, item 1 update):\n"); |
| 400 | osmo_stat_item_set(statg1->items[TEST_A_ITEM], 10); |
| 401 | send_count = 0; |
| 402 | osmo_stats_report(); |
| 403 | OSMO_ASSERT(send_count == 2); |
| 404 | |
| 405 | printf("report (remove statg1, ctrg1):\n"); |
| 406 | /* force single flush */ |
| 407 | srep1->force_single_flush = 1; |
| 408 | srep2->force_single_flush = 1; |
| 409 | osmo_stat_item_group_free(statg1); |
| 410 | rate_ctr_group_free(ctrg1); |
| 411 | send_count = 0; |
| 412 | osmo_stats_report(); |
| 413 | OSMO_ASSERT(send_count == 8); |
| 414 | |
| 415 | printf("report (remove srep1):\n"); |
| 416 | /* force single flush */ |
| 417 | srep1->force_single_flush = 1; |
| 418 | srep2->force_single_flush = 1; |
| 419 | osmo_stats_reporter_free(srep1); |
| 420 | send_count = 0; |
| 421 | osmo_stats_report(); |
| 422 | OSMO_ASSERT(send_count == 4); |
| 423 | |
| 424 | printf("report (remove statg2):\n"); |
| 425 | /* force single flush */ |
| 426 | srep2->force_single_flush = 1; |
| 427 | osmo_stat_item_group_free(statg2); |
| 428 | send_count = 0; |
| 429 | osmo_stats_report(); |
| 430 | OSMO_ASSERT(send_count == 2); |
| 431 | |
| 432 | printf("report (remove srep2):\n"); |
| 433 | /* force single flush */ |
| 434 | srep2->force_single_flush = 1; |
| 435 | osmo_stats_reporter_free(srep2); |
| 436 | send_count = 0; |
| 437 | osmo_stats_report(); |
| 438 | OSMO_ASSERT(send_count == 0); |
| 439 | |
| 440 | printf("report (remove ctrg2, should be empty):\n"); |
| 441 | rate_ctr_group_free(ctrg2); |
| 442 | send_count = 0; |
| 443 | osmo_stats_report(); |
| 444 | OSMO_ASSERT(send_count == 0); |
| 445 | |
Jacob Erlbeck | f13de86 | 2015-11-10 11:36:58 +0100 | [diff] [blame] | 446 | /* Leak check */ |
| 447 | OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1); |
| 448 | talloc_free(stats_ctx); |
| 449 | |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 450 | printf("End test: %s\n", __func__); |
| 451 | } |
| 452 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 453 | int main(int argc, char **argv) |
| 454 | { |
| 455 | static const struct log_info log_info = {}; |
| 456 | log_init(&log_info, NULL); |
| 457 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 458 | osmo_stat_item_init(NULL); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 459 | |
| 460 | stat_test(); |
Jacob Erlbeck | 46b703d | 2015-11-09 17:25:27 +0100 | [diff] [blame] | 461 | test_reporting(); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 462 | return 0; |
| 463 | } |