a0da51ea7a518329f743839c8b22be6605452b22
[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-9
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   size_t i;
54   latency_counter_t *l;
55
56   CHECK_NOT_NULL (l = latency_counter_create ());
57
58   for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
59     printf ("# case %zu: DOUBLE_TO_CDTIME_T(%g) = %"PRIu64"\n",
60         i, cases[i].val, DOUBLE_TO_CDTIME_T (cases[i].val));
61     latency_counter_add (l, DOUBLE_TO_CDTIME_T (cases[i].val));
62
63     EXPECT_EQ_DOUBLE (cases[i].min, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l)));
64     EXPECT_EQ_DOUBLE (cases[i].max, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l)));
65     EXPECT_EQ_DOUBLE (cases[i].sum, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l)));
66     EXPECT_EQ_DOUBLE (cases[i].avg, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l)));
67   }
68
69   latency_counter_destroy (l);
70   return 0;
71 }
72
73 DEF_TEST(percentile)
74 {
75   size_t i;
76   latency_counter_t *l;
77
78   CHECK_NOT_NULL (l = latency_counter_create ());
79
80   for (i = 0; i < 100; i++) {
81     latency_counter_add (l, TIME_T_TO_CDTIME_T (((time_t) i) + 1));
82   }
83
84   EXPECT_EQ_DOUBLE (  1.0, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l)));
85   EXPECT_EQ_DOUBLE (100.0, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l)));
86   EXPECT_EQ_DOUBLE (100.0 * 101.0 / 2.0, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l)));
87   EXPECT_EQ_DOUBLE ( 50.5, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l)));
88
89   EXPECT_EQ_DOUBLE (50.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 50.0)));
90   EXPECT_EQ_DOUBLE (80.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 80.0)));
91   EXPECT_EQ_DOUBLE (95.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 95.0)));
92   EXPECT_EQ_DOUBLE (99.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 99.0)));
93
94   CHECK_ZERO (latency_counter_get_percentile (l, -1.0));
95   CHECK_ZERO (latency_counter_get_percentile (l, 101.0));
96
97   latency_counter_destroy (l);
98   return 0;
99 }
100
101 int main (void)
102 {
103   RUN_TEST(simple);
104   RUN_TEST(percentile);
105
106   END_TEST;
107 }
108
109 /* vim: set sw=2 sts=2 et : */