1 #include <glib-object.h>
8 GObjectClass parent_class;
17 guint signals[LAST_SIGNAL];
19 GType my_obj_get_type (void);
21 G_DEFINE_TYPE (MyObj, my_obj, G_TYPE_OBJECT)
24 my_obj_init (MyObj *o)
29 my_obj_class_init (MyObjClass *class)
32 g_signal_new ("signal1",
33 G_TYPE_FROM_CLASS (class),
35 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
37 g_signal_new ("signal2",
38 G_TYPE_FROM_CLASS (class),
40 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
48 static guint n_handlers = 0;
51 test_connect_many (void)
57 o = g_object_new (my_obj_get_type (), NULL);
59 g_test_timer_start ();
61 for (i = 0; i < n_handlers; i++)
62 g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
64 time_elapsed = g_test_timer_elapsed ();
68 g_test_minimized_result (time_elapsed, "connected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
72 test_disconnect_many_ordered (void)
79 handlers = g_malloc_n (n_handlers, sizeof (*handlers));
80 o = g_object_new (my_obj_get_type (), NULL);
82 for (i = 0; i < n_handlers; i++)
83 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
85 g_test_timer_start ();
87 for (i = 0; i < n_handlers; i++)
88 g_signal_handler_disconnect (o, handlers[i]);
90 time_elapsed = g_test_timer_elapsed ();
95 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
99 test_disconnect_many_inverse (void)
103 gdouble time_elapsed;
106 handlers = g_malloc_n (n_handlers, sizeof (*handlers));
107 o = g_object_new (my_obj_get_type (), NULL);
109 for (i = 0; i < n_handlers; i++)
110 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
112 g_test_timer_start ();
114 for (i = n_handlers; i > 0; i--)
115 g_signal_handler_disconnect (o, handlers[i - 1]);
117 time_elapsed = g_test_timer_elapsed ();
122 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
126 test_disconnect_many_random (void)
131 gdouble time_elapsed;
134 handlers = g_malloc_n (n_handlers, sizeof (*handlers));
135 o = g_object_new (my_obj_get_type (), NULL);
137 for (i = 0; i < n_handlers; i++)
138 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
140 for (i = 0; i < n_handlers; i++)
142 j = g_test_rand_int_range (0, n_handlers);
144 handlers[i] = handlers[j];
148 g_test_timer_start ();
150 for (i = 0; i < n_handlers; i++)
151 g_signal_handler_disconnect (o, handlers[i]);
153 time_elapsed = g_test_timer_elapsed ();
158 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
162 test_disconnect_2_signals (void)
167 gdouble time_elapsed;
170 handlers = g_malloc_n (n_handlers, sizeof (*handlers));
171 o = g_object_new (my_obj_get_type (), NULL);
173 for (i = 0; i < n_handlers; i++)
176 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
178 handlers[i] = g_signal_connect (o, "signal2", G_CALLBACK (nop), NULL);
181 for (i = 0; i < n_handlers; i++)
183 j = g_test_rand_int_range (0, n_handlers);
185 handlers[i] = handlers[j];
189 g_test_timer_start ();
191 for (i = 0; i < n_handlers; i++)
192 g_signal_handler_disconnect (o, handlers[i]);
194 time_elapsed = g_test_timer_elapsed ();
199 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
203 test_disconnect_2_objects (void)
209 gdouble time_elapsed;
212 handlers = g_malloc_n (n_handlers, sizeof (*handlers));
213 objects = g_malloc_n (n_handlers, sizeof (*objects));
214 o1 = g_object_new (my_obj_get_type (), NULL);
215 o2 = g_object_new (my_obj_get_type (), NULL);
217 for (i = 0; i < n_handlers; i++)
221 handlers[i] = g_signal_connect (o1, "signal1", G_CALLBACK (nop), NULL);
226 handlers[i] = g_signal_connect (o2, "signal1", G_CALLBACK (nop), NULL);
231 for (i = 0; i < n_handlers; i++)
233 j = g_test_rand_int_range (0, n_handlers);
235 handlers[i] = handlers[j];
238 objects[i] = objects[j];
242 g_test_timer_start ();
244 for (i = 0; i < n_handlers; i++)
245 g_signal_handler_disconnect (objects[i], handlers[i]);
247 time_elapsed = g_test_timer_elapsed ();
254 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
258 test_block_many (void)
263 gdouble time_elapsed;
266 handlers = g_malloc_n (n_handlers, sizeof (*handlers));
267 o = g_object_new (my_obj_get_type (), NULL);
269 for (i = 0; i < n_handlers; i++)
270 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
272 for (i = 0; i < n_handlers; i++)
274 j = g_test_rand_int_range (0, n_handlers);
276 handlers[i] = handlers[j];
280 g_test_timer_start ();
282 for (i = 0; i < n_handlers; i++)
283 g_signal_handler_block (o, handlers[i]);
285 for (i = n_handlers; i > 0; i--)
286 g_signal_handler_unblock (o, handlers[i - 1]);
288 time_elapsed = g_test_timer_elapsed ();
293 g_test_minimized_result (time_elapsed, "blocked and unblocked %u handlers in %6.3f seconds", n_handlers, time_elapsed);
297 main (int argc, char *argv[])
299 g_test_init (&argc, &argv, NULL);
301 n_handlers = g_test_perf () ? 500000 : 1;
303 g_test_add_func ("/signal/handler/connect-many", test_connect_many);
304 g_test_add_func ("/signal/handler/disconnect-many-ordered", test_disconnect_many_ordered);
305 g_test_add_func ("/signal/handler/disconnect-many-inverse", test_disconnect_many_inverse);
306 g_test_add_func ("/signal/handler/disconnect-many-random", test_disconnect_many_random);
307 g_test_add_func ("/signal/handler/disconnect-2-signals", test_disconnect_2_signals);
308 g_test_add_func ("/signal/handler/disconnect-2-objects", test_disconnect_2_objects);
309 g_test_add_func ("/signal/handler/block-many", test_block_many);
311 return g_test_run ();