src/utils_latency.[ch]: Improve accuracy, update unit test.
[collectd.git] / src / utils_latency_test.c
1 /**
2  * collectd - src/utils_latency_test.c
3  * Copyright (C) 2015       Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 #define DBL_PRECISION 1e-6
28
29 #include "common.h" /* for STATIC_ARRAY_SIZE */
30 #include "collectd.h"
31
32 #include "testing.h"
33 #include "utils_time.h"
34 #include "utils_latency.h"
35
36 DEF_TEST(simple)
37 {
38   struct {
39     double val;
40     double min;
41     double max;
42     double sum;
43     double avg;
44   } cases[] = {
45   /* val  min  max  sum   avg */
46     {0.5, 0.5, 0.5, 0.5,  0.5},
47     {0.3, 0.3, 0.5, 0.8,  0.4},
48     {0.7, 0.3, 0.7, 1.5,  0.5},
49     {2.5, 0.3, 2.5, 4.0,  1.0},
50     { 99, 0.3,  99, 103, 20.6},
51     /* { -1, 0.3,  99, 103, 20.6}, see issue #1139 */
52   };
53   latency_counter_t *l;
54
55   CHECK_NOT_NULL (l = latency_counter_create ());
56
57   for (size_t i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
58     printf ("# case %zu: DOUBLE_TO_CDTIME_T(%g) = %"PRIu64"\n",
59         i, cases[i].val, DOUBLE_TO_CDTIME_T (cases[i].val));
60     latency_counter_add (l, DOUBLE_TO_CDTIME_T (cases[i].val));
61
62     EXPECT_EQ_DOUBLE (cases[i].min, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l)));
63     EXPECT_EQ_DOUBLE (cases[i].max, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l)));
64     EXPECT_EQ_DOUBLE (cases[i].sum, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l)));
65     EXPECT_EQ_DOUBLE (cases[i].avg, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l)));
66   }
67
68   latency_counter_destroy (l);
69   return 0;
70 }
71
72 DEF_TEST(percentile)
73 {
74   latency_counter_t *l;
75
76   CHECK_NOT_NULL (l = latency_counter_create ());
77
78   for (size_t i = 0; i < 100; i++) {
79     latency_counter_add (l, TIME_T_TO_CDTIME_T (((time_t) i) + 1));
80   }
81
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, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l)));
85   EXPECT_EQ_DOUBLE ( 50.5, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l)));
86
87   EXPECT_EQ_DOUBLE (50.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 50.0)));
88   EXPECT_EQ_DOUBLE (80.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 80.0)));
89   EXPECT_EQ_DOUBLE (95.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 95.0)));
90   EXPECT_EQ_DOUBLE (99.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 99.0)));
91
92   CHECK_ZERO (latency_counter_get_percentile (l, -1.0));
93   CHECK_ZERO (latency_counter_get_percentile (l, 101.0));
94
95   latency_counter_destroy (l);
96   return 0;
97 }
98
99 DEF_TEST (get_rate) {
100   /* We re-declare the struct here so we can inspect its content. */
101   struct {
102     cdtime_t start_time;
103     cdtime_t sum;
104     size_t num;
105     cdtime_t min;
106     cdtime_t max;
107     cdtime_t bin_width;
108     int histogram[HISTOGRAM_NUM_BINS];
109   } *peek;
110   latency_counter_t *l;
111
112   CHECK_NOT_NULL (l = latency_counter_create ());
113   peek = (void *) l;
114
115   for (time_t i = 1; i <= 125; i++) {
116     latency_counter_add (l, TIME_T_TO_CDTIME_T(i));
117   }
118
119   /* We expect a bucket width of 125ms. */
120   EXPECT_EQ_UINT64 (DOUBLE_TO_CDTIME_T(0.125), peek->bin_width);
121
122   struct {
123     size_t index;
124     int want;
125   } bucket_cases[] = {
126     { 0, 0}, /* (0.000-0.125] */
127     { 1, 0}, /* (0.125-0.250] */
128     { 2, 0}, /* (0.250-0.375] */
129     { 3, 0}, /* (0.375-0.500] */
130     { 4, 0}, /* (0.500-0.625] */
131     { 5, 0}, /* (0.625-0.750] */
132     { 6, 0}, /* (0.750-0.875] */
133     { 7, 1}, /* (0.875-1.000] */
134     { 8, 0}, /* (1.000-1.125] */
135     { 9, 0}, /* (1.125-1.250] */
136     {10, 0}, /* (1.250-1.375] */
137     {11, 0}, /* (1.375-1.500] */
138     {12, 0}, /* (1.500-1.625] */
139     {13, 0}, /* (1.625-1.750] */
140     {14, 0}, /* (1.750-1.875] */
141     {15, 1}, /* (1.875-2.000] */
142     {16, 0}, /* (2.000-2.125] */
143   };
144
145   for (size_t i = 0; i < STATIC_ARRAY_SIZE(bucket_cases); i++) {
146     size_t index = bucket_cases[i].index;
147     EXPECT_EQ_INT(bucket_cases[i].want, peek->histogram[index]);
148   }
149
150   struct {
151     cdtime_t lower_bound;
152     cdtime_t upper_bound;
153     double want;
154   } cases[] = {
155     { // bucket 6 is zero
156       DOUBLE_TO_CDTIME_T(0.750),
157       DOUBLE_TO_CDTIME_T(0.875),
158       0.00,
159     },
160     { // bucket 7 contains the t=1 update
161       DOUBLE_TO_CDTIME_T(0.875),
162       DOUBLE_TO_CDTIME_T(1.000),
163       1.00,
164     },
165     { // range: bucket 7 - bucket 15; contains the t=1 and t=2 updates
166       DOUBLE_TO_CDTIME_T(0.875),
167       DOUBLE_TO_CDTIME_T(2.000),
168       2.00,
169     },
170     { // lower bucket is only partially applied
171       DOUBLE_TO_CDTIME_T(0.875 + (0.125 / 4)),
172       DOUBLE_TO_CDTIME_T(2.000),
173       1.75,
174     },
175     { // upper bucket is only partially applied
176       DOUBLE_TO_CDTIME_T(0.875),
177       DOUBLE_TO_CDTIME_T(2.000 - (0.125 / 4)),
178       1.75,
179     },
180     { // both buckets are only partially applied
181       DOUBLE_TO_CDTIME_T(0.875 + (0.125 / 4)),
182       DOUBLE_TO_CDTIME_T(2.000 - (0.125 / 4)),
183       1.50,
184     },
185     { // lower bound is unspecified
186       0,
187       DOUBLE_TO_CDTIME_T(2.000),
188       2.00,
189     },
190     { // upper bound is unspecified
191       DOUBLE_TO_CDTIME_T(125.000 - 0.125),
192       0,
193       1.00,
194     },
195     { // overflow test
196       DOUBLE_TO_CDTIME_T(1.000),
197       DOUBLE_TO_CDTIME_T(999999),
198       124.00,
199     },
200   };
201
202   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
203     cdtime_t now = peek->start_time + TIME_T_TO_CDTIME_T(1);
204     EXPECT_EQ_DOUBLE (cases[i].want,
205                       latency_counter_get_rate (l, cases[i].lower_bound, cases[i].upper_bound, now));
206   }
207
208   latency_counter_destroy (l);
209   return 0;
210 }
211
212 int main (void)
213 {
214   RUN_TEST(simple);
215   RUN_TEST(percentile);
216   RUN_TEST(get_rate);
217
218   END_TEST;
219 }
220
221 /* vim: set sw=2 sts=2 et : */