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
24 #define NSOURCES 50000
27 callback (gpointer user_data)
29 g_assert_not_reached ();
34 shuffle (GSource **sources, int num)
39 for (i = 0; i < num * 10; i++)
41 a = g_random_int_range (0, num);
42 b = g_random_int_range (0, num);
44 sources[a] = sources[b];
50 thread_pool_attach_func (gpointer data,
53 GMainContext *context = user_data;
54 GSource *source = data;
56 g_source_attach (source, context);
57 g_source_unref (source);
61 thread_pool_destroy_func (gpointer data,
64 GSource *source = data;
66 g_source_destroy (source);
70 main (int argc, char **argv)
75 GMainContext *context;
80 context = g_main_context_default ();
81 sources = g_new0 (GSource *, NSOURCES);
83 start = g_get_monotonic_time ();
84 for (i = 0; i < NSOURCES; i++)
86 sources[i] = g_idle_source_new ();
87 g_source_set_callback (sources[i], callback, NULL, NULL);
88 g_source_attach (sources[i], context);
90 end = g_get_monotonic_time ();
91 g_print ("Add same-priority sources: %" G_GINT64_FORMAT "\n",
92 (end - start) / 1000);
95 start = g_get_monotonic_time ();
96 for (i = 0; i < NSOURCES; i++)
97 g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
98 end = g_get_monotonic_time ();
99 g_print ("Find each source: %" G_GINT64_FORMAT "\n",
100 (end - start) / 1000);
103 shuffle (sources, NSOURCES);
105 start = g_get_monotonic_time ();
106 for (i = 0; i < NSOURCES; i++)
108 g_source_destroy (sources[i]);
109 g_source_unref (sources[i]);
111 end = g_get_monotonic_time ();
112 g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
113 (end - start) / 1000);
115 /* Make sure they really did get removed */
116 g_main_context_iteration (context, FALSE);
118 start = g_get_monotonic_time ();
119 for (i = 0; i < NSOURCES; i++)
121 sources[i] = g_idle_source_new ();
122 g_source_set_callback (sources[i], callback, NULL, NULL);
123 g_source_set_priority (sources[i], i % 100);
124 g_source_attach (sources[i], context);
126 end = g_get_monotonic_time ();
127 g_print ("Add different-priority sources: %" G_GINT64_FORMAT "\n",
128 (end - start) / 1000);
131 start = g_get_monotonic_time ();
132 for (i = 0; i < NSOURCES; i++)
133 g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
134 end = g_get_monotonic_time ();
135 g_print ("Find each source: %" G_GINT64_FORMAT "\n",
136 (end - start) / 1000);
139 shuffle (sources, NSOURCES);
141 start = g_get_monotonic_time ();
142 for (i = 0; i < NSOURCES; i++)
144 g_source_destroy (sources[i]);
145 g_source_unref (sources[i]);
147 end = g_get_monotonic_time ();
148 g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
149 (end - start) / 1000);
151 /* Make sure they really did get removed */
152 g_main_context_iteration (context, FALSE);
154 pool = g_thread_pool_new (thread_pool_attach_func, context,
156 start = g_get_monotonic_time ();
157 for (i = 0; i < NSOURCES; i++)
159 sources[i] = g_idle_source_new ();
160 g_source_set_callback (sources[i], callback, NULL, NULL);
161 g_thread_pool_push (pool, sources[i], &error);
162 g_assert_no_error (error);
164 g_thread_pool_free (pool, FALSE, TRUE);
165 end = g_get_monotonic_time ();
166 g_print ("Add sources from threads: %" G_GINT64_FORMAT "\n",
167 (end - start) / 1000);
169 pool = g_thread_pool_new (thread_pool_destroy_func, context,
171 start = g_get_monotonic_time ();
172 for (i = 0; i < NSOURCES; i++)
174 g_thread_pool_push (pool, sources[i], &error);
175 g_assert_no_error (error);
177 g_thread_pool_free (pool, FALSE, TRUE);
178 end = g_get_monotonic_time ();
179 g_print ("Remove sources from threads: %" G_GINT64_FORMAT "\n",
180 (end - start) / 1000);
182 /* Make sure they really did get removed */
183 g_main_context_iteration (context, FALSE);