dee

Merge lp:~kamstrup/dee/benchmark into lp:dee

Proposed by Mikkel Kamstrup Erlandsen
Status: Merged
Approved by: Michal Hruby
Approved revision: 318
Merged at revision: 317
Proposed branch: lp:~kamstrup/dee/benchmark
Merge into: lp:dee
Diff against target: 310 lines (+217/-20)
3 files modified
Makefile.am (+3/-0)
tests/Makefile.am (+22/-20)
tests/test-benchmark.c (+192/-0)
To merge this branch: bzr merge lp:~kamstrup/dee/benchmark
Reviewer Review Type Date Requested Status
Michal Hruby (community) Approve
Review via email: mp+84785@code.launchpad.net

Description of the change

Add a small simple benchmark suite

This branch adds a new make target 'benchmark' or stand alone executable
to the test suite tests/test-benchmark.

It produces some simple statistical output and it's easy to add new
benchmarks in there.

To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

Awesome!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile.am'
2--- Makefile.am 2011-11-24 21:36:17 +0000
3+++ Makefile.am 2011-12-07 15:49:34 +0000
4@@ -20,3 +20,6 @@
5 COPYING.GPL
6
7 DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc --enable-introspection=no
8+
9+benchmark:
10+ cd tests && make benchmark
11
12=== modified file 'tests/Makefile.am'
13--- tests/Makefile.am 2011-11-29 16:36:04 +0000
14+++ tests/Makefile.am 2011-12-07 15:49:34 +0000
15@@ -1,5 +1,24 @@
16 noinst_PROGRAMS = \
17- test-dee
18+ test-dee \
19+ test-benchmark
20+
21+AM_CPPFLAGS = \
22+ -I$(top_srcdir) \
23+ -I$(top_srcdir)/dee \
24+ -I$(top_srcdir)/dee \
25+ -DTESTDIR=\""$(top_builddir)/tests"\" \
26+ -DDEE_COMPILATION \
27+ $(GCC_FLAGS) \
28+ $(DEE_CFLAGS) \
29+ $(MAINTAINER_CFLAGS)
30+
31+test_benchmark_SOURCES = \
32+ test-benchmark.c
33+
34+test_benchmark_LDADD = $(top_builddir)/dee/libdee-1.0.la $(DEE_LIBS) -lm
35+
36+benchmark: test-benchmark
37+ ./test-benchmark
38
39 test_dee_SOURCES = \
40 test-dee.c \
41@@ -19,22 +38,12 @@
42 test-resource-manager.c \
43 $(top_srcdir)/dee/dee-glist-result-set.h
44
45-test_dee_CPPFLAGS = \
46- -I$(top_srcdir) \
47- -I$(top_srcdir)/dee \
48- -I$(top_srcdir)/dee \
49- -DTESTDIR=\""$(top_builddir)/tests"\" \
50- -DDEE_COMPILATION \
51- $(GCC_FLAGS) \
52- $(DEE_CFLAGS) \
53- $(MAINTAINER_CFLAGS)
54-
55 test_dee_LDADD = $(top_builddir)/dee/libdee-1.0.la $(DEE_LIBS)
56
57 if HAVE_GTX
58 test_dee_SOURCES += test-model-interactions.c
59 test_dee_SOURCES += test-peer-interactions.c
60-test_dee_CPPFLAGS += $(GTX_CFLAGS)
61+AM_CPPFLAGS += $(GTX_CFLAGS)
62 test_dee_LDADD += $(GTX_LIBS)
63
64 model_helpers = \
65@@ -56,15 +65,12 @@
66 $(peer_helpers:.c=)
67
68 model_helper_clone3rows_SOURCES = model-helper-clone3rows.c
69-model_helper_clone3rows_CPPFLAGS = $(test_dee_CPPFLAGS)
70 model_helper_clone3rows_LDADD = $(test_dee_LDADD)
71
72 model_helper_add3rows_SOURCES = model-helper-add3rows.c
73-model_helper_add3rows_CPPFLAGS = $(test_dee_CPPFLAGS)
74 model_helper_add3rows_LDADD = $(test_dee_LDADD)
75
76 model_helper_change3rows_SOURCES = model-helper-change3rows.c
77-model_helper_change3rows_CPPFLAGS = $(test_dee_CPPFLAGS)
78 model_helper_change3rows_LDADD = $(test_dee_LDADD)
79
80 model_helper_remove3rows_SOURCES = model-helper-remove3rows.c
81@@ -72,11 +78,9 @@
82 model_helper_remove3rows_LDADD = $(test_dee_LDADD)
83
84 model_helper_clear3rows_SOURCES = model-helper-clear3rows.c
85-model_helper_clear3rows_CPPFLAGS = $(test_dee_CPPFLAGS)
86 model_helper_clear3rows_LDADD = $(test_dee_LDADD)
87
88 model_helper_clear3add5_SOURCES = model-helper-clear3add5.c
89-model_helper_clear3add5_CPPFLAGS = $(test_dee_CPPFLAGS)
90 model_helper_clear3add5_LDADD = $(test_dee_LDADD)
91
92 model_helper_insert1row_SOURCES = model-helper-insert1row.c
93@@ -84,15 +88,12 @@
94 model_helper_insert1row_LDADD = $(test_dee_LDADD)
95
96 model_helper_schemaless_SOURCES = model-helper-schemaless.c
97-model_helper_schemaless_CPPFLAGS = $(test_dee_CPPFLAGS)
98 model_helper_schemaless_LDADD = $(test_dee_LDADD)
99
100 model_helper_introspect_SOURCES = model-helper-introspect.c
101-model_helper_introspect_CPPFLAGS = $(test_dee_CPPFLAGS)
102 model_helper_introspect_LDADD = $(test_dee_LDADD)
103
104 peer_helper_1peer_SOURCES = peer-helper-1peer.c
105-peer_helper_1peer_CPPFLAGS = $(test_dee_CPPFLAGS)
106 peer_helper_1peer_LDADD = $(test_dee_LDADD)
107
108 endif # HAVE_GTX
109@@ -147,3 +148,4 @@
110
111 clean-generic:
112 rm -rf test-dee-results.xml test-dee-results.html dee-test-resource-manager
113+
114
115=== added file 'tests/test-benchmark.c'
116--- tests/test-benchmark.c 1970-01-01 00:00:00 +0000
117+++ tests/test-benchmark.c 2011-12-07 15:49:34 +0000
118@@ -0,0 +1,192 @@
119+/*
120+ * Copyright (C) 2011 Canonical Ltd
121+ *
122+ * This program is free software: you can redistribute it and/or modify
123+ * it under the terms of the GNU General Public License version 3 as
124+ * published by the Free Software Foundation.
125+ *
126+ * This program is distributed in the hope that it will be useful,
127+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
128+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
129+ * GNU General Public License for more details.
130+ *
131+ * You should have received a copy of the GNU General Public License
132+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
133+ *
134+ * Authored by
135+ * Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
136+ *
137+ */
138+
139+#include "config.h"
140+#include <math.h>
141+#include <glib.h>
142+#include <glib/gprintf.h>
143+#include <glib-object.h>
144+
145+#include <dee.h>
146+
147+typedef struct _Benchmark Benchmark;
148+typedef void (*BenchmarkFunc) (Benchmark *benchmark);
149+typedef void (*BenchmarkSetup) (Benchmark *benchmark);
150+typedef void (*BenchmarkTeardown) (Benchmark *benchmark);
151+
152+typedef struct {
153+ gdouble elapsed;
154+} RunData;
155+
156+struct _Benchmark {
157+ const gchar *name;
158+ BenchmarkSetup benchmark_setup;
159+ BenchmarkFunc benchmark_func;
160+ BenchmarkTeardown benchmark_teardown;
161+ guint n_runs;
162+ RunData *runs;
163+ gpointer state;
164+};
165+
166+static GList *benchmarks = NULL;
167+
168+static void
169+add_benchmark (Benchmark *benchmark)
170+{
171+ benchmarks = g_list_append (benchmarks, benchmark);
172+}
173+
174+static void
175+run_benchmark (Benchmark *bench)
176+{
177+ GTimer *timer;
178+ guint i;
179+ gdouble total_runtime, avg_runtime, std_dev;
180+
181+ bench->runs = g_new0 (RunData, bench->n_runs + 1);
182+ timer = g_timer_new ();
183+ total_runtime = 0;
184+
185+ bench->benchmark_setup (bench);
186+
187+ g_printf ("=== %s ===\n", bench->name);
188+
189+ for (i = 0; i < bench->n_runs; i++)
190+ {
191+ g_timer_start (timer);
192+ bench->benchmark_func (bench);
193+ bench->runs[i].elapsed = g_timer_elapsed (timer, NULL);
194+ total_runtime += bench->runs[i].elapsed;
195+ }
196+
197+ /* Compute average runtime */
198+ avg_runtime = total_runtime / bench->n_runs;
199+
200+ /* Compute standard deviation */
201+ std_dev = 0;
202+ for (i = 0; i < bench->n_runs; i++)
203+ {
204+ gdouble delta = bench->runs[i].elapsed - avg_runtime;
205+ std_dev += delta * delta;
206+ }
207+ std_dev = sqrt (std_dev / bench->n_runs);
208+
209+ /* Print report */
210+ g_printf ("Runs : %u\n", bench->n_runs);
211+ g_printf ("Total runtime : %fs\n", total_runtime);
212+ g_printf ("Avg. runtime : %fs\n", avg_runtime);
213+ g_printf ("Std. deviation : %fs\n", std_dev);
214+ g_printf ("\n");
215+
216+
217+
218+ g_timer_destroy (timer);
219+ bench->benchmark_teardown (bench);
220+ // purposely leak bench->runs. Caller may want it
221+}
222+
223+static void
224+run_benchmarks (void)
225+{
226+ GList *iter;
227+
228+ for (iter = benchmarks; iter; iter = iter->next)
229+ {
230+ Benchmark *bench = (Benchmark*) iter->data;
231+ run_benchmark (bench);
232+ }
233+}
234+
235+static void
236+bench_seqmodel_setup (Benchmark *bench)
237+{
238+ DeeModel *model;
239+
240+ model = dee_sequence_model_new ();
241+ dee_model_set_schema (model, "s", "s", "s", "u", "b", NULL);
242+ bench->state = model;
243+}
244+
245+static void
246+bench_seqmodel_append_run (Benchmark *bench)
247+{
248+ DeeModel *model;
249+ guint limit = 1000, i;
250+
251+ g_assert (DEE_IS_MODEL (bench->state));
252+
253+ model = DEE_MODEL (bench->state);
254+
255+ for (i = 0; i < limit; i++)
256+ {
257+ dee_model_append (model, "Hello", "world", "!", 42, TRUE);
258+ }
259+}
260+
261+static void
262+bench_seqmodel_prepend_run (Benchmark *bench)
263+{
264+ DeeModel *model;
265+ guint limit = 1000, i;
266+
267+ g_assert (DEE_IS_MODEL (bench->state));
268+
269+ model = DEE_MODEL (bench->state);
270+
271+ for (i = 0; i < limit; i++)
272+ {
273+ dee_model_prepend (model, "Hello", "world", "!", 42, TRUE);
274+ }
275+}
276+
277+static void
278+bench_gobject_teardown (Benchmark *bench)
279+{
280+ GObject *obj;
281+
282+ obj = G_OBJECT (bench->state);
283+ g_object_unref (obj);
284+}
285+
286+gint
287+main (gint argc, gchar *argv[])
288+{
289+ g_type_init ();
290+ g_thread_init (NULL);
291+
292+ Benchmark seqmodel_append = { "SequenceModel.append()",
293+ bench_seqmodel_setup,
294+ bench_seqmodel_append_run,
295+ bench_gobject_teardown,
296+ 10,
297+ NULL };
298+ add_benchmark (&seqmodel_append);
299+
300+ Benchmark seqmodel_prepend = { "SequenceModel.prepend()",
301+ bench_seqmodel_setup,
302+ bench_seqmodel_prepend_run,
303+ bench_gobject_teardown,
304+ 10,
305+ NULL };
306+ add_benchmark (&seqmodel_prepend);
307+
308+ run_benchmarks ();
309+ return 0;
310+}

Subscribers

People subscribed via source and target branches