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 static GType simple_object_get_type (void);
154 #define SIMPLE_TYPE_OBJECT (simple_object_get_type ())
155 typedef struct _SimpleObject SimpleObject;
156 typedef struct _SimpleObjectClass SimpleObjectClass;
160 GObject parent_instance;
164 struct _SimpleObjectClass
166 GObjectClass parent_class;
169 G_DEFINE_TYPE (SimpleObject, simple_object, G_TYPE_OBJECT);
172 simple_object_finalize (GObject *object)
174 G_OBJECT_CLASS (simple_object_parent_class)->finalize (object);
178 simple_object_class_init (SimpleObjectClass *class)
180 GObjectClass *object_class = G_OBJECT_CLASS (class);
182 object_class->finalize = simple_object_finalize;
186 simple_object_init (SimpleObject *simple_object)
188 simple_object->val = 42;
191 typedef struct _TestIfaceClass TestIfaceClass;
192 typedef struct _TestIfaceClass TestIface1Class;
193 typedef struct _TestIfaceClass TestIface2Class;
194 typedef struct _TestIfaceClass TestIface3Class;
195 typedef struct _TestIfaceClass TestIface4Class;
196 typedef struct _TestIfaceClass TestIface5Class;
197 typedef struct _TestIface TestIface;
199 struct _TestIfaceClass
201 GTypeInterface base_iface;
202 void (*method) (TestIface *obj);
205 static GType test_iface1_get_type (void);
206 static GType test_iface2_get_type (void);
207 static GType test_iface3_get_type (void);
208 static GType test_iface4_get_type (void);
209 static GType test_iface5_get_type (void);
211 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
212 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
213 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
214 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
215 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
217 static DEFINE_IFACE (TestIface1, test_iface1, NULL, NULL)
218 static DEFINE_IFACE (TestIface2, test_iface2, NULL, NULL)
219 static DEFINE_IFACE (TestIface3, test_iface3, NULL, NULL)
220 static DEFINE_IFACE (TestIface4, test_iface4, NULL, NULL)
221 static DEFINE_IFACE (TestIface5, test_iface5, NULL, NULL)
223 /*************************************************************
224 * Complex object is a GObject subclass with a properties,
225 * construct properties, signals and implementing an interface.
226 *************************************************************/
228 static GType complex_object_get_type (void);
229 #define COMPLEX_TYPE_OBJECT (complex_object_get_type ())
230 typedef struct _ComplexObject ComplexObject;
231 typedef struct _ComplexObjectClass ComplexObjectClass;
233 struct _ComplexObject
235 GObject parent_instance;
240 struct _ComplexObjectClass
242 GObjectClass parent_class;
244 void (*signal) (ComplexObject *obj);
245 void (*signal_empty) (ComplexObject *obj);
248 static void complex_test_iface_init (gpointer g_iface,
249 gpointer iface_data);
251 G_DEFINE_TYPE_EXTENDED (ComplexObject, complex_object,
253 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE1,
254 complex_test_iface_init)
255 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE2,
256 complex_test_iface_init)
257 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE3,
258 complex_test_iface_init)
259 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE4,
260 complex_test_iface_init)
261 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE5,
262 complex_test_iface_init)
265 #define COMPLEX_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), COMPLEX_TYPE_OBJECT, ComplexObject))
275 COMPLEX_SIGNAL_EMPTY,
276 COMPLEX_SIGNAL_GENERIC,
277 COMPLEX_SIGNAL_GENERIC_EMPTY,
281 static guint complex_signals[COMPLEX_LAST_SIGNAL] = { 0 };
284 complex_object_finalize (GObject *object)
286 G_OBJECT_CLASS (complex_object_parent_class)->finalize (object);
290 complex_object_set_property (GObject *object,
295 ComplexObject *complex = COMPLEX_OBJECT (object);
300 complex->val1 = g_value_get_int (value);
303 complex->val2 = g_value_get_int (value);
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 complex_object_get_property (GObject *object,
317 ComplexObject *complex = COMPLEX_OBJECT (object);
322 g_value_set_int (value, complex->val1);
325 g_value_set_int (value, complex->val2);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 complex_object_real_signal (ComplexObject *obj)
339 complex_object_class_init (ComplexObjectClass *class)
341 GObjectClass *object_class = G_OBJECT_CLASS (class);
343 object_class->finalize = complex_object_finalize;
344 object_class->set_property = complex_object_set_property;
345 object_class->get_property = complex_object_get_property;
347 class->signal = complex_object_real_signal;
349 complex_signals[COMPLEX_SIGNAL] =
350 g_signal_new ("signal",
351 G_TYPE_FROM_CLASS (object_class),
353 G_STRUCT_OFFSET (ComplexObjectClass, signal),
355 g_cclosure_marshal_VOID__VOID,
358 complex_signals[COMPLEX_SIGNAL_EMPTY] =
359 g_signal_new ("signal-empty",
360 G_TYPE_FROM_CLASS (object_class),
362 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
364 g_cclosure_marshal_VOID__VOID,
367 complex_signals[COMPLEX_SIGNAL_GENERIC] =
368 g_signal_new ("signal-generic",
369 G_TYPE_FROM_CLASS (object_class),
371 G_STRUCT_OFFSET (ComplexObjectClass, signal),
375 complex_signals[COMPLEX_SIGNAL_GENERIC_EMPTY] =
376 g_signal_new ("signal-generic-empty",
377 G_TYPE_FROM_CLASS (object_class),
379 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
384 g_object_class_install_property (object_class,
386 g_param_spec_int ("val1",
392 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
393 g_object_class_install_property (object_class,
395 g_param_spec_int ("val2",
407 complex_object_iface_method (TestIface *obj)
409 ComplexObject *complex = COMPLEX_OBJECT (obj);
414 complex_test_iface_init (gpointer g_iface,
417 TestIfaceClass *iface = g_iface;
418 iface->method = complex_object_iface_method;
422 complex_object_init (ComplexObject *complex_object)
424 complex_object->val2 = 43;
427 /*************************************************************
428 * Test object construction performance
429 *************************************************************/
431 #define NUM_OBJECT_TO_CONSTRUCT 10000
433 struct ConstructionTest {
440 test_construction_setup (PerformanceTest *test)
442 struct ConstructionTest *data;
444 data = g_new0 (struct ConstructionTest, 1);
445 data->type = ((GType (*)(void))test->extra_data)();
451 test_construction_init (PerformanceTest *test,
455 struct ConstructionTest *data = _data;
458 n = NUM_OBJECT_TO_CONSTRUCT * count_factor;
459 if (data->n_objects != n)
462 data->objects = g_new (GObject *, n);
467 test_construction_run (PerformanceTest *test,
470 struct ConstructionTest *data = _data;
471 GObject **objects = data->objects;
472 GType type = data->type;
475 n_objects = data->n_objects;
476 for (i = 0; i < n_objects; i++)
477 objects[i] = g_object_new (type, NULL);
481 test_construction_finish (PerformanceTest *test,
484 struct ConstructionTest *data = _data;
487 for (i = 0; i < data->n_objects; i++)
488 g_object_unref (data->objects[i]);
492 test_construction_teardown (PerformanceTest *test,
495 struct ConstructionTest *data = _data;
496 g_free (data->objects);
501 test_construction_print_result (PerformanceTest *test,
505 struct ConstructionTest *data = _data;
507 g_print ("Number of constructed objects per second: %.0f\n",
508 data->n_objects / time);
511 /*************************************************************
512 * Test runtime type check performance
513 *************************************************************/
515 #define NUM_KILO_CHECKS_PER_ROUND 50
517 struct TypeCheckTest {
523 test_type_check_setup (PerformanceTest *test)
525 struct TypeCheckTest *data;
527 data = g_new0 (struct TypeCheckTest, 1);
528 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
534 test_type_check_init (PerformanceTest *test,
538 struct TypeCheckTest *data = _data;
540 data->n_checks = factor * NUM_KILO_CHECKS_PER_ROUND;
544 /* Work around g_type_check_instance_is_a being marked "pure",
545 and thus only called once for the loop. */
546 gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
547 GType iface_type) = &g_type_check_instance_is_a;
550 test_type_check_run (PerformanceTest *test,
553 struct TypeCheckTest *data = _data;
554 volatile GObject *object = data->object;
555 volatile GType type, types[5];
558 types[0] = test_iface1_get_type ();
559 types[1] = test_iface2_get_type ();
560 types[2] = test_iface3_get_type ();
561 types[3] = test_iface4_get_type ();
562 types[4] = test_iface5_get_type ();
564 for (i = 0; i < data->n_checks; i++)
567 for (j = 0; j < 1000; j++)
569 my_type_check_instance_is_a ((GTypeInstance *)object,
576 test_type_check_finish (PerformanceTest *test,
582 test_type_check_print_result (PerformanceTest *test,
586 struct TypeCheckTest *data = _data;
587 g_print ("Million type checks per second: %.2f\n",
588 data->n_checks / (1000*time));
592 test_type_check_teardown (PerformanceTest *test,
595 struct TypeCheckTest *data = _data;
597 g_object_unref (data->object);
601 /*************************************************************
602 * Test signal emissions performance (common code)
603 *************************************************************/
605 #define NUM_EMISSIONS_PER_ROUND 10000
607 struct EmissionTest {
614 test_emission_run (PerformanceTest *test,
617 struct EmissionTest *data = _data;
618 GObject *object = data->object;
621 for (i = 0; i < data->n_checks; i++)
622 g_signal_emit (object, data->signal_id, 0);
625 /*************************************************************
626 * Test signal unhandled emissions performance
627 *************************************************************/
630 test_emission_unhandled_setup (PerformanceTest *test)
632 struct EmissionTest *data;
634 data = g_new0 (struct EmissionTest, 1);
635 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
636 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
641 test_emission_unhandled_init (PerformanceTest *test,
645 struct EmissionTest *data = _data;
647 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
651 test_emission_unhandled_finish (PerformanceTest *test,
657 test_emission_unhandled_print_result (PerformanceTest *test,
661 struct EmissionTest *data = _data;
663 g_print ("Emissions per second: %.0f\n",
664 data->n_checks / time);
668 test_emission_unhandled_teardown (PerformanceTest *test,
671 struct EmissionTest *data = _data;
673 g_object_unref (data->object);
677 /*************************************************************
678 * Test signal handled emissions performance
679 *************************************************************/
682 test_emission_handled_handler (ComplexObject *obj, gpointer data)
687 test_emission_handled_setup (PerformanceTest *test)
689 struct EmissionTest *data;
691 data = g_new0 (struct EmissionTest, 1);
692 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
693 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
694 g_signal_connect (data->object, "signal",
695 G_CALLBACK (test_emission_handled_handler),
697 g_signal_connect (data->object, "signal-empty",
698 G_CALLBACK (test_emission_handled_handler),
700 g_signal_connect (data->object, "signal-generic",
701 G_CALLBACK (test_emission_handled_handler),
703 g_signal_connect (data->object, "signal-generic-empty",
704 G_CALLBACK (test_emission_handled_handler),
711 test_emission_handled_init (PerformanceTest *test,
715 struct EmissionTest *data = _data;
717 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
721 test_emission_handled_finish (PerformanceTest *test,
727 test_emission_handled_print_result (PerformanceTest *test,
731 struct EmissionTest *data = _data;
733 g_print ("Emissions per second: %.0f\n",
734 data->n_checks / time);
738 test_emission_handled_teardown (PerformanceTest *test,
741 struct EmissionTest *data = _data;
743 g_object_unref (data->object);
747 /*************************************************************
749 *************************************************************/
751 static PerformanceTest tests[] = {
753 "simple-construction",
754 simple_object_get_type,
755 test_construction_setup,
756 test_construction_init,
757 test_construction_run,
758 test_construction_finish,
759 test_construction_teardown,
760 test_construction_print_result
763 "complex-construction",
764 complex_object_get_type,
765 test_construction_setup,
766 test_construction_init,
767 test_construction_run,
768 test_construction_finish,
769 test_construction_teardown,
770 test_construction_print_result
775 test_type_check_setup,
776 test_type_check_init,
778 test_type_check_finish,
779 test_type_check_teardown,
780 test_type_check_print_result
784 GINT_TO_POINTER (COMPLEX_SIGNAL),
785 test_emission_unhandled_setup,
786 test_emission_unhandled_init,
788 test_emission_unhandled_finish,
789 test_emission_unhandled_teardown,
790 test_emission_unhandled_print_result
793 "emit-unhandled-empty",
794 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
795 test_emission_unhandled_setup,
796 test_emission_unhandled_init,
798 test_emission_unhandled_finish,
799 test_emission_unhandled_teardown,
800 test_emission_unhandled_print_result
803 "emit-unhandled-generic",
804 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
805 test_emission_unhandled_setup,
806 test_emission_unhandled_init,
808 test_emission_unhandled_finish,
809 test_emission_unhandled_teardown,
810 test_emission_unhandled_print_result
813 "emit-unhandled-generic-empty",
814 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
815 test_emission_unhandled_setup,
816 test_emission_unhandled_init,
818 test_emission_unhandled_finish,
819 test_emission_unhandled_teardown,
820 test_emission_unhandled_print_result
824 GINT_TO_POINTER (COMPLEX_SIGNAL),
825 test_emission_handled_setup,
826 test_emission_handled_init,
828 test_emission_handled_finish,
829 test_emission_handled_teardown,
830 test_emission_handled_print_result
833 "emit-handled-empty",
834 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
835 test_emission_handled_setup,
836 test_emission_handled_init,
838 test_emission_handled_finish,
839 test_emission_handled_teardown,
840 test_emission_handled_print_result
843 "emit-handled-generic",
844 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
845 test_emission_handled_setup,
846 test_emission_handled_init,
848 test_emission_handled_finish,
849 test_emission_handled_teardown,
850 test_emission_handled_print_result
853 "emit-handled-generic-empty",
854 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
855 test_emission_handled_setup,
856 test_emission_handled_init,
858 test_emission_handled_finish,
859 test_emission_handled_teardown,
860 test_emission_handled_print_result
864 static PerformanceTest *
865 find_test (const char *name)
868 for (i = 0; i < G_N_ELEMENTS (tests); i++)
870 if (strcmp (tests[i].name, name) == 0)
879 PerformanceTest *test;
880 GOptionContext *context;
881 GError *error = NULL;
884 context = g_option_context_new ("GObject performance tests");
885 g_option_context_add_main_entries (context, cmd_entries, NULL);
886 if (!g_option_context_parse (context, &argc, &argv, &error))
888 g_printerr ("%s: %s\n", argv[0], error->message);
894 for (i = 1; i < argc; i++)
896 test = find_test (argv[i]);
903 for (i = 0; i < G_N_ELEMENTS (tests); i++)
904 run_test (&tests[i]);