9aecf38c1ab24fe473bd46b2439fd6b701304037
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 */
27 #define DBL_PRECISION 1e-9
29 #include "testing.h"
30 #include "collectd.h"
31 #include "common.h" /* for STATIC_ARRAY_SIZE */
32 #include "utils_time.h"
33 #include "utils_latency.h"
35 DEF_TEST(simple)
36 {
37 struct {
38 double val;
39 double min;
40 double max;
41 double sum;
42 double avg;
43 } cases[] = {
44 /* val min max sum avg */
45 {0.5, 0.5, 0.5, 0.5, 0.5},
46 {0.3, 0.3, 0.5, 0.8, 0.4},
47 {0.7, 0.3, 0.7, 1.5, 0.5},
48 {2.5, 0.3, 2.5, 4.0, 1.0},
49 { -1, 0.3, 2.5, 4.0, 1.0},
50 };
51 size_t i;
52 latency_counter_t *l;
54 CHECK_NOT_NULL (l = latency_counter_create ());
56 for (i = 0; i < STATIC_ARRAY_SIZE (cases); i++) {
57 latency_counter_add (l, DOUBLE_TO_CDTIME_T (cases[i].val));
59 DBLEQ (cases[i].min, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l)));
60 DBLEQ (cases[i].max, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l)));
61 DBLEQ (cases[i].sum, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l)));
62 DBLEQ (cases[i].avg, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l)));
63 }
65 latency_counter_destroy (l);
66 return 0;
67 }
69 DEF_TEST(percentile)
70 {
71 size_t i;
72 latency_counter_t *l;
74 CHECK_NOT_NULL (l = latency_counter_create ());
76 for (i = 0; i < 100; i++) {
77 latency_counter_add (l, TIME_T_TO_CDTIME_T (((time_t) i) + 1));
78 }
80 DBLEQ ( 1.0, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l)));
81 DBLEQ (100.0, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l)));
82 DBLEQ (100.0 * 101.0 / 2.0, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l)));
83 DBLEQ ( 50.5, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l)));
85 DBLEQ (50.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 50.0)));
86 DBLEQ (80.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 80.0)));
87 DBLEQ (95.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 95.0)));
88 DBLEQ (99.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 99.0)));
90 CHECK_ZERO (latency_counter_get_percentile (l, -1.0));
91 CHECK_ZERO (latency_counter_get_percentile (l, 101.0));
93 latency_counter_destroy (l);
94 return 0;
95 }
97 int main (void)
98 {
99 RUN_TEST(simple);
100 RUN_TEST(percentile);
102 END_TEST;
103 }
105 /* vim: set sw=2 sts=2 et : */