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 unhandled emissions performance
603 *************************************************************/
605 #define NUM_EMISSIONS_PER_ROUND 10000
607 struct EmissionTest {
614 test_emission_unhandled_setup (PerformanceTest *test)
616 struct EmissionTest *data;
618 data = g_new0 (struct EmissionTest, 1);
619 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
620 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
625 test_emission_unhandled_init (PerformanceTest *test,
629 struct EmissionTest *data = _data;
631 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
635 test_emission_unhandled_run (PerformanceTest *test,
638 struct EmissionTest *data = _data;
639 GObject *object = data->object;
642 for (i = 0; i < data->n_checks; i++)
643 g_signal_emit (object,
649 test_emission_unhandled_finish (PerformanceTest *test,
655 test_emission_unhandled_print_result (PerformanceTest *test,
659 struct EmissionTest *data = _data;
661 g_print ("Emissions per second: %.0f\n",
662 data->n_checks / time);
666 test_emission_unhandled_teardown (PerformanceTest *test,
669 struct EmissionTest *data = _data;
671 g_object_unref (data->object);
675 /*************************************************************
676 * Test signal handled emissions performance
677 *************************************************************/
680 test_emission_handled_handler (ComplexObject *obj, gpointer data)
685 test_emission_handled_setup (PerformanceTest *test)
687 struct EmissionTest *data;
689 data = g_new0 (struct EmissionTest, 1);
690 data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
691 data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
692 g_signal_connect (data->object, "signal",
693 G_CALLBACK (test_emission_handled_handler),
695 g_signal_connect (data->object, "signal-empty",
696 G_CALLBACK (test_emission_handled_handler),
698 g_signal_connect (data->object, "signal-generic",
699 G_CALLBACK (test_emission_handled_handler),
701 g_signal_connect (data->object, "signal-generic-empty",
702 G_CALLBACK (test_emission_handled_handler),
709 test_emission_handled_init (PerformanceTest *test,
713 struct EmissionTest *data = _data;
715 data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
719 test_emission_handled_run (PerformanceTest *test,
722 struct EmissionTest *data = _data;
723 GObject *object = data->object;
726 for (i = 0; i < data->n_checks; i++)
727 g_signal_emit (object,
733 test_emission_handled_finish (PerformanceTest *test,
739 test_emission_handled_print_result (PerformanceTest *test,
743 struct EmissionTest *data = _data;
745 g_print ("Emissions per second: %.0f\n",
746 data->n_checks / time);
750 test_emission_handled_teardown (PerformanceTest *test,
753 struct EmissionTest *data = _data;
755 g_object_unref (data->object);
759 /*************************************************************
761 *************************************************************/
763 static PerformanceTest tests[] = {
765 "simple-construction",
766 simple_object_get_type,
767 test_construction_setup,
768 test_construction_init,
769 test_construction_run,
770 test_construction_finish,
771 test_construction_teardown,
772 test_construction_print_result
775 "complex-construction",
776 complex_object_get_type,
777 test_construction_setup,
778 test_construction_init,
779 test_construction_run,
780 test_construction_finish,
781 test_construction_teardown,
782 test_construction_print_result
787 test_type_check_setup,
788 test_type_check_init,
790 test_type_check_finish,
791 test_type_check_teardown,
792 test_type_check_print_result
796 GINT_TO_POINTER (COMPLEX_SIGNAL),
797 test_emission_unhandled_setup,
798 test_emission_unhandled_init,
799 test_emission_unhandled_run,
800 test_emission_unhandled_finish,
801 test_emission_unhandled_teardown,
802 test_emission_unhandled_print_result
805 "emit-unhandled-empty",
806 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
807 test_emission_unhandled_setup,
808 test_emission_unhandled_init,
809 test_emission_unhandled_run,
810 test_emission_unhandled_finish,
811 test_emission_unhandled_teardown,
812 test_emission_unhandled_print_result
815 "emit-unhandled-generic",
816 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
817 test_emission_unhandled_setup,
818 test_emission_unhandled_init,
819 test_emission_unhandled_run,
820 test_emission_unhandled_finish,
821 test_emission_unhandled_teardown,
822 test_emission_unhandled_print_result
825 "emit-unhandled-generic-empty",
826 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
827 test_emission_unhandled_setup,
828 test_emission_unhandled_init,
829 test_emission_unhandled_run,
830 test_emission_unhandled_finish,
831 test_emission_unhandled_teardown,
832 test_emission_unhandled_print_result
836 GINT_TO_POINTER (COMPLEX_SIGNAL),
837 test_emission_handled_setup,
838 test_emission_handled_init,
839 test_emission_handled_run,
840 test_emission_handled_finish,
841 test_emission_handled_teardown,
842 test_emission_handled_print_result
845 "emit-handled-empty",
846 GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
847 test_emission_handled_setup,
848 test_emission_handled_init,
849 test_emission_handled_run,
850 test_emission_handled_finish,
851 test_emission_handled_teardown,
852 test_emission_handled_print_result
855 "emit-handled-generic",
856 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
857 test_emission_handled_setup,
858 test_emission_handled_init,
859 test_emission_handled_run,
860 test_emission_handled_finish,
861 test_emission_handled_teardown,
862 test_emission_handled_print_result
865 "emit-handled-generic-empty",
866 GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
867 test_emission_handled_setup,
868 test_emission_handled_init,
869 test_emission_handled_run,
870 test_emission_handled_finish,
871 test_emission_handled_teardown,
872 test_emission_handled_print_result
876 static PerformanceTest *
877 find_test (const char *name)
880 for (i = 0; i < G_N_ELEMENTS (tests); i++)
882 if (strcmp (tests[i].name, name) == 0)
891 PerformanceTest *test;
892 GOptionContext *context;
893 GError *error = NULL;
896 context = g_option_context_new ("GObject performance tests");
897 g_option_context_add_main_entries (context, cmd_entries, NULL);
898 if (!g_option_context_parse (context, &argc, &argv, &error))
900 g_printerr ("%s: %s\n", argv[0], error->message);
906 for (i = 1; i < argc; i++)
908 test = find_test (argv[i]);
915 for (i = 0; i < G_N_ELEMENTS (tests); i++)
916 run_test (&tests[i]);