GLib 2.38.0
[glib.git] / tests / sources.c
blob35edb852efd4ed03d6662c5e2cdc9befe1547e41
1 /* This library is free software; you can redistribute it and/or
2 * modify it under the terms of the GNU Lesser General Public
3 * License as published by the Free Software Foundation; either
4 * version 2 of the License, or (at your option) any later version.
6 * This library is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * Lesser General Public License for more details.
11 * You should have received a copy of the GNU Lesser General Public
12 * License along with this library; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 02111-1307, USA.
16 * Copyright 2012 Red Hat, Inc
19 #undef G_DISABLE_ASSERT
20 #undef G_LOG_DOMAIN
22 #include <glib.h>
24 #define NSOURCES 50000
26 static gboolean
27 callback (gpointer user_data)
29 g_assert_not_reached ();
30 return FALSE;
33 static void
34 shuffle (GSource **sources, int num)
36 int i, a, b;
37 GSource *tmp;
39 for (i = 0; i < num * 10; i++)
41 a = g_random_int_range (0, num);
42 b = g_random_int_range (0, num);
43 tmp = sources[a];
44 sources[a] = sources[b];
45 sources[b] = tmp;
49 static void
50 thread_pool_attach_func (gpointer data,
51 gpointer user_data)
53 GMainContext *context = user_data;
54 GSource *source = data;
56 g_source_attach (source, context);
59 static void
60 thread_pool_destroy_func (gpointer data,
61 gpointer user_data)
63 GSource *source = data;
65 g_source_destroy (source);
68 int
69 main (int argc, char **argv)
71 int i;
72 gint64 start;
73 gint64 end;
74 GMainContext *context;
75 GSource **sources;
76 GThreadPool *pool;
77 GError *error = NULL;
79 context = g_main_context_default ();
80 sources = g_new0 (GSource *, NSOURCES);
82 start = g_get_monotonic_time ();
83 for (i = 0; i < NSOURCES; i++)
85 sources[i] = g_idle_source_new ();
86 g_source_set_callback (sources[i], callback, NULL, NULL);
87 g_source_attach (sources[i], context);
89 end = g_get_monotonic_time ();
90 g_print ("Add same-priority sources: %" G_GINT64_FORMAT "\n",
91 (end - start) / 1000);
93 #ifdef SLOW
94 start = g_get_monotonic_time ();
95 for (i = 0; i < NSOURCES; i++)
96 g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
97 end = g_get_monotonic_time ();
98 g_print ("Find each source: %" G_GINT64_FORMAT "\n",
99 (end - start) / 1000);
100 #endif
102 shuffle (sources, NSOURCES);
104 start = g_get_monotonic_time ();
105 for (i = 0; i < NSOURCES; i++)
106 g_source_destroy (sources[i]);
107 end = g_get_monotonic_time ();
108 g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
109 (end - start) / 1000);
111 /* Make sure they really did get removed */
112 g_main_context_iteration (context, FALSE);
114 start = g_get_monotonic_time ();
115 for (i = 0; i < NSOURCES; i++)
117 sources[i] = g_idle_source_new ();
118 g_source_set_callback (sources[i], callback, NULL, NULL);
119 g_source_set_priority (sources[i], i % 100);
120 g_source_attach (sources[i], context);
122 end = g_get_monotonic_time ();
123 g_print ("Add different-priority sources: %" G_GINT64_FORMAT "\n",
124 (end - start) / 1000);
126 #ifdef SLOW
127 start = g_get_monotonic_time ();
128 for (i = 0; i < NSOURCES; i++)
129 g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
130 end = g_get_monotonic_time ();
131 g_print ("Find each source: %" G_GINT64_FORMAT "\n",
132 (end - start) / 1000);
133 #endif
135 shuffle (sources, NSOURCES);
137 start = g_get_monotonic_time ();
138 for (i = 0; i < NSOURCES; i++)
139 g_source_destroy (sources[i]);
140 end = g_get_monotonic_time ();
141 g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
142 (end - start) / 1000);
144 /* Make sure they really did get removed */
145 g_main_context_iteration (context, FALSE);
147 pool = g_thread_pool_new (thread_pool_attach_func, context,
148 20, TRUE, NULL);
149 start = g_get_monotonic_time ();
150 for (i = 0; i < NSOURCES; i++)
152 sources[i] = g_idle_source_new ();
153 g_source_set_callback (sources[i], callback, NULL, NULL);
154 g_thread_pool_push (pool, sources[i], &error);
155 g_assert_no_error (error);
157 g_thread_pool_free (pool, FALSE, TRUE);
158 end = g_get_monotonic_time ();
159 g_print ("Add sources from threads: %" G_GINT64_FORMAT "\n",
160 (end - start) / 1000);
162 pool = g_thread_pool_new (thread_pool_destroy_func, context,
163 20, TRUE, NULL);
164 start = g_get_monotonic_time ();
165 for (i = 0; i < NSOURCES; i++)
167 g_thread_pool_push (pool, sources[i], &error);
168 g_assert_no_error (error);
170 g_thread_pool_free (pool, FALSE, TRUE);
171 end = g_get_monotonic_time ();
172 g_print ("Remove sources from threads: %" G_GINT64_FORMAT "\n",
173 (end - start) / 1000);
175 /* Make sure they really did get removed */
176 g_main_context_iteration (context, FALSE);
178 return 0;