2 * collectd - src/utils_latency_test.c
3 * Copyright (C) 2015 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
27 #define DBL_PRECISION 1e-6
30 #include "common.h" /* for STATIC_ARRAY_SIZE */
33 #include "utils_latency.h"
34 #include "utils_time.h"
44 /* val min max sum avg */
45 {0.5, 0.5, 0.5, 0.5, 0.5}, {0.3, 0.3, 0.5, 0.8, 0.4},
46 {0.7, 0.3, 0.7, 1.5, 0.5}, {2.5, 0.3, 2.5, 4.0, 1.0},
47 {99, 0.3, 99, 103, 20.6},
48 /* { -1, 0.3, 99, 103, 20.6}, see issue #1139 */
52 CHECK_NOT_NULL(l = latency_counter_create());
54 for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
55 printf("# case %" PRIsz ": DOUBLE_TO_CDTIME_T(%g) = %" PRIu64 "\n", i,
56 cases[i].val, DOUBLE_TO_CDTIME_T(cases[i].val));
57 latency_counter_add(l, DOUBLE_TO_CDTIME_T(cases[i].val));
59 EXPECT_EQ_DOUBLE(cases[i].min,
60 CDTIME_T_TO_DOUBLE(latency_counter_get_min(l)));
61 EXPECT_EQ_DOUBLE(cases[i].max,
62 CDTIME_T_TO_DOUBLE(latency_counter_get_max(l)));
63 EXPECT_EQ_DOUBLE(cases[i].sum,
64 CDTIME_T_TO_DOUBLE(latency_counter_get_sum(l)));
65 EXPECT_EQ_DOUBLE(cases[i].avg,
66 CDTIME_T_TO_DOUBLE(latency_counter_get_average(l)));
69 latency_counter_destroy(l);
73 DEF_TEST(percentile) {
76 CHECK_NOT_NULL(l = latency_counter_create());
78 for (size_t i = 0; i < 100; i++) {
79 latency_counter_add(l, TIME_T_TO_CDTIME_T(((time_t)i) + 1));
82 EXPECT_EQ_DOUBLE(1.0, CDTIME_T_TO_DOUBLE(latency_counter_get_min(l)));
83 EXPECT_EQ_DOUBLE(100.0, CDTIME_T_TO_DOUBLE(latency_counter_get_max(l)));
84 EXPECT_EQ_DOUBLE(100.0 * 101.0 / 2.0,
85 CDTIME_T_TO_DOUBLE(latency_counter_get_sum(l)));
86 EXPECT_EQ_DOUBLE(50.5, CDTIME_T_TO_DOUBLE(latency_counter_get_average(l)));
88 EXPECT_EQ_DOUBLE(50.0,
89 CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(l, 50.0)));
90 EXPECT_EQ_DOUBLE(80.0,
91 CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(l, 80.0)));
92 EXPECT_EQ_DOUBLE(95.0,
93 CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(l, 95.0)));
94 EXPECT_EQ_DOUBLE(99.0,
95 CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(l, 99.0)));
97 CHECK_ZERO(latency_counter_get_percentile(l, -1.0));
98 CHECK_ZERO(latency_counter_get_percentile(l, 101.0));
100 latency_counter_destroy(l);
105 /* We re-declare the struct here so we can inspect its content. */
113 int histogram[HISTOGRAM_NUM_BINS];
115 latency_counter_t *l;
117 CHECK_NOT_NULL(l = latency_counter_create());
120 for (time_t i = 1; i <= 125; i++) {
121 latency_counter_add(l, TIME_T_TO_CDTIME_T(i));
124 /* We expect a bucket width of 125ms. */
125 EXPECT_EQ_UINT64(DOUBLE_TO_CDTIME_T(0.125), peek->bin_width);
131 {0, 0}, /* (0.000-0.125] */
132 {1, 0}, /* (0.125-0.250] */
133 {2, 0}, /* (0.250-0.375] */
134 {3, 0}, /* (0.375-0.500] */
135 {4, 0}, /* (0.500-0.625] */
136 {5, 0}, /* (0.625-0.750] */
137 {6, 0}, /* (0.750-0.875] */
138 {7, 1}, /* (0.875-1.000] */
139 {8, 0}, /* (1.000-1.125] */
140 {9, 0}, /* (1.125-1.250] */
141 {10, 0}, /* (1.250-1.375] */
142 {11, 0}, /* (1.375-1.500] */
143 {12, 0}, /* (1.500-1.625] */
144 {13, 0}, /* (1.625-1.750] */
145 {14, 0}, /* (1.750-1.875] */
146 {15, 1}, /* (1.875-2.000] */
147 {16, 0}, /* (2.000-2.125] */
150 for (size_t i = 0; i < STATIC_ARRAY_SIZE(bucket_cases); i++) {
151 size_t index = bucket_cases[i].index;
152 EXPECT_EQ_INT(bucket_cases[i].want, peek->histogram[index]);
156 cdtime_t lower_bound;
157 cdtime_t upper_bound;
162 DOUBLE_TO_CDTIME_T_STATIC(0.750), DOUBLE_TO_CDTIME_T_STATIC(0.875),
166 // bucket 7 contains the t=1 update
167 DOUBLE_TO_CDTIME_T_STATIC(0.875), DOUBLE_TO_CDTIME_T_STATIC(1.000),
171 // range: bucket 7 - bucket 15; contains the t=1 and t=2 updates
172 DOUBLE_TO_CDTIME_T_STATIC(0.875), DOUBLE_TO_CDTIME_T_STATIC(2.000),
176 // lower bucket is only partially applied
177 DOUBLE_TO_CDTIME_T_STATIC(0.875 + (0.125 / 4)),
178 DOUBLE_TO_CDTIME_T_STATIC(2.000), 1.75,
181 // upper bucket is only partially applied
182 DOUBLE_TO_CDTIME_T_STATIC(0.875),
183 DOUBLE_TO_CDTIME_T_STATIC(2.000 - (0.125 / 4)), 1.75,
186 // both buckets are only partially applied
187 DOUBLE_TO_CDTIME_T_STATIC(0.875 + (0.125 / 4)),
188 DOUBLE_TO_CDTIME_T_STATIC(2.000 - (0.125 / 4)), 1.50,
191 // lower bound is unspecified
192 0, DOUBLE_TO_CDTIME_T_STATIC(2.000), 2.00,
195 // upper bound is unspecified
196 DOUBLE_TO_CDTIME_T_STATIC(125.000 - 0.125), 0, 1.00,
199 // overflow test: upper >> longest latency
200 DOUBLE_TO_CDTIME_T_STATIC(1.000), DOUBLE_TO_CDTIME_T_STATIC(999999),
204 // overflow test: lower > longest latency
205 DOUBLE_TO_CDTIME_T_STATIC(130), 0, 0.00,
208 // lower > upper => error
209 DOUBLE_TO_CDTIME_T_STATIC(10), DOUBLE_TO_CDTIME_T_STATIC(9), NAN,
212 // lower == upper => zero
213 DOUBLE_TO_CDTIME_T_STATIC(9), DOUBLE_TO_CDTIME_T_STATIC(9), 0.00,
217 for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
218 cdtime_t now = peek->start_time + TIME_T_TO_CDTIME_T(1);
219 EXPECT_EQ_DOUBLE(cases[i].want,
220 latency_counter_get_rate(l, cases[i].lower_bound,
221 cases[i].upper_bound, now));
224 latency_counter_destroy(l);
230 RUN_TEST(percentile);