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, see <http://www.gnu.org/licenses/>.
20 #include <glib-object.h>
21 #include "testcommon.h"
23 #define WARM_UP_N_RUNS 50
24 #define ESTIMATE_ROUND_TIME_N_RUNS 5
25 #define DEFAULT_TEST_TIME 15 /* seconds */
26 /* The time we want each round to take, in seconds, this should
27 * be large enough compared to the timer resolution, but small
28 * enought that the risk of any random slowness will miss the
30 #define TARGET_ROUND_TIME 0.008
32 static gboolean verbose = FALSE;
33 static int test_length = DEFAULT_TEST_TIME;
35 static GOptionEntry cmd_entries[] = {
36 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
37 "Print extra information", NULL},
38 {"seconds", 's', 0, G_OPTION_ARG_INT, &test_length,
39 "Time to run each test in seconds", NULL},
43 typedef struct _PerformanceTest PerformanceTest;
44 struct _PerformanceTest {
48 gpointer (*setup) (PerformanceTest *test);
49 void (*init) (PerformanceTest *test,
52 void (*run) (PerformanceTest *test,
54 void (*finish) (PerformanceTest *test,
56 void (*teardown) (PerformanceTest *test,
58 void (*print_result) (PerformanceTest *test,
64 run_test (PerformanceTest *test)
67 guint64 i, num_rounds;
68 double elapsed, min_elapsed, max_elapsed, avg_elapsed, factor;
71 g_print ("Running test %s\n", test->name);
74 timer = g_timer_new ();
75 data = test->setup (test);
78 g_print ("Warming up\n");
80 g_timer_start (timer);
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 elapsed = g_timer_elapsed (timer, NULL);
95 g_print ("Warm up time: %.2f secs\n", elapsed);
96 g_print ("Estimating round time\n");
99 /* Estimate time for one run by doing a few test rounds */
101 for (i = 0; i < ESTIMATE_ROUND_TIME_N_RUNS; i++)
103 test->init (test, data, 1.0);
104 g_timer_start (timer);
105 test->run (test, data);
106 g_timer_stop (timer);
107 test->finish (test, data);
109 elapsed = g_timer_elapsed (timer, NULL);
111 min_elapsed = elapsed;
113 min_elapsed = MIN (min_elapsed, elapsed);
116 factor = TARGET_ROUND_TIME / min_elapsed;
119 g_print ("Uncorrected round time: %.4f msecs, correction factor %.2f\n", 1000*min_elapsed, factor);
121 /* Calculate number of rounds needed */
122 num_rounds = (test_length / TARGET_ROUND_TIME) + 1;
125 g_print ("Running %"G_GINT64_MODIFIER"d rounds\n", num_rounds);
128 for (i = 0; i < num_rounds; i++)
130 test->init (test, data, factor);
131 g_timer_start (timer);
132 test->run (test, data);
133 g_timer_stop (timer);
134 test->finish (test, data);
135 elapsed = g_timer_elapsed (timer, NULL);
138 max_elapsed = min_elapsed = avg_elapsed = elapsed;
141 min_elapsed = MIN (min_elapsed, elapsed);
142 max_elapsed = MAX (max_elapsed, elapsed);
143 avg_elapsed += elapsed;
147 avg_elapsed = avg_elapsed / num_rounds;
151 g_print ("Minimum corrected round time: %.2f msecs\n", min_elapsed * 1000);
152 g_print ("Maximum corrected round time: %.2f msecs\n", max_elapsed * 1000);
153 g_print ("Average corrected round time: %.2f msecs\n", avg_elapsed * 1000);
155 /* Print the results */
156 test->print_result (test, data, min_elapsed);
159 test->teardown (test, data);
160 g_timer_destroy (timer);
163 /*************************************************************
164 * Simple object is a very simple small GObject subclass
165 * with no properties, no signals, implementing no interfaces
166 *************************************************************/
168 static GType simple_object_get_type (void);
169 #define SIMPLE_TYPE_OBJECT (simple_object_get_type ())
170 typedef struct _SimpleObject SimpleObject;
171 typedef struct _SimpleObjectClass SimpleObjectClass;
175 GObject parent_instance;
179 struct _SimpleObjectClass
181 GObjectClass parent_class;
184 G_DEFINE_TYPE (SimpleObject, simple_object, G_TYPE_OBJECT);
187 simple_object_finalize (GObject *object)
189 G_OBJECT_CLASS (simple_object_parent_class)->finalize (object);
193 simple_object_class_init (SimpleObjectClass *class)
195 GObjectClass *object_class = G_OBJECT_CLASS (class);
197 object_class->finalize = simple_object_finalize;
201 simple_object_init (SimpleObject *simple_object)
203 simple_object->val = 42;
206 typedef struct _TestIfaceClass TestIfaceClass;
207 typedef struct _TestIfaceClass TestIface1Class;
208 typedef struct _TestIfaceClass TestIface2Class;
209 typedef struct _TestIfaceClass TestIface3Class;
210 typedef struct _TestIfaceClass TestIface4Class;
211 typedef struct _TestIfaceClass TestIface5Class;
212 typedef struct _TestIface TestIface;
214 struct _TestIfaceClass
216 GTypeInterface base_iface;
217 void (*method) (TestIface *obj);
220 static GType test_iface1_get_type (void);
221 static GType test_iface2_get_type (void);
222 static GType test_iface3_get_type (void);
223 static GType test_iface4_get_type (void);
224 static GType test_iface5_get_type (void);
226 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
227 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
228 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
229 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
230 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
232 static DEFINE_IFACE (TestIface1, test_iface1, NULL, NULL)
233 static DEFINE_IFACE (TestIface2, test_iface2, NULL, NULL)
234 static DEFINE_IFACE (TestIface3, test_iface3, NULL, NULL)
235 static DEFINE_IFACE (TestIface4, test_iface4, NULL, NULL)
236 static DEFINE_IFACE (TestIface5, test_iface5, NULL, NULL)
238 /*************************************************************
239 * Complex object is a GObject subclass with a properties,
240 * construct properties, signals and implementing an interface.
241 *************************************************************/
243 static GType complex_object_get_type (void);
244 #define COMPLEX_TYPE_OBJECT (complex_object_get_type ())
245 typedef struct _ComplexObject ComplexObject;
246 typedef struct _ComplexObjectClass ComplexObjectClass;
248 struct _ComplexObject
250 GObject parent_instance;
255 struct _ComplexObjectClass
257 GObjectClass parent_class;
259 void (*signal) (ComplexObject *obj);
260 void (*signal_empty) (ComplexObject *obj);
263 static void complex_test_iface_init (gpointer g_iface,
264 gpointer iface_data);
266 G_DEFINE_TYPE_EXTENDED (ComplexObject, complex_object,
268 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE1,
269 complex_test_iface_init)
270 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE2,
271 complex_test_iface_init)
272 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE3,
273 complex_test_iface_init)
274 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE4,
275 complex_test_iface_init)
276 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE5,
277 complex_test_iface_init)
280 #define COMPLEX_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), COMPLEX_TYPE_OBJECT, ComplexObject))
290 COMPLEX_SIGNAL_EMPTY,
291 COMPLEX_SIGNAL_GENERIC,
292 COMPLEX_SIGNAL_GENERIC_EMPTY,
297 static guint complex_signals[COMPLEX_LAST_SIGNAL] = { 0 };
300 complex_object_finalize (GObject *object)
302 G_OBJECT_CLASS (complex_object_parent_class)->finalize (object);
306 complex_object_set_property (GObject *object,
311 ComplexObject *complex = COMPLEX_OBJECT (object);
316 complex->val1 = g_value_get_int (value);
319 complex->val2 = g_value_get_int (value);
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328 complex_object_get_property (GObject *object,
333 ComplexObject *complex = COMPLEX_OBJECT (object);
338 g_value_set_int (value, complex->val1);
341 g_value_set_int (value, complex->val2);
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 complex_object_real_signal (ComplexObject *obj)
355 complex_object_class_init (ComplexObjectClass *class)
357 GObjectClass *object_class = G_OBJECT_CLASS (class);
359 object_class->finalize = complex_object_finalize;
360 object_class->set_property = complex_object_set_property;
361 object_class->get_property = complex_object_get_property;
363 class->signal = complex_object_real_signal;
365 complex_signals[COMPLEX_SIGNAL] =
366 g_signal_new ("signal",
367 G_TYPE_FROM_CLASS (object_class),
369 G_STRUCT_OFFSET (ComplexObjectClass, signal),
371 g_cclosure_marshal_VOID__VOID,
374 complex_signals[COMPLEX_SIGNAL_EMPTY] =
375 g_signal_new ("signal-empty",
376 G_TYPE_FROM_CLASS (object_class),
378 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
380 g_cclosure_marshal_VOID__VOID,
383 complex_signals[COMPLEX_SIGNAL_GENERIC] =
384 g_signal_new ("signal-generic",
385 G_TYPE_FROM_CLASS (object_class),
387 G_STRUCT_OFFSET (ComplexObjectClass, signal),
391 complex_signals[COMPLEX_SIGNAL_GENERIC_EMPTY] =
392 g_signal_new ("signal-generic-empty",
393 G_TYPE_FROM_CLASS (object_class),
395 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
400 complex_signals[COMPLEX_SIGNAL_ARGS] =
401 g_signal_new ("signal-args",
402 G_TYPE_FROM_CLASS (object_class),
404 G_STRUCT_OFFSET (ComplexObjectClass, signal),
406 g_cclosure_marshal_VOID__UINT_POINTER,
407 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_POINTER);
409 g_object_class_install_property (object_class,
411 g_param_spec_int ("val1",
417 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
418 g_object_class_install_property (object_class,
420 g_param_spec_int ("val2",
432 complex_object_iface_method (TestIface *obj)
434 ComplexObject *complex = COMPLEX_OBJECT (obj);
439 complex_test_iface_init (gpointer g_iface,
442 TestIfaceClass *iface = g_iface;
443 iface->method = complex_object_iface_method;
447 complex_object_init (ComplexObject *complex_object)
449 complex_object->val2 = 43;
452 /*************************************************************
453 * Test object construction performance
454 *************************************************************/
456 #define NUM_OBJECT_TO_CONSTRUCT 10000
458 struct ConstructionTest {
465 test_construction_setup (PerformanceTest *test)
467 struct ConstructionTest *data;
469 data = g_new0 (struct ConstructionTest, 1);
470 data->type = ((GType (*)(void))test->extra_data)();
476 test_construction_init (PerformanceTest *test,
480 struct ConstructionTest *data = _data;
483 n = NUM_OBJECT_TO_CONSTRUCT * count_factor;
484 if (data->n_objects != n)
487 data->objects = g_new (GObject *, n);
492 test_construction_run (PerformanceTest *test,
495 struct ConstructionTest *data = _data;
496 GObject **objects = data->objects;
497 GType type = data->type;
500 n_objects = data->n_objects;
501 for (i = 0; i < n_objects; i++)
502 objects[i] = g_object_new (type, NULL);
506 test_construction_finish (PerformanceTest *test,
509 struct ConstructionTest *data = _data;
512 for (i = 0; i < data->n_objects; i++)
513 g_object_unref (data->objects[i]);
517 test_construction_teardown (PerformanceTest *test,
520 struct ConstructionTest *data = _data;
521 g_free (data->objects);
526 test_construction_print_result (PerformanceTest *test,
530 struct ConstructionTest *data = _data;
532 g_print ("Millions of constructed objects per second: %.3f\n",
533 data->n_objects / (time * 1000000));
536 /*************************************************************
537 * Test runtime type check performance
538 *************************************************************/
540 #define NUM_KILO_CHECKS_PER_ROUND 50
542 struct TypeCheckTest {
548 test_type_check_setup (PerformanceTest *test)
550 struct TypeCheckTest *data;
552 data = g_new0 (struct TypeCheckTest, 1);
553 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
559 test_type_check_init (PerformanceTest *test,
563 struct TypeCheckTest *data = _data;
565 data->n_checks = factor * NUM_KILO_CHECKS_PER_ROUND;
569 /* Work around g_type_check_instance_is_a being marked "pure",
570 and thus only called once for the loop. */
571 gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
572 GType iface_type) = &g_type_check_instance_is_a;
575 test_type_check_run (PerformanceTest *test,
578 struct TypeCheckTest *data = _data;
579 volatile GObject *object = data->object;
580 volatile GType type, types[5];
583 types[0] = test_iface1_get_type ();
584 types[1] = test_iface2_get_type ();
585 types[2] = test_iface3_get_type ();
586 types[3] = test_iface4_get_type ();
587 types[4] = test_iface5_get_type ();
589 for (i = 0; i < data->n_checks; i++)
592 for (j = 0; j < 1000; j++)
594 my_type_check_instance_is_a ((GTypeInstance *)object,
601 test_type_check_finish (PerformanceTest *test,
607 test_type_check_print_result (PerformanceTest *test,
611 struct TypeCheckTest *data = _data;
612 g_print ("Million type checks per second: %.2f\n",
613 data->n_checks / (1000*time));
617 test_type_check_teardown (PerformanceTest *test,
620 struct TypeCheckTest *data = _data;
622 g_object_unref (data->object);
626 /*************************************************************
627 * Test signal emissions performance (common code)
628 *************************************************************/
630 #define NUM_EMISSIONS_PER_ROUND 10000
632 struct EmissionTest {
639 test_emission_run (PerformanceTest *test,
642 struct EmissionTest *data = _data;
643 GObject *object = data->object;
646 for (i = 0; i < data->n_checks; i++)
647 g_signal_emit (object, data->signal_id, 0);
651 test_emission_run_args (PerformanceTest *test,
654 struct EmissionTest *data = _data;
655 GObject *object = data->object;
658 for (i = 0; i < data->n_checks; i++)
659 g_signal_emit (object, data->signal_id, 0, 0, NULL);
662 /*************************************************************
663 * Test signal unhandled emissions performance
664 *************************************************************/
667 test_emission_unhandled_setup (PerformanceTest *test)
669 struct EmissionTest *data;
671 data = g_new0 (struct EmissionTest, 1);
672 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
673 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
678 test_emission_unhandled_init (PerformanceTest *test,
682 struct EmissionTest *data = _data;
684 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
688 test_emission_unhandled_finish (PerformanceTest *test,
694 test_emission_unhandled_print_result (PerformanceTest *test,
698 struct EmissionTest *data = _data;
700 g_print ("Emissions per second: %.0f\n",
701 data->n_checks / time);
705 test_emission_unhandled_teardown (PerformanceTest *test,
708 struct EmissionTest *data = _data;
710 g_object_unref (data->object);
714 /*************************************************************
715 * Test signal handled emissions performance
716 *************************************************************/
719 test_emission_handled_handler (ComplexObject *obj, gpointer data)
724 test_emission_handled_setup (PerformanceTest *test)
726 struct EmissionTest *data;
728 data = g_new0 (struct EmissionTest, 1);
729 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
730 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
731 g_signal_connect (data->object, "signal",
732 G_CALLBACK (test_emission_handled_handler),
734 g_signal_connect (data->object, "signal-empty",
735 G_CALLBACK (test_emission_handled_handler),
737 g_signal_connect (data->object, "signal-generic",
738 G_CALLBACK (test_emission_handled_handler),
740 g_signal_connect (data->object, "signal-generic-empty",
741 G_CALLBACK (test_emission_handled_handler),
743 g_signal_connect (data->object, "signal-args",
744 G_CALLBACK (test_emission_handled_handler),
751 test_emission_handled_init (PerformanceTest *test,
755 struct EmissionTest *data = _data;
757 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
761 test_emission_handled_finish (PerformanceTest *test,
767 test_emission_handled_print_result (PerformanceTest *test,
771 struct EmissionTest *data = _data;
773 g_print ("Emissions per second: %.0f\n",
774 data->n_checks / time);
778 test_emission_handled_teardown (PerformanceTest *test,
781 struct EmissionTest *data = _data;
783 g_object_unref (data->object);
787 /*************************************************************
788 * Test object refcount performance
789 *************************************************************/
791 #define NUM_KILO_REFS_PER_ROUND 100000
793 struct RefcountTest {
799 test_refcount_setup (PerformanceTest *test)
801 struct RefcountTest *data;
803 data = g_new0 (struct RefcountTest, 1);
804 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
810 test_refcount_init (PerformanceTest *test,
814 struct RefcountTest *data = _data;
816 data->n_checks = factor * NUM_KILO_REFS_PER_ROUND;
820 test_refcount_run (PerformanceTest *test,
823 struct RefcountTest *data = _data;
824 GObject *object = data->object;
827 for (i = 0; i < data->n_checks; i++)
829 g_object_ref (object);
830 g_object_ref (object);
831 g_object_ref (object);
832 g_object_unref (object);
833 g_object_unref (object);
835 g_object_ref (object);
836 g_object_ref (object);
837 g_object_unref (object);
838 g_object_unref (object);
839 g_object_unref (object);
844 test_refcount_finish (PerformanceTest *test,
850 test_refcount_print_result (PerformanceTest *test,
854 struct RefcountTest *data = _data;
855 g_print ("Million refs+unref per second: %.2f\n",
856 data->n_checks * 5 / (time * 1000000 ));
860 test_refcount_teardown (PerformanceTest *test,
863 struct RefcountTest *data = _data;
865 g_object_unref (data->object);
869 /*************************************************************
871 *************************************************************/
873 static PerformanceTest tests[] = {
875 "simple-construction",
876 simple_object_get_type,
877 test_construction_setup,
878 test_construction_init,
879 test_construction_run,
880 test_construction_finish,
881 test_construction_teardown,
882 test_construction_print_result
885 "complex-construction",
886 complex_object_get_type,
887 test_construction_setup,
888 test_construction_init,
889 test_construction_run,
890 test_construction_finish,
891 test_construction_teardown,
892 test_construction_print_result
897 test_type_check_setup,
898 test_type_check_init,
900 test_type_check_finish,
901 test_type_check_teardown,
902 test_type_check_print_result
906 GINT_TO_POINTER (COMPLEX_SIGNAL),
907 test_emission_unhandled_setup,
908 test_emission_unhandled_init,
910 test_emission_unhandled_finish,
911 test_emission_unhandled_teardown,
912 test_emission_unhandled_print_result
915 "emit-unhandled-empty",
916 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
917 test_emission_unhandled_setup,
918 test_emission_unhandled_init,
920 test_emission_unhandled_finish,
921 test_emission_unhandled_teardown,
922 test_emission_unhandled_print_result
925 "emit-unhandled-generic",
926 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
927 test_emission_unhandled_setup,
928 test_emission_unhandled_init,
930 test_emission_unhandled_finish,
931 test_emission_unhandled_teardown,
932 test_emission_unhandled_print_result
935 "emit-unhandled-generic-empty",
936 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
937 test_emission_unhandled_setup,
938 test_emission_unhandled_init,
940 test_emission_unhandled_finish,
941 test_emission_unhandled_teardown,
942 test_emission_unhandled_print_result
945 "emit-unhandled-args",
946 GINT_TO_POINTER (COMPLEX_SIGNAL_ARGS),
947 test_emission_unhandled_setup,
948 test_emission_unhandled_init,
949 test_emission_run_args,
950 test_emission_unhandled_finish,
951 test_emission_unhandled_teardown,
952 test_emission_unhandled_print_result
956 GINT_TO_POINTER (COMPLEX_SIGNAL),
957 test_emission_handled_setup,
958 test_emission_handled_init,
960 test_emission_handled_finish,
961 test_emission_handled_teardown,
962 test_emission_handled_print_result
965 "emit-handled-empty",
966 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
967 test_emission_handled_setup,
968 test_emission_handled_init,
970 test_emission_handled_finish,
971 test_emission_handled_teardown,
972 test_emission_handled_print_result
975 "emit-handled-generic",
976 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
977 test_emission_handled_setup,
978 test_emission_handled_init,
980 test_emission_handled_finish,
981 test_emission_handled_teardown,
982 test_emission_handled_print_result
985 "emit-handled-generic-empty",
986 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
987 test_emission_handled_setup,
988 test_emission_handled_init,
990 test_emission_handled_finish,
991 test_emission_handled_teardown,
992 test_emission_handled_print_result
996 GINT_TO_POINTER (COMPLEX_SIGNAL_ARGS),
997 test_emission_handled_setup,
998 test_emission_handled_init,
999 test_emission_run_args,
1000 test_emission_handled_finish,
1001 test_emission_handled_teardown,
1002 test_emission_handled_print_result
1007 test_refcount_setup,
1010 test_refcount_finish,
1011 test_refcount_teardown,
1012 test_refcount_print_result
1016 static PerformanceTest *
1017 find_test (const char *name)
1020 for (i = 0; i < G_N_ELEMENTS (tests); i++)
1022 if (strcmp (tests[i].name, name) == 0)
1031 PerformanceTest *test;
1032 GOptionContext *context;
1033 GError *error = NULL;
1036 context = g_option_context_new ("GObject performance tests");
1037 g_option_context_add_main_entries (context, cmd_entries, NULL);
1038 if (!g_option_context_parse (context, &argc, &argv, &error))
1040 g_printerr ("%s: %s\n", argv[0], error->message);
1046 for (i = 1; i < argc; i++)
1048 test = find_test (argv[i]);
1055 for (i = 0; i < G_N_ELEMENTS (tests); i++)
1056 run_test (&tests[i]);