[glib: 15/16] tests: Allow signal-handler performance tests to be smoketested




commit 1aaf1e7de119c1727d0115951876afb3bf0daa34
Author: Philip Withnall <pwithnall endlessos org>
Date:   Tue Jun 14 15:24:17 2022 +0100

    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>

 gobject/tests/signal-handler.c | 125 +++++++++++++++++++++++------------------
 1 file changed, 69 insertions(+), 56 deletions(-)
---
diff --git a/gobject/tests/signal-handler.c b/gobject/tests/signal-handler.c
index 7a236eed9a..de38e8d65a 100644
--- a/gobject/tests/signal-handler.c
+++ b/gobject/tests/signal-handler.c
@@ -45,96 +45,101 @@ nop (void)
 {
 }
 
-#define HANDLERS 500000
+static guint n_handlers = 0;
 
 static void
 test_connect_many (void)
 {
   MyObj *o;
   gdouble time_elapsed;
-  gint i;
+  guint i;
 
   o = g_object_new (my_obj_get_type (), NULL);
 
   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); 
 
   time_elapsed = g_test_timer_elapsed ();
 
   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
 test_disconnect_many_ordered (void)
 {
   MyObj *o;
-  gulong handlers[HANDLERS];
+  gulong *handlers;
   gdouble time_elapsed;
-  gint i;
+  guint i;
 
+  handlers = g_malloc_n (n_handlers, sizeof (*handlers));
   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); 
 
   g_test_timer_start ();
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     g_signal_handler_disconnect (o, handlers[i]); 
 
   time_elapsed = g_test_timer_elapsed ();
 
   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
 test_disconnect_many_inverse (void)
 {
   MyObj *o;
-  gulong handlers[HANDLERS];
+  gulong *handlers;
   gdouble time_elapsed;
-  gint i;
+  guint i;
 
+  handlers = g_malloc_n (n_handlers, sizeof (*handlers));
   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); 
 
   g_test_timer_start ();
 
-  for (i = HANDLERS - 1; i >= 0; i--)
-    g_signal_handler_disconnect (o, handlers[i]); 
+  for (i = n_handlers; i > 0; i--)
+    g_signal_handler_disconnect (o, handlers[i - 1]);
 
   time_elapsed = g_test_timer_elapsed ();
 
   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
 test_disconnect_many_random (void)
 {
   MyObj *o;
-  gulong handlers[HANDLERS];
+  gulong *handlers;
   gulong id;
   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);
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     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];
       handlers[i] = handlers[j];
       handlers[j] = id;
@@ -142,28 +147,30 @@ test_disconnect_many_random (void)
 
   g_test_timer_start ();
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     g_signal_handler_disconnect (o, handlers[i]); 
 
   time_elapsed = g_test_timer_elapsed ();
 
   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
 test_disconnect_2_signals (void)
 {
   MyObj *o;
-  gulong handlers[HANDLERS];
+  gulong *handlers;
   gulong id;
   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);
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     {
       if (i % 2 == 0)
         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); 
     }
 
-  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];
       handlers[i] = handlers[j];
       handlers[j] = id;
@@ -181,30 +188,33 @@ test_disconnect_2_signals (void)
 
   g_test_timer_start ();
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     g_signal_handler_disconnect (o, handlers[i]); 
 
   time_elapsed = g_test_timer_elapsed ();
 
   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
 test_disconnect_2_objects (void)
 {
   MyObj *o1, *o2, *o;
-  gulong handlers[HANDLERS];
-  MyObj *objects[HANDLERS];
+  gulong *handlers;
+  MyObj **objects;
   gulong id;
   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);
   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)
         {
@@ -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];
       handlers[i] = handlers[j];
       handlers[j] = id;
@@ -231,34 +241,37 @@ test_disconnect_2_objects (void)
 
   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]); 
 
   time_elapsed = g_test_timer_elapsed ();
 
   g_object_unref (o1);
   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
 test_block_many (void)
 {
   MyObj *o;
-  gulong handlers[HANDLERS];
+  gulong *handlers;
   gulong id;
   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);
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     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];
       handlers[i] = handlers[j];
       handlers[j] = id;
@@ -266,17 +279,18 @@ test_block_many (void)
 
   g_test_timer_start ();
 
-  for (i = 0; i < HANDLERS; i++)
+  for (i = 0; i < n_handlers; i++)
     g_signal_handler_block (o, handlers[i]); 
 
-  for (i = HANDLERS - 1; i >= 0; i--)
-    g_signal_handler_unblock (o, handlers[i]); 
+  for (i = n_handlers; i > 0; i--)
+    g_signal_handler_unblock (o, handlers[i - 1]);
 
   time_elapsed = g_test_timer_elapsed ();
 
   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
@@ -284,16 +298,15 @@ main (int argc, char *argv[])
 {
   g_test_init (&argc, &argv, NULL);
 
-  if (g_test_perf ())
-    {
-      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-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-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/block-many", test_block_many);
-    }
+  n_handlers = g_test_perf () ? 500000 : 1;
+
+  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-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-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/block-many", test_block_many);
 
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]