dlog_logger: refactor QoS distributions
[platform/core/system/dlog.git] / src / tests / qos_distributions.c
1 #include <assert.h>
2 #include "../logger/qos.h"
3
4 /* Some proportional modes look at the cached total log count, which
5  * is cached in the metrics struct which we don't want to fill normally,
6  * so we just implement this function instead of linking to the actual
7  * metrics implementation. */
8 static int total_logs;
9 int metrics_get_total(const struct metrics *m)
10 {
11         return total_logs;
12 }
13
14 void test_proportional_raw(void)
15 {
16         const qos_distribution_func qos_distribution_proportional_raw = qos_get_distribution_func_by_name("proportional_raw");
17         assert(qos_distribution_proportional_raw);
18
19         struct metrics_pid_aggr_info i1 [] =
20                 {{ .count =  8, }
21                 ,{ .count = 12, }
22                 ,{ .count =  4, }
23                 ,{ .count = 16, }
24         };
25         total_logs = 8 + 12 + 4 + 16;
26
27         qos_distribution_proportional_raw(&(struct qos_module) { .max_throughput = 200 }, i1, 4);
28         assert(i1[0].count == 40);
29         assert(i1[1].count == 60);
30         assert(i1[2].count == 20);
31         assert(i1[3].count == 80);
32 }
33
34 void test_equal(void)
35 {
36         const qos_distribution_func qos_distribution_equal = qos_get_distribution_func_by_name("equal");
37         assert(qos_distribution_equal);
38
39         struct metrics_pid_aggr_info i1 [] =
40                 {{ .count =  1, }
41                 ,{ .count = 66, }
42                 ,{ .count = 17, }
43                 ,{ .count = 42, }
44         };
45         total_logs = 1 + 66 + 17 + 42;
46
47         qos_distribution_equal(&(struct qos_module) { .max_throughput = 200 }, i1, 4);
48         assert(i1[0].count == 50);
49         assert(i1[1].count == 50);
50         assert(i1[2].count == 50);
51         assert(i1[3].count == 50);
52 }
53
54 void test_equal_dual(void)
55 {
56         const qos_distribution_func qos_distribution_equal_dual = qos_get_distribution_func_by_name("equal_dual");
57         assert(qos_distribution_equal_dual);
58
59         struct metrics_pid_aggr_info i1 [] =
60                 {{ .count =  20, }
61                 ,{ .count =  85, }
62                 ,{ .count = 120, }
63         };
64         total_logs = 20 + 85 + 120;
65
66         qos_distribution_equal_dual(&(struct qos_module) { .max_throughput = 200 }, i1, 3);
67         assert(i1[0].count == -1); //  20 <  200/3
68         assert(i1[1].count == 90); //  85 >= 200/3, 90 == (200-20)/2
69         assert(i1[2].count == 90); // 120 >= 200/3, 90 == (200-20)/2
70 }
71
72 void test_equal_multi(void)
73 {
74         const qos_distribution_func qos_distribution_equal_multi = qos_get_distribution_func_by_name("equal_multi");
75         assert(qos_distribution_equal_multi);
76
77         struct metrics_pid_aggr_info i1 [] =
78                 {{ .count =  30, }
79                 ,{ .count =  95, }
80                 ,{ .count = 120, }
81         };
82         total_logs = 30 + 95 + 120;
83
84         qos_distribution_equal_multi(&(struct qos_module) { .max_throughput = 200 }, i1, 3);
85         assert(i1[0].count == -1); //  30 <  200/3
86         assert(i1[1].count == 85); //  95 >= 200/3, and  95 >= (200-30)/2
87         assert(i1[2].count == 85); // 120 >= 200/3, and 120 >= (200-30)/2
88
89
90         struct metrics_pid_aggr_info i2 [] =
91                 {{ .count =  20, }
92                 ,{ .count =  85, }
93                 ,{ .count = 120, }
94         };
95         total_logs = 20 + 85 + 120;
96
97         qos_distribution_equal_multi(&(struct qos_module) { .max_throughput = 200 }, i2, 3);
98         assert(i2[0].count == -1); //  20 <  200/3
99         assert(i2[1].count == -1); //  85 >= 200/3, but  85 <  (200-20)/2
100         assert(i2[2].count == 95); // 120 >= 200/3, and 120 >= (200-20)/2, and 120 >= (200-20-85)/1
101 }
102
103 void test_proportional_talmud(void)
104 {
105         const qos_distribution_func qos_distribution_proportional_talmud = qos_get_distribution_func_by_name("proportional_talmud");
106         assert(qos_distribution_proportional_talmud);
107
108         struct metrics_pid_aggr_info i1 [] =
109                 {{ .count =  50, }
110                 ,{ .count = 100, }
111         };
112         total_logs = 50 + 100;
113
114         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 100 }, i1, 2);
115         assert(i1[0].count == 25);
116         assert(i1[1].count == 75);
117
118         struct metrics_pid_aggr_info i2 [] =
119                 {{ .count = 100, }
120                 ,{ .count = 200, }
121                 ,{ .count = 300, }
122         };
123         total_logs = 100 + 200 + 300;
124
125         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 100 }, i2, 3);
126         assert(i2[0].count == 33);
127         assert(i2[1].count == 33);
128         assert(i2[2].count == 33);
129
130         struct metrics_pid_aggr_info i3 [] =
131                 {{ .count = 100, }
132                 ,{ .count = 200, }
133                 ,{ .count = 300, }
134         };
135         total_logs = 100 + 200 + 300;
136
137         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 200 }, i3, 3);
138         assert(i3[0].count == 50);
139         assert(i3[1].count == 75);
140         assert(i3[2].count == 75);
141
142         struct metrics_pid_aggr_info i4 [] =
143                 {{ .count = 100, }
144                 ,{ .count = 200, }
145                 ,{ .count = 300, }
146         };
147         total_logs = 100 + 200 + 300;
148
149         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 300 }, i4, 3);
150         assert(i4[0].count == 50);
151         assert(i4[1].count == 100);
152         assert(i4[2].count == 150);
153
154         struct metrics_pid_aggr_info i5 [] =
155                 {{ .count = 100, }
156                 ,{ .count = 200, }
157                 ,{ .count = 300, }
158         };
159         total_logs = 100 + 200 + 300;
160
161         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 400 }, i5, 3);
162         assert(i5[0].count == 50);
163         assert(i5[1].count == 125);
164         assert(i5[2].count == 225);
165
166         struct metrics_pid_aggr_info i6 [] =
167                 {{ .count = 10, }
168                 ,{ .count = 20, }
169                 ,{ .count = 30, }
170         };
171         total_logs = 10 + 20 + 30;
172
173         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 100 }, i6, 3);
174         assert(i6[0].count == 10);
175         assert(i6[1].count == 20);
176         assert(i6[2].count == 30);
177
178         struct metrics_pid_aggr_info i7 [] =
179                 {{ .count = 100, }
180                 ,{ .count = 200, }
181                 ,{ .count = 300, }
182         };
183         total_logs = 100 + 200 + 300;
184
185         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 500 }, i7, 3);
186         assert(i7[0].count == 67);
187         assert(i7[1].count == 167);
188         assert(i7[2].count == 267);
189
190         struct metrics_pid_aggr_info i8 [] =
191                 {{ .count = 100, }
192                 ,{ .count = 200, }
193                 ,{ .count = 300, }
194                 ,{ .count = 400, }
195         };
196         total_logs = 100 + 200 + 300 + 400;
197
198         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 700 }, i8, 4);
199         assert(i8[0].count == 50);
200         assert(i8[1].count == 117);
201         assert(i8[2].count == 217);
202         assert(i8[3].count == 317);
203 }
204
205 int main(void)
206 {
207         test_proportional_raw();
208         test_equal();
209         test_equal_dual();
210         test_equal_multi();
211         test_proportional_talmud();
212 }