1 // Copyright 2018 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6
7 #include <lib/unittest/unittest.h>
8
9 #include "counters_private.h"
10
value_cleanup()11 static bool value_cleanup() {
12 BEGIN_TEST;
13
14 uint64_t outputs[SMP_MAX_CPUS];
15 size_t out_count;
16
17 // Sorted.
18 uint64_t inputs0[SMP_MAX_CPUS] = {13, 4, 8, 9};
19 counters_clean_up_values(inputs0, outputs, &out_count);
20 ASSERT_EQ(out_count, 4u, "");
21 EXPECT_EQ(outputs[0], 4u, "");
22 EXPECT_EQ(outputs[1], 8u, "");
23 EXPECT_EQ(outputs[2], 9u, "");
24 EXPECT_EQ(outputs[3], 13u, "");
25
26 // Collapsed to remove zeros.
27 uint64_t inputs1[SMP_MAX_CPUS] = {13, 0, 0, 9};
28 counters_clean_up_values(inputs1, outputs, &out_count);
29 ASSERT_EQ(out_count, 2u, "");
30 EXPECT_EQ(outputs[0], 9u, "");
31 EXPECT_EQ(outputs[1], 13u, "");
32
33 END_TEST;
34 }
35
36 // Data to compare vs. results in
37 // https://docs.google.com/spreadsheets/d/1D58chwOpO-3_c41NMGJkpmFuOpGSYH-W50bD6MdOAjo/edit?usp=sharing
38 static uint64_t test_counters_inputs0[SMP_MAX_CPUS] = {5105, 4602, 4031, 4866};
39 static uint64_t test_counters_inputs1[SMP_MAX_CPUS] = {3524, 3461, 3567, 2866};
40
percentile_determination()41 static bool percentile_determination() {
42 BEGIN_TEST;
43
44 uint64_t cleaned[SMP_MAX_CPUS];
45 size_t out_count;
46
47 counters_clean_up_values(test_counters_inputs0, cleaned, &out_count);
48 EXPECT_EQ(counters_get_percentile(cleaned, out_count, /*0.25*/ 64),
49 /* 4459.25 */ (4459u << 8) + 64u, "");
50 EXPECT_EQ(counters_get_percentile(cleaned, out_count, /*0.75*/ 192),
51 /* 4925.75 */ (4925u << 8) + 192u, "");
52
53 counters_clean_up_values(test_counters_inputs1, cleaned, &out_count);
54 EXPECT_EQ(counters_get_percentile(cleaned, out_count, /*0.25*/ 64),
55 /* 3312.25 */ (3312u << 8) + 64u, "");
56 EXPECT_EQ(counters_get_percentile(cleaned, out_count, /*0.75*/ 192),
57 /* 3534.75 */ (3534u << 8) + 192u, "");
58
59 END_TEST;
60 }
61
outlier_check()62 static bool outlier_check() {
63 BEGIN_TEST;
64
65 uint64_t no_values[SMP_MAX_CPUS] = {0};
66 EXPECT_FALSE(counters_has_outlier(no_values), "0 values shouldn't have outlier");
67
68 uint64_t one_value[SMP_MAX_CPUS] = {789};
69 EXPECT_FALSE(counters_has_outlier(one_value), "1 value shouldn't have outlier");
70
71 EXPECT_FALSE(counters_has_outlier(test_counters_inputs0), "");
72 EXPECT_TRUE(counters_has_outlier(test_counters_inputs1), "");
73
74 END_TEST;
75 }
76
77 UNITTEST_START_TESTCASE(counters_tests)
78 UNITTEST("value cleanup", value_cleanup)
79 UNITTEST("percentile determination", percentile_determination)
80 UNITTEST("outlier check", outlier_check)
81 UNITTEST_END_TESTCASE(counters_tests, "counters_tests", "Counters tests");
82