aa0e7ef5fce6d9d4baace518085f4c02ec38ab69
[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 int total_logs;
9 int metrics_get_total(const struct metrics *m)
10 {
11         return total_logs;
12 }
13
14 void test_proportional_raw()
15 {
16         struct metrics_pid_aggr_info i1 [] =
17                 {{ .count =  8, }
18                 ,{ .count = 12, }
19                 ,{ .count =  4, }
20                 ,{ .count = 16, }
21         };
22         total_logs = 8 + 12 + 4 + 16;
23
24         qos_distribution_proportional_raw(&(struct qos_module) { .max_throughput = 200 }, i1, 4);
25         assert(i1[0].count == 40);
26         assert(i1[1].count == 60);
27         assert(i1[2].count == 20);
28         assert(i1[3].count == 80);
29 }
30
31 void test_equal()
32 {
33         struct metrics_pid_aggr_info i1 [] =
34                 {{ .count =  1, }
35                 ,{ .count = 66, }
36                 ,{ .count = 17, }
37                 ,{ .count = 42, }
38         };
39         total_logs = 1 + 66 + 17 + 42;
40
41         qos_distribution_equal(&(struct qos_module) { .max_throughput = 200 }, i1, 4);
42         assert(i1[0].count == 50);
43         assert(i1[1].count == 50);
44         assert(i1[2].count == 50);
45         assert(i1[3].count == 50);
46 }
47
48 void test_equal_dual()
49 {
50         struct metrics_pid_aggr_info i1 [] =
51                 {{ .count =  20, }
52                 ,{ .count =  85, }
53                 ,{ .count = 120, }
54         };
55         total_logs = 20 + 85 + 120;
56
57         qos_distribution_equal_dual(&(struct qos_module) { .max_throughput = 200 }, i1, 3);
58         assert(i1[0].count == -1); //  20 <  200/3
59         assert(i1[1].count == 90); //  85 >= 200/3, 90 == (200-20)/2
60         assert(i1[2].count == 90); // 120 >= 200/3, 90 == (200-20)/2
61 }
62
63 void test_equal_multi()
64 {
65         struct metrics_pid_aggr_info i1 [] =
66                 {{ .count =  30, }
67                 ,{ .count =  95, }
68                 ,{ .count = 120, }
69         };
70         total_logs = 30 + 95 + 120;
71
72         qos_distribution_equal_multi(&(struct qos_module) { .max_throughput = 200 }, i1, 3);
73         assert(i1[0].count == -1); //  30 <  200/3
74         assert(i1[1].count == 85); //  95 >= 200/3, and  95 >= (200-30)/2
75         assert(i1[2].count == 85); // 120 >= 200/3, and 120 >= (200-30)/2
76
77
78         struct metrics_pid_aggr_info i2 [] =
79                 {{ .count =  20, }
80                 ,{ .count =  85, }
81                 ,{ .count = 120, }
82         };
83         total_logs = 20 + 85 + 120;
84
85         qos_distribution_equal_multi(&(struct qos_module) { .max_throughput = 200 }, i2, 3);
86         assert(i2[0].count == -1); //  20 <  200/3
87         assert(i2[1].count == -1); //  85 >= 200/3, but  85 <  (200-20)/2
88         assert(i2[2].count == 95); // 120 >= 200/3, and 120 >= (200-20)/2, and 120 >= (200-20-85)/1
89 }
90
91 void test_proportional_talmud()
92 {
93         struct metrics_pid_aggr_info i1 [] =
94                 {{ .count =  50, }
95                 ,{ .count = 100, }
96         };
97         total_logs = 50 + 100;
98
99         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 100 }, i1, 2);
100         assert(i1[0].count == 25);
101         assert(i1[1].count == 75);
102
103         struct metrics_pid_aggr_info i2 [] =
104                 {{ .count = 100, }
105                 ,{ .count = 200, }
106                 ,{ .count = 300, }
107         };
108         total_logs = 100 + 200 + 300;
109
110         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 100 }, i2, 3);
111         assert(i2[0].count == 33);
112         assert(i2[1].count == 33);
113         assert(i2[2].count == 33);
114
115         struct metrics_pid_aggr_info i3 [] =
116                 {{ .count = 100, }
117                 ,{ .count = 200, }
118                 ,{ .count = 300, }
119         };
120         total_logs = 100 + 200 + 300;
121
122         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 200 }, i3, 3);
123         assert(i3[0].count == 50);
124         assert(i3[1].count == 75);
125         assert(i3[2].count == 75);
126
127         struct metrics_pid_aggr_info i4 [] =
128                 {{ .count = 100, }
129                 ,{ .count = 200, }
130                 ,{ .count = 300, }
131         };
132         total_logs = 100 + 200 + 300;
133
134         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 300 }, i4, 3);
135         assert(i4[0].count == 50);
136         assert(i4[1].count == 100);
137         assert(i4[2].count == 150);
138
139         struct metrics_pid_aggr_info i5 [] =
140                 {{ .count = 100, }
141                 ,{ .count = 200, }
142                 ,{ .count = 300, }
143         };
144         total_logs = 100 + 200 + 300;
145
146         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 400 }, i5, 3);
147         assert(i5[0].count == 50);
148         assert(i5[1].count == 125);
149         assert(i5[2].count == 225);
150
151         struct metrics_pid_aggr_info i6 [] =
152                 {{ .count = 10, }
153                 ,{ .count = 20, }
154                 ,{ .count = 30, }
155         };
156         total_logs = 10 + 20 + 30;
157
158         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 100 }, i6, 3);
159         assert(i6[0].count == 10);
160         assert(i6[1].count == 20);
161         assert(i6[2].count == 30);
162
163         struct metrics_pid_aggr_info i7 [] =
164                 {{ .count = 100, }
165                 ,{ .count = 200, }
166                 ,{ .count = 300, }
167         };
168         total_logs = 100 + 200 + 300;
169
170         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 500 }, i7, 3);
171         assert(i7[0].count == 67);
172         assert(i7[1].count == 167);
173         assert(i7[2].count == 267);
174
175         struct metrics_pid_aggr_info i8 [] =
176                 {{ .count = 100, }
177                 ,{ .count = 200, }
178                 ,{ .count = 300, }
179                 ,{ .count = 400, }
180         };
181         total_logs = 100 + 200 + 300 + 400;
182
183         qos_distribution_proportional_talmud(&(struct qos_module) { .max_throughput = 700 }, i8, 4);
184         assert(i8[0].count == 50);
185         assert(i8[1].count == 117);
186         assert(i8[2].count == 217);
187         assert(i8[3].count == 317);
188 }
189
190 int main()
191 {
192         test_proportional_raw();
193         test_equal();
194         test_equal_dual();
195         test_equal_multi();
196         test_proportional_talmud();
197 }