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.1 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 * enough 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);
131 for (i = 0; i < num_rounds; i++)
133 test->init (test, data, factor);
134 g_timer_start (timer);
135 test->run (test, data);
136 g_timer_stop (timer);
137 test->finish (test, data);
138 elapsed = g_timer_elapsed (timer, NULL);
141 max_elapsed = min_elapsed = avg_elapsed = elapsed;
144 min_elapsed = MIN (min_elapsed, elapsed);
145 max_elapsed = MAX (max_elapsed, elapsed);
146 avg_elapsed += elapsed;
151 avg_elapsed = avg_elapsed / num_rounds;
155 g_print ("Minimum corrected round time: %.2f msecs\n", min_elapsed * 1000);
156 g_print ("Maximum corrected round time: %.2f msecs\n", max_elapsed * 1000);
157 g_print ("Average corrected round time: %.2f msecs\n", avg_elapsed * 1000);
160 /* Print the results */
161 test->print_result (test, data, min_elapsed);
164 test->teardown (test, data);
165 g_timer_destroy (timer);
168 /*************************************************************
169 * Simple object is a very simple small GObject subclass
170 * with no properties, no signals, implementing no interfaces
171 *************************************************************/
173 static GType simple_object_get_type (void);
174 #define SIMPLE_TYPE_OBJECT (simple_object_get_type ())
175 typedef struct _SimpleObject SimpleObject;
176 typedef struct _SimpleObjectClass SimpleObjectClass;
180 GObject parent_instance;
184 struct _SimpleObjectClass
186 GObjectClass parent_class;
189 G_DEFINE_TYPE (SimpleObject, simple_object, G_TYPE_OBJECT)
192 simple_object_finalize (GObject *object)
194 G_OBJECT_CLASS (simple_object_parent_class)->finalize (object);
198 simple_object_class_init (SimpleObjectClass *class)
200 GObjectClass *object_class = G_OBJECT_CLASS (class);
202 object_class->finalize = simple_object_finalize;
206 simple_object_init (SimpleObject *simple_object)
208 simple_object->val = 42;
211 typedef struct _TestIfaceClass TestIfaceClass;
212 typedef struct _TestIfaceClass TestIface1Class;
213 typedef struct _TestIfaceClass TestIface2Class;
214 typedef struct _TestIfaceClass TestIface3Class;
215 typedef struct _TestIfaceClass TestIface4Class;
216 typedef struct _TestIfaceClass TestIface5Class;
217 typedef struct _TestIface TestIface;
219 struct _TestIfaceClass
221 GTypeInterface base_iface;
222 void (*method) (TestIface *obj);
225 static GType test_iface1_get_type (void);
226 static GType test_iface2_get_type (void);
227 static GType test_iface3_get_type (void);
228 static GType test_iface4_get_type (void);
229 static GType test_iface5_get_type (void);
231 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
232 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
233 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
234 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
235 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
237 static DEFINE_IFACE (TestIface1, test_iface1, NULL, NULL)
238 static DEFINE_IFACE (TestIface2, test_iface2, NULL, NULL)
239 static DEFINE_IFACE (TestIface3, test_iface3, NULL, NULL)
240 static DEFINE_IFACE (TestIface4, test_iface4, NULL, NULL)
241 static DEFINE_IFACE (TestIface5, test_iface5, NULL, NULL)
243 /*************************************************************
244 * Complex object is a GObject subclass with a properties,
245 * construct properties, signals and implementing an interface.
246 *************************************************************/
248 static GType complex_object_get_type (void);
249 #define COMPLEX_TYPE_OBJECT (complex_object_get_type ())
250 typedef struct _ComplexObject ComplexObject;
251 typedef struct _ComplexObjectClass ComplexObjectClass;
253 struct _ComplexObject
255 GObject parent_instance;
260 struct _ComplexObjectClass
262 GObjectClass parent_class;
264 void (*signal) (ComplexObject *obj);
265 void (*signal_empty) (ComplexObject *obj);
268 static void complex_test_iface_init (gpointer g_iface,
269 gpointer iface_data);
271 G_DEFINE_TYPE_EXTENDED (ComplexObject, complex_object,
273 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE1, complex_test_iface_init)
274 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE2, complex_test_iface_init)
275 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE3, complex_test_iface_init)
276 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE4, complex_test_iface_init)
277 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE5, complex_test_iface_init))
279 #define COMPLEX_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), COMPLEX_TYPE_OBJECT, ComplexObject))
289 COMPLEX_SIGNAL_EMPTY,
290 COMPLEX_SIGNAL_GENERIC,
291 COMPLEX_SIGNAL_GENERIC_EMPTY,
296 static guint complex_signals[COMPLEX_LAST_SIGNAL] = { 0 };
299 complex_object_finalize (GObject *object)
301 G_OBJECT_CLASS (complex_object_parent_class)->finalize (object);
305 complex_object_set_property (GObject *object,
310 ComplexObject *complex = COMPLEX_OBJECT (object);
315 complex->val1 = g_value_get_int (value);
318 complex->val2 = g_value_get_int (value);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 complex_object_get_property (GObject *object,
332 ComplexObject *complex = COMPLEX_OBJECT (object);
337 g_value_set_int (value, complex->val1);
340 g_value_set_int (value, complex->val2);
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349 complex_object_real_signal (ComplexObject *obj)
354 complex_object_class_init (ComplexObjectClass *class)
356 GObjectClass *object_class = G_OBJECT_CLASS (class);
358 object_class->finalize = complex_object_finalize;
359 object_class->set_property = complex_object_set_property;
360 object_class->get_property = complex_object_get_property;
362 class->signal = complex_object_real_signal;
364 complex_signals[COMPLEX_SIGNAL] =
365 g_signal_new ("signal",
366 G_TYPE_FROM_CLASS (object_class),
368 G_STRUCT_OFFSET (ComplexObjectClass, signal),
370 g_cclosure_marshal_VOID__VOID,
373 complex_signals[COMPLEX_SIGNAL_EMPTY] =
374 g_signal_new ("signal-empty",
375 G_TYPE_FROM_CLASS (object_class),
377 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
379 g_cclosure_marshal_VOID__VOID,
382 complex_signals[COMPLEX_SIGNAL_GENERIC] =
383 g_signal_new ("signal-generic",
384 G_TYPE_FROM_CLASS (object_class),
386 G_STRUCT_OFFSET (ComplexObjectClass, signal),
390 complex_signals[COMPLEX_SIGNAL_GENERIC_EMPTY] =
391 g_signal_new ("signal-generic-empty",
392 G_TYPE_FROM_CLASS (object_class),
394 G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
399 complex_signals[COMPLEX_SIGNAL_ARGS] =
400 g_signal_new ("signal-args",
401 G_TYPE_FROM_CLASS (object_class),
403 G_STRUCT_OFFSET (ComplexObjectClass, signal),
405 g_cclosure_marshal_VOID__UINT_POINTER,
406 G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_POINTER);
408 g_object_class_install_property (object_class,
410 g_param_spec_int ("val1",
416 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
417 g_object_class_install_property (object_class,
419 g_param_spec_int ("val2",
431 complex_object_iface_method (TestIface *obj)
433 ComplexObject *complex = COMPLEX_OBJECT (obj);
438 complex_test_iface_init (gpointer g_iface,
441 TestIfaceClass *iface = g_iface;
442 iface->method = complex_object_iface_method;
446 complex_object_init (ComplexObject *complex_object)
448 complex_object->val2 = 43;
451 /*************************************************************
452 * Test object construction performance
453 *************************************************************/
455 #define NUM_OBJECT_TO_CONSTRUCT 10000
457 struct ConstructionTest {
464 test_construction_setup (PerformanceTest *test)
466 struct ConstructionTest *data;
468 data = g_new0 (struct ConstructionTest, 1);
469 data->type = ((GType (*)(void))test->extra_data)();
475 test_construction_init (PerformanceTest *test,
479 struct ConstructionTest *data = _data;
482 n = NUM_OBJECT_TO_CONSTRUCT * count_factor;
483 if (data->n_objects != n)
486 data->objects = g_new (GObject *, n);
491 test_construction_run (PerformanceTest *test,
494 struct ConstructionTest *data = _data;
495 GObject **objects = data->objects;
496 GType type = data->type;
499 n_objects = data->n_objects;
500 for (i = 0; i < n_objects; i++)
501 objects[i] = g_object_new (type, NULL);
505 test_construction_finish (PerformanceTest *test,
508 struct ConstructionTest *data = _data;
511 for (i = 0; i < data->n_objects; i++)
512 g_object_unref (data->objects[i]);
516 test_construction_teardown (PerformanceTest *test,
519 struct ConstructionTest *data = _data;
520 g_free (data->objects);
525 test_construction_print_result (PerformanceTest *test,
529 struct ConstructionTest *data = _data;
531 g_print ("Millions of constructed objects per second: %.3f\n",
532 data->n_objects / (time * 1000000));
535 /*************************************************************
536 * Test runtime type check performance
537 *************************************************************/
539 #define NUM_KILO_CHECKS_PER_ROUND 50
541 struct TypeCheckTest {
547 test_type_check_setup (PerformanceTest *test)
549 struct TypeCheckTest *data;
551 data = g_new0 (struct TypeCheckTest, 1);
552 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
558 test_type_check_init (PerformanceTest *test,
562 struct TypeCheckTest *data = _data;
564 data->n_checks = factor * NUM_KILO_CHECKS_PER_ROUND;
568 /* Work around g_type_check_instance_is_a being marked "pure",
569 and thus only called once for the loop. */
570 gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
571 GType iface_type) = &g_type_check_instance_is_a;
574 test_type_check_run (PerformanceTest *test,
577 struct TypeCheckTest *data = _data;
578 GObject *object = data->object;
579 GType type, types[5];
582 types[0] = test_iface1_get_type ();
583 types[1] = test_iface2_get_type ();
584 types[2] = test_iface3_get_type ();
585 types[3] = test_iface4_get_type ();
586 types[4] = test_iface5_get_type ();
588 for (i = 0; i < data->n_checks; i++)
591 for (j = 0; j < 1000; j++)
593 my_type_check_instance_is_a ((GTypeInstance *)object,
600 test_type_check_finish (PerformanceTest *test,
606 test_type_check_print_result (PerformanceTest *test,
610 struct TypeCheckTest *data = _data;
611 g_print ("Million type checks per second: %.2f\n",
612 data->n_checks / (1000*time));
616 test_type_check_teardown (PerformanceTest *test,
619 struct TypeCheckTest *data = _data;
621 g_object_unref (data->object);
625 /*************************************************************
626 * Test signal emissions performance (common code)
627 *************************************************************/
629 #define NUM_EMISSIONS_PER_ROUND 10000
631 struct EmissionTest {
638 test_emission_run (PerformanceTest *test,
641 struct EmissionTest *data = _data;
642 GObject *object = data->object;
645 for (i = 0; i < data->n_checks; i++)
646 g_signal_emit (object, data->signal_id, 0);
650 test_emission_run_args (PerformanceTest *test,
653 struct EmissionTest *data = _data;
654 GObject *object = data->object;
657 for (i = 0; i < data->n_checks; i++)
658 g_signal_emit (object, data->signal_id, 0, 0, NULL);
661 /*************************************************************
662 * Test signal unhandled emissions performance
663 *************************************************************/
666 test_emission_unhandled_setup (PerformanceTest *test)
668 struct EmissionTest *data;
670 data = g_new0 (struct EmissionTest, 1);
671 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
672 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
677 test_emission_unhandled_init (PerformanceTest *test,
681 struct EmissionTest *data = _data;
683 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
687 test_emission_unhandled_finish (PerformanceTest *test,
693 test_emission_unhandled_print_result (PerformanceTest *test,
697 struct EmissionTest *data = _data;
699 g_print ("Emissions per second: %.0f\n",
700 data->n_checks / time);
704 test_emission_unhandled_teardown (PerformanceTest *test,
707 struct EmissionTest *data = _data;
709 g_object_unref (data->object);
713 /*************************************************************
714 * Test signal handled emissions performance
715 *************************************************************/
718 test_emission_handled_handler (ComplexObject *obj, gpointer data)
723 test_emission_handled_setup (PerformanceTest *test)
725 struct EmissionTest *data;
727 data = g_new0 (struct EmissionTest, 1);
728 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
729 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
730 g_signal_connect (data->object, "signal",
731 G_CALLBACK (test_emission_handled_handler),
733 g_signal_connect (data->object, "signal-empty",
734 G_CALLBACK (test_emission_handled_handler),
736 g_signal_connect (data->object, "signal-generic",
737 G_CALLBACK (test_emission_handled_handler),
739 g_signal_connect (data->object, "signal-generic-empty",
740 G_CALLBACK (test_emission_handled_handler),
742 g_signal_connect (data->object, "signal-args",
743 G_CALLBACK (test_emission_handled_handler),
750 test_emission_handled_init (PerformanceTest *test,
754 struct EmissionTest *data = _data;
756 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
760 test_emission_handled_finish (PerformanceTest *test,
766 test_emission_handled_print_result (PerformanceTest *test,
770 struct EmissionTest *data = _data;
772 g_print ("Emissions per second: %.0f\n",
773 data->n_checks / time);
777 test_emission_handled_teardown (PerformanceTest *test,
780 struct EmissionTest *data = _data;
782 g_object_unref (data->object);
786 /*************************************************************
787 * Test object refcount performance
788 *************************************************************/
790 #define NUM_KILO_REFS_PER_ROUND 100000
792 struct RefcountTest {
798 test_refcount_setup (PerformanceTest *test)
800 struct RefcountTest *data;
802 data = g_new0 (struct RefcountTest, 1);
803 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
809 test_refcount_init (PerformanceTest *test,
813 struct RefcountTest *data = _data;
815 data->n_checks = factor * NUM_KILO_REFS_PER_ROUND;
819 test_refcount_run (PerformanceTest *test,
822 struct RefcountTest *data = _data;
823 GObject *object = data->object;
826 for (i = 0; i < data->n_checks; i++)
828 g_object_ref (object);
829 g_object_ref (object);
830 g_object_ref (object);
831 g_object_unref (object);
832 g_object_unref (object);
834 g_object_ref (object);
835 g_object_ref (object);
836 g_object_unref (object);
837 g_object_unref (object);
838 g_object_unref (object);
843 test_refcount_finish (PerformanceTest *test,
849 test_refcount_print_result (PerformanceTest *test,
853 struct RefcountTest *data = _data;
854 g_print ("Million refs+unref per second: %.2f\n",
855 data->n_checks * 5 / (time * 1000000 ));
859 test_refcount_teardown (PerformanceTest *test,
862 struct RefcountTest *data = _data;
864 g_object_unref (data->object);
868 /*************************************************************
870 *************************************************************/
872 static PerformanceTest tests[] = {
874 "simple-construction",
875 simple_object_get_type,
876 test_construction_setup,
877 test_construction_init,
878 test_construction_run,
879 test_construction_finish,
880 test_construction_teardown,
881 test_construction_print_result
884 "complex-construction",
885 complex_object_get_type,
886 test_construction_setup,
887 test_construction_init,
888 test_construction_run,
889 test_construction_finish,
890 test_construction_teardown,
891 test_construction_print_result
896 test_type_check_setup,
897 test_type_check_init,
899 test_type_check_finish,
900 test_type_check_teardown,
901 test_type_check_print_result
905 GINT_TO_POINTER (COMPLEX_SIGNAL),
906 test_emission_unhandled_setup,
907 test_emission_unhandled_init,
909 test_emission_unhandled_finish,
910 test_emission_unhandled_teardown,
911 test_emission_unhandled_print_result
914 "emit-unhandled-empty",
915 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
916 test_emission_unhandled_setup,
917 test_emission_unhandled_init,
919 test_emission_unhandled_finish,
920 test_emission_unhandled_teardown,
921 test_emission_unhandled_print_result
924 "emit-unhandled-generic",
925 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
926 test_emission_unhandled_setup,
927 test_emission_unhandled_init,
929 test_emission_unhandled_finish,
930 test_emission_unhandled_teardown,
931 test_emission_unhandled_print_result
934 "emit-unhandled-generic-empty",
935 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
936 test_emission_unhandled_setup,
937 test_emission_unhandled_init,
939 test_emission_unhandled_finish,
940 test_emission_unhandled_teardown,
941 test_emission_unhandled_print_result
944 "emit-unhandled-args",
945 GINT_TO_POINTER (COMPLEX_SIGNAL_ARGS),
946 test_emission_unhandled_setup,
947 test_emission_unhandled_init,
948 test_emission_run_args,
949 test_emission_unhandled_finish,
950 test_emission_unhandled_teardown,
951 test_emission_unhandled_print_result
955 GINT_TO_POINTER (COMPLEX_SIGNAL),
956 test_emission_handled_setup,
957 test_emission_handled_init,
959 test_emission_handled_finish,
960 test_emission_handled_teardown,
961 test_emission_handled_print_result
964 "emit-handled-empty",
965 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
966 test_emission_handled_setup,
967 test_emission_handled_init,
969 test_emission_handled_finish,
970 test_emission_handled_teardown,
971 test_emission_handled_print_result
974 "emit-handled-generic",
975 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
976 test_emission_handled_setup,
977 test_emission_handled_init,
979 test_emission_handled_finish,
980 test_emission_handled_teardown,
981 test_emission_handled_print_result
984 "emit-handled-generic-empty",
985 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
986 test_emission_handled_setup,
987 test_emission_handled_init,
989 test_emission_handled_finish,
990 test_emission_handled_teardown,
991 test_emission_handled_print_result
995 GINT_TO_POINTER (COMPLEX_SIGNAL_ARGS),
996 test_emission_handled_setup,
997 test_emission_handled_init,
998 test_emission_run_args,
999 test_emission_handled_finish,
1000 test_emission_handled_teardown,
1001 test_emission_handled_print_result
1006 test_refcount_setup,
1009 test_refcount_finish,
1010 test_refcount_teardown,
1011 test_refcount_print_result
1015 static PerformanceTest *
1016 find_test (const char *name)
1019 for (i = 0; i < G_N_ELEMENTS (tests); i++)
1021 if (strcmp (tests[i].name, name) == 0)
1030 PerformanceTest *test;
1031 GOptionContext *context;
1032 GError *error = NULL;
1035 context = g_option_context_new ("GObject performance tests");
1036 g_option_context_add_main_entries (context, cmd_entries, NULL);
1037 if (!g_option_context_parse (context, &argc, &argv, &error))
1039 g_printerr ("%s: %s\n", argv[0], error->message);
1045 for (i = 1; i < argc; i++)
1047 test = find_test (argv[i]);
1055 for (k = 0; k < G_N_ELEMENTS (tests); k++)
1056 run_test (&tests[k]);