Debian Codegraph @

benchmark/lib/bench-reporter.c

raw symbols SCIP
1 /*
2 Copyright (C) 2008-2021 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <string.h>
20
21 #include "bench-reporter.h"
22
23 typedef struct _BenchItem BenchItem;
24 struct _BenchItem
25 {
26 gchar *label;
27 gint n;
28 BenchSetupFunc bench_setup;
29 BenchFunc bench;
30 BenchTeardownFunc bench_teardown;
31 gpointer data;
32 };
33
34 static BenchItem *
35 bench_item_new(const gchar *label, gint n,
36 BenchSetupFunc bench_setup,
37 BenchFunc bench,
38 BenchTeardownFunc bench_teardown,
39 gpointer data)
40 {
41 BenchItem *item;
42
43 item = g_slice_new(BenchItem);
44
45 item->label = g_strdup(label);
46 item->n = n;
47 item->bench_setup = bench_setup;
48 item->bench = bench;
49 item->bench_teardown = bench_teardown;
50 item->data = data;
51
52 return item;
53 }
54
55 static void
56 bench_item_free(BenchItem *item)
57 {
58 if (item->label)
59 g_free(item->label);
60
61 g_slice_free(BenchItem, item);
62 }
63
64 #define BENCH_REPORTER_GET_PRIVATE(obj) \
65 bench_reporter_get_instance_private(BENCH_REPORTER(obj))
66
67 typedef struct _BenchReporterPrivate BenchReporterPrivate;
68 struct _BenchReporterPrivate
69 {
70 GList *items;
71 };
72
73 G_DEFINE_TYPE_WITH_PRIVATE(BenchReporter, bench_reporter, G_TYPE_OBJECT)
74
75 static void dispose (GObject *object);
76
77 static void
78 bench_reporter_class_init(BenchReporterClass *klass)
79 {
80 GObjectClass *gobject_class;
81
82 gobject_class = G_OBJECT_CLASS(klass);
83
84 gobject_class->dispose = dispose;
85 }
86
87 static void
88 bench_reporter_init(BenchReporter *reporter)
89 {
90 BenchReporterPrivate *priv;
91
92 priv = BENCH_REPORTER_GET_PRIVATE(reporter);
93
94 priv->items = NULL;
95 }
96
97 static void
98 dispose(GObject *object)
99 {
100 BenchReporterPrivate *priv;
101
102 priv = BENCH_REPORTER_GET_PRIVATE(object);
103
104 if (priv->items) {
105 g_list_foreach(priv->items, (GFunc)bench_item_free, NULL);
106 g_list_free(priv->items);
107 priv->items = NULL;
108 }
109
110 G_OBJECT_CLASS(bench_reporter_parent_class)->dispose(object);
111 }
112
113 BenchReporter *
114 bench_reporter_new(void)
115 {
116 return g_object_new(BENCH_TYPE_REPORTER, NULL);
117 }
118
119 void
120 bench_reporter_register(BenchReporter *reporter,
121 const gchar *label, gint n,
122 BenchSetupFunc bench_setup,
123 BenchFunc bench,
124 BenchTeardownFunc bench_teardown,
125 gpointer data)
126 {
127 BenchReporterPrivate *priv;
128
129 priv = BENCH_REPORTER_GET_PRIVATE(reporter);
130
131 priv->items = g_list_append(priv->items, bench_item_new(label, n,
132 bench_setup,
133 bench,
134 bench_teardown,
135 data));
136 }
137
138 #define INDENT " "
139
140 static void
141 print_header(BenchReporterPrivate *priv, gint max_label_length)
142 {
143 gint n_spaces;
144
145 g_print(INDENT);
146 for (n_spaces = max_label_length + strlen(": ");
147 n_spaces > 0;
148 n_spaces--) {
149 g_print(" ");
150 }
151 g_print("(total) ");
152 g_print("(average) ");
153 g_print("(median)\n");
154 }
155
156 static void
157 print_label(BenchReporterPrivate *priv, BenchItem *item, gint max_label_length)
158 {
159 gint n_left_spaces;
160
161 g_print(INDENT);
162 if (item->label) {
163 n_left_spaces = max_label_length - strlen(item->label);
164 } else {
165 n_left_spaces = max_label_length;
166 }
167 for (; n_left_spaces > 0; n_left_spaces--) {
168 g_print(" ");
169 }
170 if (item->label)
171 g_print("%s", item->label);
172 g_print(": ");
173 }
174
175 static void
176 report_elapsed_time(gdouble elapsed_time)
177 {
178 gdouble one_second = 1.0;
179 gdouble one_millisecond = one_second / 1000.0;
180 gdouble one_microsecond = one_millisecond / 1000.0;
181
182 if (elapsed_time < one_microsecond) {
183 g_print("(%.8fms)", elapsed_time * 1000.0);
184 } else if (elapsed_time < one_millisecond) {
185 g_print("(%.4fms)", elapsed_time * 1000.0);
186 } else {
187 g_print("(%.4fs) ", elapsed_time);
188 }
189 }
190
191 static gdouble
192 compute_total_elapsed_time(GArray *elapsed_times)
193 {
194 guint i;
195 gdouble total = 0.0;
196
197 for (i = 0; i< elapsed_times->len; i++) {
198 gdouble elapsed_time = g_array_index(elapsed_times, gdouble, i);
199 total += elapsed_time;
200 }
201
202 return total;
203 }
204
205 static void
206 report_elapsed_time_total(GArray *elapsed_times)
207 {
208 report_elapsed_time(compute_total_elapsed_time(elapsed_times));
209 }
210
211 static void
212 report_elapsed_time_average(GArray *elapsed_times)
213 {
214 gdouble total;
215 gdouble average;
216
217 total = compute_total_elapsed_time(elapsed_times);
218 average = total / elapsed_times->len;
219 report_elapsed_time(average);
220 }
221
222 static gint
223 compare_elapsed_time(gconstpointer a, gconstpointer b)
224 {
225 const gdouble *elapsed_time1 = a;
226 const gdouble *elapsed_time2 = b;
227
228 if (*elapsed_time1 > *elapsed_time2) {
229 return 1;
230 } else if (*elapsed_time1 < *elapsed_time2) {
231 return -1;
232 } else {
233 return 0;
234 }
235 }
236
237 static void
238 report_elapsed_time_median(GArray *elapsed_times)
239 {
240 gdouble median;
241
242 g_array_sort(elapsed_times, compare_elapsed_time);
243 median = g_array_index(elapsed_times, gdouble, elapsed_times->len / 2);
244 report_elapsed_time(median);
245 }
246
247 static void
248 report_elapsed_time_statistics(GArray *elapsed_times)
249 {
250 report_elapsed_time_total(elapsed_times);
251 g_print(" ");
252 report_elapsed_time_average(elapsed_times);
253 g_print(" ");
254 report_elapsed_time_median(elapsed_times);
255 g_print("\n");
256 }
257
258 static void
259 run_item(BenchReporterPrivate *priv, BenchItem *item, gint max_label_length)
260 {
261 GTimer *timer;
262 GArray *elapsed_times;
263 gint i;
264
265 print_label(priv, item, max_label_length);
266
267 elapsed_times = g_array_new(FALSE, FALSE, sizeof(gdouble));
268
269 timer = g_timer_new();
270 for (i = 0; i < item->n; i++) {
271 gdouble elapsed_time;
272 if (item->bench_setup)
273 item->bench_setup(item->data);
274 g_timer_start(timer);
275 item->bench(item->data);
276 g_timer_stop(timer);
277 elapsed_time = g_timer_elapsed(timer, NULL);
278 g_array_append_val(elapsed_times, elapsed_time);
279 if (item->bench_teardown)
280 item->bench_teardown(item->data);
281 }
282 g_timer_destroy(timer);
283
284 report_elapsed_time_statistics(elapsed_times);
285
286 g_array_free(elapsed_times, TRUE);
287
288 }
289
290 void
291 bench_reporter_run(BenchReporter *reporter)
292 {
293 BenchReporterPrivate *priv;
294 GList *node;
295 size_t max_label_length = 0;
296
297 priv = BENCH_REPORTER_GET_PRIVATE(reporter);
298 for (node = priv->items; node; node = g_list_next(node)) {
299 BenchItem *item = node->data;
300
301 if (item->label)
302 max_label_length = MAX(max_label_length, strlen(item->label));
303 }
304
305 print_header(priv, max_label_length);
306 for (node = priv->items; node; node = g_list_next(node)) {
307 BenchItem *item = node->data;
308
309 run_item(priv, item, max_label_length);
310 }
311 }