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);
237 void (*signal_empty) (ComplexObject *obj);
240 static void complex_test_iface_init (gpointer g_iface,
241 gpointer iface_data);
243 G_DEFINE_TYPE_EXTENDED (ComplexObject, complex_object,
245 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE1,
246 complex_test_iface_init)
247 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE2,
248 complex_test_iface_init)
249 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE3,
250 complex_test_iface_init)
251 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE4,
252 complex_test_iface_init)
253 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE5,
254 complex_test_iface_init)
257 #define COMPLEX_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), COMPLEX_TYPE_OBJECT, ComplexObject))
267 COMPLEX_SIGNAL_EMPTY,
268 COMPLEX_SIGNAL_GENERIC,
269 COMPLEX_SIGNAL_GENERIC_EMPTY,
273 static guint complex_signals[COMPLEX_LAST_SIGNAL] = { 0 };
276 complex_object_finalize (GObject *object)
278 G_OBJECT_CLASS (complex_object_parent_class)->finalize (object);
282 complex_object_set_property (GObject *object,
287 ComplexObject *complex = COMPLEX_OBJECT (object);
292 complex->val1 = g_value_get_int (value);
295 complex->val2 = g_value_get_int (value);
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 complex_object_get_property (GObject *object,
309 ComplexObject *complex = COMPLEX_OBJECT (object);
314 g_value_set_int (value, complex->val1);
317 g_value_set_int (value, complex->val2);
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326 complex_object_real_signal (ComplexObject *obj)
331 complex_object_class_init (ComplexObjectClass *class)
333 GObjectClass *object_class = G_OBJECT_CLASS (class);
335 object_class->finalize = complex_object_finalize;
336 object_class->set_property = complex_object_set_property;
337 object_class->get_property = complex_object_get_property;
339 class->signal = complex_object_real_signal;
341 complex_signals[COMPLEX_SIGNAL] =
342 g_signal_new ("signal",
343 G_TYPE_FROM_CLASS (object_class),
345 G_STRUCT_OFFSET (ComplexObjectClass, signal),
347 g_cclosure_marshal_VOID__VOID,
350 complex_signals[COMPLEX_SIGNAL_EMPTY] =
351 g_signal_new ("signal-empty",
352 G_TYPE_FROM_CLASS (object_class),
354 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
356 g_cclosure_marshal_VOID__VOID,
359 complex_signals[COMPLEX_SIGNAL_GENERIC] =
360 g_signal_new ("signal-generic",
361 G_TYPE_FROM_CLASS (object_class),
363 G_STRUCT_OFFSET (ComplexObjectClass, signal),
367 complex_signals[COMPLEX_SIGNAL_GENERIC_EMPTY] =
368 g_signal_new ("signal-generic-empty",
369 G_TYPE_FROM_CLASS (object_class),
371 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
376 g_object_class_install_property (object_class,
378 g_param_spec_int ("val1",
384 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
385 g_object_class_install_property (object_class,
387 g_param_spec_int ("val2",
399 complex_object_iface_method (TestIface *obj)
401 ComplexObject *complex = COMPLEX_OBJECT (obj);
406 complex_test_iface_init (gpointer g_iface,
409 TestIfaceClass *iface = g_iface;
410 iface->method = complex_object_iface_method;
414 complex_object_init (ComplexObject *complex_object)
416 complex_object->val2 = 43;
419 /*************************************************************
420 * Test object construction performance
421 *************************************************************/
423 #define NUM_OBJECT_TO_CONSTRUCT 10000
425 struct ConstructionTest {
432 test_construction_setup (PerformanceTest *test)
434 struct ConstructionTest *data;
436 data = g_new0 (struct ConstructionTest, 1);
437 data->type = ((GType (*)())test->extra_data)();
443 test_construction_init (PerformanceTest *test,
447 struct ConstructionTest *data = _data;
450 n = NUM_OBJECT_TO_CONSTRUCT * count_factor;
451 if (data->n_objects != n)
454 data->objects = g_new (GObject *, n);
459 test_construction_run (PerformanceTest *test,
462 struct ConstructionTest *data = _data;
463 GObject **objects = data->objects;
464 GType type = data->type;
467 n_objects = data->n_objects;
468 for (i = 0; i < n_objects; i++)
469 objects[i] = g_object_new (type, NULL);
473 test_construction_finish (PerformanceTest *test,
476 struct ConstructionTest *data = _data;
479 for (i = 0; i < data->n_objects; i++)
480 g_object_unref (data->objects[i]);
484 test_construction_teardown (PerformanceTest *test,
487 struct ConstructionTest *data = _data;
488 g_free (data->objects);
493 test_construction_print_result (PerformanceTest *test,
497 struct ConstructionTest *data = _data;
499 g_print ("Number of constructed objects per second: %.0f\n",
500 data->n_objects / time);
503 /*************************************************************
504 * Test runtime type check performance
505 *************************************************************/
507 #define NUM_KILO_CHECKS_PER_ROUND 50
509 struct TypeCheckTest {
515 test_type_check_setup (PerformanceTest *test)
517 struct TypeCheckTest *data;
519 data = g_new0 (struct TypeCheckTest, 1);
520 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
526 test_type_check_init (PerformanceTest *test,
530 struct TypeCheckTest *data = _data;
532 data->n_checks = factor * NUM_KILO_CHECKS_PER_ROUND;
536 /* Work around g_type_check_instance_is_a being marked "pure",
537 and thus only called once for the loop. */
538 gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
539 GType iface_type) = &g_type_check_instance_is_a;
542 test_type_check_run (PerformanceTest *test,
545 struct TypeCheckTest *data = _data;
546 volatile GObject *object = data->object;
547 volatile GType type, types[5];
550 types[0] = test_iface1_get_type ();
551 types[1] = test_iface2_get_type ();
552 types[2] = test_iface3_get_type ();
553 types[3] = test_iface4_get_type ();
554 types[4] = test_iface5_get_type ();
556 for (i = 0; i < data->n_checks; i++)
559 for (j = 0; j < 1000; j++)
561 my_type_check_instance_is_a ((GTypeInstance *)object,
568 test_type_check_finish (PerformanceTest *test,
574 test_type_check_print_result (PerformanceTest *test,
578 struct TypeCheckTest *data = _data;
579 g_print ("Million type checks per second: %.2f\n",
580 data->n_checks / (1000*time));
584 test_type_check_teardown (PerformanceTest *test,
587 struct TypeCheckTest *data = _data;
589 g_object_unref (data->object);
593 /*************************************************************
594 * Test signal unhandled emissions performance
595 *************************************************************/
597 #define NUM_EMISSIONS_PER_ROUND 10000
599 struct EmissionTest {
606 test_emission_unhandled_setup (PerformanceTest *test)
608 struct EmissionTest *data;
610 data = g_new0 (struct EmissionTest, 1);
611 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
612 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
617 test_emission_unhandled_init (PerformanceTest *test,
621 struct EmissionTest *data = _data;
623 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
627 test_emission_unhandled_run (PerformanceTest *test,
630 struct EmissionTest *data = _data;
631 GObject *object = data->object;
634 for (i = 0; i < data->n_checks; i++)
635 g_signal_emit (object,
641 test_emission_unhandled_finish (PerformanceTest *test,
647 test_emission_unhandled_print_result (PerformanceTest *test,
651 struct EmissionTest *data = _data;
653 g_print ("Emissions per second: %.0f\n",
654 data->n_checks / time);
658 test_emission_unhandled_teardown (PerformanceTest *test,
661 struct EmissionTest *data = _data;
663 g_object_unref (data->object);
667 /*************************************************************
668 * Test signal handled emissions performance
669 *************************************************************/
672 test_emission_handled_handler (ComplexObject *obj, gpointer data)
677 test_emission_handled_setup (PerformanceTest *test)
679 struct EmissionTest *data;
681 data = g_new0 (struct EmissionTest, 1);
682 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
683 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
684 g_signal_connect (data->object, "signal",
685 G_CALLBACK (test_emission_handled_handler),
687 g_signal_connect (data->object, "signal-empty",
688 G_CALLBACK (test_emission_handled_handler),
690 g_signal_connect (data->object, "signal-generic",
691 G_CALLBACK (test_emission_handled_handler),
693 g_signal_connect (data->object, "signal-generic-empty",
694 G_CALLBACK (test_emission_handled_handler),
701 test_emission_handled_init (PerformanceTest *test,
705 struct EmissionTest *data = _data;
707 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
711 test_emission_handled_run (PerformanceTest *test,
714 struct EmissionTest *data = _data;
715 GObject *object = data->object;
718 for (i = 0; i < data->n_checks; i++)
719 g_signal_emit (object,
725 test_emission_handled_finish (PerformanceTest *test,
731 test_emission_handled_print_result (PerformanceTest *test,
735 struct EmissionTest *data = _data;
737 g_print ("Emissions per second: %.0f\n",
738 data->n_checks / time);
742 test_emission_handled_teardown (PerformanceTest *test,
745 struct EmissionTest *data = _data;
747 g_object_unref (data->object);
751 /*************************************************************
753 *************************************************************/
755 static PerformanceTest tests[] = {
757 "simple-construction",
758 simple_object_get_type,
759 test_construction_setup,
760 test_construction_init,
761 test_construction_run,
762 test_construction_finish,
763 test_construction_teardown,
764 test_construction_print_result
767 "complex-construction",
768 complex_object_get_type,
769 test_construction_setup,
770 test_construction_init,
771 test_construction_run,
772 test_construction_finish,
773 test_construction_teardown,
774 test_construction_print_result
779 test_type_check_setup,
780 test_type_check_init,
782 test_type_check_finish,
783 test_type_check_teardown,
784 test_type_check_print_result
788 GINT_TO_POINTER (COMPLEX_SIGNAL),
789 test_emission_unhandled_setup,
790 test_emission_unhandled_init,
791 test_emission_unhandled_run,
792 test_emission_unhandled_finish,
793 test_emission_unhandled_teardown,
794 test_emission_unhandled_print_result
797 "emit-unhandled-empty",
798 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
799 test_emission_unhandled_setup,
800 test_emission_unhandled_init,
801 test_emission_unhandled_run,
802 test_emission_unhandled_finish,
803 test_emission_unhandled_teardown,
804 test_emission_unhandled_print_result
807 "emit-unhandled-generic",
808 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
809 test_emission_unhandled_setup,
810 test_emission_unhandled_init,
811 test_emission_unhandled_run,
812 test_emission_unhandled_finish,
813 test_emission_unhandled_teardown,
814 test_emission_unhandled_print_result
817 "emit-unhandled-generic-empty",
818 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
819 test_emission_unhandled_setup,
820 test_emission_unhandled_init,
821 test_emission_unhandled_run,
822 test_emission_unhandled_finish,
823 test_emission_unhandled_teardown,
824 test_emission_unhandled_print_result
828 GINT_TO_POINTER (COMPLEX_SIGNAL),
829 test_emission_handled_setup,
830 test_emission_handled_init,
831 test_emission_handled_run,
832 test_emission_handled_finish,
833 test_emission_handled_teardown,
834 test_emission_handled_print_result
837 "emit-handled-empty",
838 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
839 test_emission_handled_setup,
840 test_emission_handled_init,
841 test_emission_handled_run,
842 test_emission_handled_finish,
843 test_emission_handled_teardown,
844 test_emission_handled_print_result
847 "emit-handled-generic",
848 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
849 test_emission_handled_setup,
850 test_emission_handled_init,
851 test_emission_handled_run,
852 test_emission_handled_finish,
853 test_emission_handled_teardown,
854 test_emission_handled_print_result
857 "emit-handled-generic-empty",
858 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
859 test_emission_handled_setup,
860 test_emission_handled_init,
861 test_emission_handled_run,
862 test_emission_handled_finish,
863 test_emission_handled_teardown,
864 test_emission_handled_print_result
868 static PerformanceTest *
869 find_test (const char *name)
872 for (i = 0; i < G_N_ELEMENTS (tests); i++)
874 if (strcmp (tests[i].name, name) == 0)
883 PerformanceTest *test;
884 GOptionContext *context;
885 GError *error = NULL;
888 context = g_option_context_new ("GObject performance tests");
889 g_option_context_add_main_entries (context, cmd_entries, NULL);
890 if (!g_option_context_parse (context, &argc, &argv, &error))
892 g_printerr ("%s: %s\n", argv[0], error->message);
898 for (i = 1; i < argc; i++)
900 test = find_test (argv[i]);
907 for (i = 0; i < G_N_ELEMENTS (tests); i++)
908 run_test (&tests[i]);