1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2009 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <glib-object.h>
23 #include "testcommon.h"
25 #define WARM_UP_N_RUNS 50
26 #define ESTIMATE_ROUND_TIME_N_RUNS 5
27 #define DEFAULT_TEST_TIME 15 /* seconds */
28 /* The time we want each round to take, in seconds, this should
29 * be large enough compared to the timer resolution, but small
30 * enought that the risk of any random slowness will miss the
32 #define TARGET_ROUND_TIME 0.004
34 static gboolean verbose = FALSE;
35 static int test_length = DEFAULT_TEST_TIME;
37 static GOptionEntry cmd_entries[] = {
38 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
39 "Print extra information", NULL},
40 {"seconds", 's', 0, G_OPTION_ARG_INT, &test_length,
41 "Time to run each test in seconds", NULL},
45 typedef struct _PerformanceTest PerformanceTest;
46 struct _PerformanceTest {
50 gpointer (*setup) (PerformanceTest *test);
51 void (*init) (PerformanceTest *test,
54 void (*run) (PerformanceTest *test,
56 void (*finish) (PerformanceTest *test,
58 void (*teardown) (PerformanceTest *test,
60 void (*print_result) (PerformanceTest *test,
66 run_test (PerformanceTest *test)
69 guint64 i, num_rounds;
70 double elapsed, min_elapsed, factor;
73 g_print ("Running test %s\n", test->name);
76 timer = g_timer_new ();
77 data = test->setup (test);
80 g_print ("Warming up\n");
82 /* Warm up the test by doing a few runs */
83 for (i = 0; i < WARM_UP_N_RUNS; i++)
85 test->init (test, data, 1.0);
86 test->run (test, data);
87 test->finish (test, data);
91 g_print ("Estimating round time\n");
93 /* Estimate time for one run by doing a few test rounds */
95 for (i = 0; i < ESTIMATE_ROUND_TIME_N_RUNS; i++)
97 test->init (test, data, 1.0);
98 g_timer_start (timer);
99 test->run (test, data);
100 g_timer_stop (timer);
101 test->finish (test, data);
103 elapsed = g_timer_elapsed (timer, NULL);
105 min_elapsed = elapsed;
107 min_elapsed = MIN (min_elapsed, elapsed);
110 factor = TARGET_ROUND_TIME / min_elapsed;
113 g_print ("Uncorrected round time: %f.4 secs, correction factor %f.2\n", min_elapsed, factor);
115 /* Calculate number of rounds needed */
116 num_rounds = (test_length / TARGET_ROUND_TIME) + 1;
119 g_print ("Running %"G_GINT64_MODIFIER"d rounds\n", num_rounds);
122 for (i = 0; i < num_rounds; i++)
124 test->init (test, data, factor);
125 g_timer_start (timer);
126 test->run (test, data);
127 g_timer_stop (timer);
128 test->finish (test, data);
129 elapsed = g_timer_elapsed (timer, NULL);
132 min_elapsed = elapsed;
134 min_elapsed = MIN (min_elapsed, elapsed);
138 g_print ("Minimum corrected round time: %f secs\n", min_elapsed);
140 /* Print the results */
141 test->print_result (test, data, min_elapsed);
144 test->teardown (test, data);
145 g_timer_destroy (timer);
148 /*************************************************************
149 * Simple object is a very simple small GObject subclass
150 * with no properties, no signals, implementing no interfaces
151 *************************************************************/
153 #define SIMPLE_TYPE_OBJECT (simple_object_get_type ())
154 typedef struct _SimpleObject SimpleObject;
155 typedef struct _SimpleObjectClass SimpleObjectClass;
159 GObject parent_instance;
163 struct _SimpleObjectClass
165 GObjectClass parent_class;
168 G_DEFINE_TYPE (SimpleObject, simple_object, G_TYPE_OBJECT);
171 simple_object_finalize (GObject *object)
173 G_OBJECT_CLASS (simple_object_parent_class)->finalize (object);
177 simple_object_class_init (SimpleObjectClass *class)
179 GObjectClass *object_class = G_OBJECT_CLASS (class);
181 object_class->finalize = simple_object_finalize;
185 simple_object_init (SimpleObject *simple_object)
187 simple_object->val = 42;
190 typedef struct _TestIfaceClass TestIfaceClass;
191 typedef struct _TestIfaceClass TestIface1Class;
192 typedef struct _TestIfaceClass TestIface2Class;
193 typedef struct _TestIfaceClass TestIface3Class;
194 typedef struct _TestIfaceClass TestIface4Class;
195 typedef struct _TestIfaceClass TestIface5Class;
196 typedef struct _TestIface TestIface;
198 struct _TestIfaceClass
200 GTypeInterface base_iface;
201 void (*method) (TestIface *obj);
204 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
205 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
206 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
207 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
208 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
210 static DEFINE_IFACE (TestIface1, test_iface1, NULL, NULL)
211 static DEFINE_IFACE (TestIface2, test_iface2, NULL, NULL)
212 static DEFINE_IFACE (TestIface3, test_iface3, NULL, NULL)
213 static DEFINE_IFACE (TestIface4, test_iface4, NULL, NULL)
214 static DEFINE_IFACE (TestIface5, test_iface5, NULL, NULL)
216 /*************************************************************
217 * Complex object is a GObject subclass with a properties,
218 * construct properties, signals and implementing an interface.
219 *************************************************************/
221 #define COMPLEX_TYPE_OBJECT (complex_object_get_type ())
222 typedef struct _ComplexObject ComplexObject;
223 typedef struct _ComplexObjectClass ComplexObjectClass;
225 struct _ComplexObject
227 GObject parent_instance;
232 struct _ComplexObjectClass
234 GObjectClass parent_class;
236 void (*signal) (ComplexObject *obj);
239 static void complex_test_iface_init (gpointer g_iface,
240 gpointer iface_data);
242 G_DEFINE_TYPE_EXTENDED (ComplexObject, complex_object,
244 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE1,
245 complex_test_iface_init)
246 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE2,
247 complex_test_iface_init)
248 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE3,
249 complex_test_iface_init)
250 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE4,
251 complex_test_iface_init)
252 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE5,
253 complex_test_iface_init)
256 #define COMPLEX_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), COMPLEX_TYPE_OBJECT, ComplexObject))
269 static guint complex_signals[COMPLEX_LAST_SIGNAL] = { 0 };
272 complex_object_finalize (GObject *object)
274 G_OBJECT_CLASS (complex_object_parent_class)->finalize (object);
278 complex_object_set_property (GObject *object,
283 ComplexObject *complex = COMPLEX_OBJECT (object);
288 complex->val1 = g_value_get_int (value);
291 complex->val2 = g_value_get_int (value);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 complex_object_get_property (GObject *object,
305 ComplexObject *complex = COMPLEX_OBJECT (object);
310 g_value_set_int (value, complex->val1);
313 g_value_set_int (value, complex->val2);
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 complex_object_real_signal (ComplexObject *obj)
327 complex_object_class_init (ComplexObjectClass *class)
329 GObjectClass *object_class = G_OBJECT_CLASS (class);
331 object_class->finalize = complex_object_finalize;
332 object_class->set_property = complex_object_set_property;
333 object_class->get_property = complex_object_get_property;
334 class->signal = complex_object_real_signal;
336 complex_signals[COMPLEX_SIGNAL] =
337 g_signal_new ("signal",
338 G_TYPE_FROM_CLASS (object_class),
340 G_STRUCT_OFFSET (ComplexObjectClass, signal),
342 g_cclosure_marshal_VOID__VOID,
345 g_object_class_install_property (object_class,
347 g_param_spec_int ("val1",
353 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
354 g_object_class_install_property (object_class,
356 g_param_spec_int ("val2",
368 complex_object_iface_method (TestIface *obj)
370 ComplexObject *complex = COMPLEX_OBJECT (obj);
375 complex_test_iface_init (gpointer g_iface,
378 TestIfaceClass *iface = g_iface;
379 iface->method = complex_object_iface_method;
383 complex_object_init (ComplexObject *complex_object)
385 complex_object->val2 = 43;
388 /*************************************************************
389 * Test object construction performance
390 *************************************************************/
392 #define NUM_OBJECT_TO_CONSTRUCT 10000
394 struct ConstructionTest {
401 test_construction_setup (PerformanceTest *test)
403 struct ConstructionTest *data;
405 data = g_new0 (struct ConstructionTest, 1);
406 data->type = ((GType (*)())test->extra_data)();
412 test_construction_init (PerformanceTest *test,
416 struct ConstructionTest *data = _data;
419 n = NUM_OBJECT_TO_CONSTRUCT * count_factor;
420 if (data->n_objects != n)
423 data->objects = g_new (GObject *, n);
428 test_construction_run (PerformanceTest *test,
431 struct ConstructionTest *data = _data;
432 GObject **objects = data->objects;
433 GType type = data->type;
436 n_objects = data->n_objects;
437 for (i = 0; i < n_objects; i++)
438 objects[i] = g_object_new (type, NULL);
442 test_construction_finish (PerformanceTest *test,
445 struct ConstructionTest *data = _data;
448 for (i = 0; i < data->n_objects; i++)
449 g_object_unref (data->objects[i]);
453 test_construction_teardown (PerformanceTest *test,
456 struct ConstructionTest *data = _data;
457 g_free (data->objects);
462 test_construction_print_result (PerformanceTest *test,
466 struct ConstructionTest *data = _data;
468 g_print ("Number of constructed objects per second: %.0f\n",
469 data->n_objects / time);
472 /*************************************************************
473 * Test runtime type check performance
474 *************************************************************/
476 #define NUM_KILO_CHECKS_PER_ROUND 50
478 struct TypeCheckTest {
484 test_type_check_setup (PerformanceTest *test)
486 struct TypeCheckTest *data;
488 data = g_new0 (struct TypeCheckTest, 1);
489 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
495 test_type_check_init (PerformanceTest *test,
499 struct TypeCheckTest *data = _data;
501 data->n_checks = factor * NUM_KILO_CHECKS_PER_ROUND;
505 /* Work around g_type_check_instance_is_a being marked "pure",
506 and thus only called once for the loop. */
507 gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
508 GType iface_type) = &g_type_check_instance_is_a;
511 test_type_check_run (PerformanceTest *test,
514 struct TypeCheckTest *data = _data;
515 volatile GObject *object = data->object;
516 volatile GType type, types[5];
519 types[0] = test_iface1_get_type ();
520 types[1] = test_iface2_get_type ();
521 types[2] = test_iface3_get_type ();
522 types[3] = test_iface4_get_type ();
523 types[4] = test_iface5_get_type ();
525 for (i = 0; i < data->n_checks; i++)
528 for (j = 0; j < 1000; j++)
530 my_type_check_instance_is_a ((GTypeInstance *)object,
537 test_type_check_finish (PerformanceTest *test,
543 test_type_check_print_result (PerformanceTest *test,
547 struct TypeCheckTest *data = _data;
548 g_print ("Million type checks per second: %.2f\n",
549 data->n_checks / (1000*time));
553 test_type_check_teardown (PerformanceTest *test,
556 struct TypeCheckTest *data = _data;
558 g_object_unref (data->object);
562 /*************************************************************
563 * Test signal unhandled emissions performance
564 *************************************************************/
566 #define NUM_EMISSIONS_PER_ROUND 10000
568 struct EmissionTest {
574 test_emission_unhandled_setup (PerformanceTest *test)
576 struct EmissionTest *data;
578 data = g_new0 (struct EmissionTest, 1);
579 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
585 test_emission_unhandled_init (PerformanceTest *test,
589 struct EmissionTest *data = _data;
591 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
595 test_emission_unhandled_run (PerformanceTest *test,
598 struct EmissionTest *data = _data;
599 GObject *object = data->object;
602 for (i = 0; i < data->n_checks; i++)
603 g_signal_emit (object,
604 complex_signals[COMPLEX_SIGNAL],
609 test_emission_unhandled_finish (PerformanceTest *test,
615 test_emission_unhandled_print_result (PerformanceTest *test,
619 struct EmissionTest *data = _data;
621 g_print ("Emissions per second: %.0f\n",
622 data->n_checks / time);
626 test_emission_unhandled_teardown (PerformanceTest *test,
629 struct EmissionTest *data = _data;
631 g_object_unref (data->object);
635 /*************************************************************
636 * Test signal handled emissions performance
637 *************************************************************/
640 test_emission_handled_handler (ComplexObject *obj, gpointer data)
645 test_emission_handled_setup (PerformanceTest *test)
647 struct EmissionTest *data;
649 data = g_new0 (struct EmissionTest, 1);
650 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
651 g_signal_connect (data->object, "signal",
652 G_CALLBACK (test_emission_handled_handler),
659 test_emission_handled_init (PerformanceTest *test,
663 struct EmissionTest *data = _data;
665 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
669 test_emission_handled_run (PerformanceTest *test,
672 struct EmissionTest *data = _data;
673 GObject *object = data->object;
676 for (i = 0; i < data->n_checks; i++)
677 g_signal_emit (object,
678 complex_signals[COMPLEX_SIGNAL],
683 test_emission_handled_finish (PerformanceTest *test,
689 test_emission_handled_print_result (PerformanceTest *test,
693 struct EmissionTest *data = _data;
695 g_print ("Emissions per second: %.0f\n",
696 data->n_checks / time);
700 test_emission_handled_teardown (PerformanceTest *test,
703 struct EmissionTest *data = _data;
705 g_object_unref (data->object);
709 /*************************************************************
711 *************************************************************/
713 static PerformanceTest tests[] = {
715 "simple-construction",
716 simple_object_get_type,
717 test_construction_setup,
718 test_construction_init,
719 test_construction_run,
720 test_construction_finish,
721 test_construction_teardown,
722 test_construction_print_result
725 "complex-construction",
726 complex_object_get_type,
727 test_construction_setup,
728 test_construction_init,
729 test_construction_run,
730 test_construction_finish,
731 test_construction_teardown,
732 test_construction_print_result
737 test_type_check_setup,
738 test_type_check_init,
740 test_type_check_finish,
741 test_type_check_teardown,
742 test_type_check_print_result
747 test_emission_unhandled_setup,
748 test_emission_unhandled_init,
749 test_emission_unhandled_run,
750 test_emission_unhandled_finish,
751 test_emission_unhandled_teardown,
752 test_emission_unhandled_print_result
757 test_emission_handled_setup,
758 test_emission_handled_init,
759 test_emission_handled_run,
760 test_emission_handled_finish,
761 test_emission_handled_teardown,
762 test_emission_handled_print_result
766 static PerformanceTest *
767 find_test (const char *name)
770 for (i = 0; i < G_N_ELEMENTS (tests); i++)
772 if (strcmp (tests[i].name, name) == 0)
781 PerformanceTest *test;
782 GOptionContext *context;
783 GError *error = NULL;
788 context = g_option_context_new ("GObject performance tests");
789 g_option_context_add_main_entries (context, cmd_entries, NULL);
790 if (!g_option_context_parse (context, &argc, &argv, &error))
792 g_printerr ("%s: %s\n", argv[0], error->message);
798 for (i = 1; i < argc; i++)
800 test = find_test (argv[i]);
807 for (i = 0; i < G_N_ELEMENTS (tests); i++)
808 run_test (&tests[i]);