tests: Allow signal-handler performance tests to be smoketested

As with the previous commit, allow them to be smoketested when run
without `-m perf`.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-06-14 15:24:17 +01:00
parent 421c238e81
commit 1aaf1e7de1

View File

@ -45,96 +45,101 @@ nop (void)
{ {
} }
#define HANDLERS 500000 static guint n_handlers = 0;
static void static void
test_connect_many (void) test_connect_many (void)
{ {
MyObj *o; MyObj *o;
gdouble time_elapsed; gdouble time_elapsed;
gint i; guint i;
o = g_object_new (my_obj_get_type (), NULL); o = g_object_new (my_obj_get_type (), NULL);
g_test_timer_start (); g_test_timer_start ();
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL); g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o); g_object_unref (o);
g_test_minimized_result (time_elapsed, "connected %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "connected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
static void static void
test_disconnect_many_ordered (void) test_disconnect_many_ordered (void)
{ {
MyObj *o; MyObj *o;
gulong handlers[HANDLERS]; gulong *handlers;
gdouble time_elapsed; gdouble time_elapsed;
gint i; guint i;
handlers = g_malloc_n (n_handlers, sizeof (*handlers));
o = g_object_new (my_obj_get_type (), NULL); o = g_object_new (my_obj_get_type (), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL); handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
g_test_timer_start (); g_test_timer_start ();
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
g_signal_handler_disconnect (o, handlers[i]); g_signal_handler_disconnect (o, handlers[i]);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o); g_object_unref (o);
g_free (handlers);
g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
static void static void
test_disconnect_many_inverse (void) test_disconnect_many_inverse (void)
{ {
MyObj *o; MyObj *o;
gulong handlers[HANDLERS]; gulong *handlers;
gdouble time_elapsed; gdouble time_elapsed;
gint i; guint i;
handlers = g_malloc_n (n_handlers, sizeof (*handlers));
o = g_object_new (my_obj_get_type (), NULL); o = g_object_new (my_obj_get_type (), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL); handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
g_test_timer_start (); g_test_timer_start ();
for (i = HANDLERS - 1; i >= 0; i--) for (i = n_handlers; i > 0; i--)
g_signal_handler_disconnect (o, handlers[i]); g_signal_handler_disconnect (o, handlers[i - 1]);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o); g_object_unref (o);
g_free (handlers);
g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
static void static void
test_disconnect_many_random (void) test_disconnect_many_random (void)
{ {
MyObj *o; MyObj *o;
gulong handlers[HANDLERS]; gulong *handlers;
gulong id; gulong id;
gdouble time_elapsed; gdouble time_elapsed;
gint i, j; guint i, j;
handlers = g_malloc_n (n_handlers, sizeof (*handlers));
o = g_object_new (my_obj_get_type (), NULL); o = g_object_new (my_obj_get_type (), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL); handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
{ {
j = g_test_rand_int_range (0, HANDLERS); j = g_test_rand_int_range (0, n_handlers);
id = handlers[i]; id = handlers[i];
handlers[i] = handlers[j]; handlers[i] = handlers[j];
handlers[j] = id; handlers[j] = id;
@ -142,28 +147,30 @@ test_disconnect_many_random (void)
g_test_timer_start (); g_test_timer_start ();
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
g_signal_handler_disconnect (o, handlers[i]); g_signal_handler_disconnect (o, handlers[i]);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o); g_object_unref (o);
g_free (handlers);
g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
static void static void
test_disconnect_2_signals (void) test_disconnect_2_signals (void)
{ {
MyObj *o; MyObj *o;
gulong handlers[HANDLERS]; gulong *handlers;
gulong id; gulong id;
gdouble time_elapsed; gdouble time_elapsed;
gint i, j; guint i, j;
handlers = g_malloc_n (n_handlers, sizeof (*handlers));
o = g_object_new (my_obj_get_type (), NULL); o = g_object_new (my_obj_get_type (), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
{ {
if (i % 2 == 0) if (i % 2 == 0)
handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL); handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
@ -171,9 +178,9 @@ test_disconnect_2_signals (void)
handlers[i] = g_signal_connect (o, "signal2", G_CALLBACK (nop), NULL); handlers[i] = g_signal_connect (o, "signal2", G_CALLBACK (nop), NULL);
} }
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
{ {
j = g_test_rand_int_range (0, HANDLERS); j = g_test_rand_int_range (0, n_handlers);
id = handlers[i]; id = handlers[i];
handlers[i] = handlers[j]; handlers[i] = handlers[j];
handlers[j] = id; handlers[j] = id;
@ -181,30 +188,33 @@ test_disconnect_2_signals (void)
g_test_timer_start (); g_test_timer_start ();
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
g_signal_handler_disconnect (o, handlers[i]); g_signal_handler_disconnect (o, handlers[i]);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o); g_object_unref (o);
g_free (handlers);
g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
static void static void
test_disconnect_2_objects (void) test_disconnect_2_objects (void)
{ {
MyObj *o1, *o2, *o; MyObj *o1, *o2, *o;
gulong handlers[HANDLERS]; gulong *handlers;
MyObj *objects[HANDLERS]; MyObj **objects;
gulong id; gulong id;
gdouble time_elapsed; gdouble time_elapsed;
gint i, j; guint i, j;
handlers = g_malloc_n (n_handlers, sizeof (*handlers));
objects = g_malloc_n (n_handlers, sizeof (*objects));
o1 = g_object_new (my_obj_get_type (), NULL); o1 = g_object_new (my_obj_get_type (), NULL);
o2 = g_object_new (my_obj_get_type (), NULL); o2 = g_object_new (my_obj_get_type (), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
{ {
if (i % 2 == 0) if (i % 2 == 0)
{ {
@ -218,9 +228,9 @@ test_disconnect_2_objects (void)
} }
} }
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
{ {
j = g_test_rand_int_range (0, HANDLERS); j = g_test_rand_int_range (0, n_handlers);
id = handlers[i]; id = handlers[i];
handlers[i] = handlers[j]; handlers[i] = handlers[j];
handlers[j] = id; handlers[j] = id;
@ -231,34 +241,37 @@ test_disconnect_2_objects (void)
g_test_timer_start (); g_test_timer_start ();
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
g_signal_handler_disconnect (objects[i], handlers[i]); g_signal_handler_disconnect (objects[i], handlers[i]);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o1); g_object_unref (o1);
g_object_unref (o2); g_object_unref (o2);
g_free (objects);
g_free (handlers);
g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
static void static void
test_block_many (void) test_block_many (void)
{ {
MyObj *o; MyObj *o;
gulong handlers[HANDLERS]; gulong *handlers;
gulong id; gulong id;
gdouble time_elapsed; gdouble time_elapsed;
gint i, j; guint i, j;
handlers = g_malloc_n (n_handlers, sizeof (*handlers));
o = g_object_new (my_obj_get_type (), NULL); o = g_object_new (my_obj_get_type (), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL); handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
{ {
j = g_test_rand_int_range (0, HANDLERS); j = g_test_rand_int_range (0, n_handlers);
id = handlers[i]; id = handlers[i];
handlers[i] = handlers[j]; handlers[i] = handlers[j];
handlers[j] = id; handlers[j] = id;
@ -266,17 +279,18 @@ test_block_many (void)
g_test_timer_start (); g_test_timer_start ();
for (i = 0; i < HANDLERS; i++) for (i = 0; i < n_handlers; i++)
g_signal_handler_block (o, handlers[i]); g_signal_handler_block (o, handlers[i]);
for (i = HANDLERS - 1; i >= 0; i--) for (i = n_handlers; i > 0; i--)
g_signal_handler_unblock (o, handlers[i]); g_signal_handler_unblock (o, handlers[i - 1]);
time_elapsed = g_test_timer_elapsed (); time_elapsed = g_test_timer_elapsed ();
g_object_unref (o); g_object_unref (o);
g_free (handlers);
g_test_minimized_result (time_elapsed, "blocked and unblocked %u handlers in %6.3f seconds", HANDLERS, time_elapsed); g_test_minimized_result (time_elapsed, "blocked and unblocked %u handlers in %6.3f seconds", n_handlers, time_elapsed);
} }
int int
@ -284,16 +298,15 @@ main (int argc, char *argv[])
{ {
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
if (g_test_perf ()) n_handlers = g_test_perf () ? 500000 : 1;
{
g_test_add_func ("/signal/handler/connect-many", test_connect_many); g_test_add_func ("/signal/handler/connect-many", test_connect_many);
g_test_add_func ("/signal/handler/disconnect-many-ordered", test_disconnect_many_ordered); g_test_add_func ("/signal/handler/disconnect-many-ordered", test_disconnect_many_ordered);
g_test_add_func ("/signal/handler/disconnect-many-inverse", test_disconnect_many_inverse); g_test_add_func ("/signal/handler/disconnect-many-inverse", test_disconnect_many_inverse);
g_test_add_func ("/signal/handler/disconnect-many-random", test_disconnect_many_random); g_test_add_func ("/signal/handler/disconnect-many-random", test_disconnect_many_random);
g_test_add_func ("/signal/handler/disconnect-2-signals", test_disconnect_2_signals); g_test_add_func ("/signal/handler/disconnect-2-signals", test_disconnect_2_signals);
g_test_add_func ("/signal/handler/disconnect-2-objects", test_disconnect_2_objects); g_test_add_func ("/signal/handler/disconnect-2-objects", test_disconnect_2_objects);
g_test_add_func ("/signal/handler/block-many", test_block_many); g_test_add_func ("/signal/handler/block-many", test_block_many);
}
return g_test_run (); return g_test_run ();
} }