Updated Serbian translation
[platform/upstream/glib.git] / tests / sources.c
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.
5  *
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.
10  *
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.
15  *
16  * Copyright 2012 Red Hat, Inc
17  */
18
19 #undef G_DISABLE_ASSERT
20 #undef G_LOG_DOMAIN
21
22 #include <sys/time.h>
23 #include <sys/resource.h>
24
25 #include <glib.h>
26
27 #define NSOURCES 50000
28
29 static gboolean
30 callback (gpointer user_data)
31 {
32   g_assert_not_reached ();
33   return FALSE;
34 }
35
36 static void
37 shuffle (GSource **sources, int num)
38 {
39   int i, a, b;
40   GSource *tmp;
41
42   for (i = 0; i < num * 10; i++)
43     {
44       a = g_random_int_range (0, num);
45       b = g_random_int_range (0, num);
46       tmp = sources[a];
47       sources[a] = sources[b];
48       sources[b] = tmp;
49     }
50 }
51
52 static void
53 thread_pool_attach_func (gpointer data,
54                          gpointer user_data)
55 {
56   GMainContext *context = user_data;
57   GSource *source = data;
58
59   g_source_attach (source, context);
60 }
61
62 static void
63 thread_pool_destroy_func (gpointer data,
64                           gpointer user_data)
65 {
66   GSource *source = data;
67
68   g_source_destroy (source);
69 }
70
71 static double
72 difftimeval (struct timeval *old, struct timeval *new)
73 {
74   return
75     (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
76 }
77
78 int
79 main (int argc, char **argv)
80 {
81   int i;
82   struct rusage old_usage;
83   struct rusage new_usage;
84   GMainContext *context;
85   GSource **sources;
86   GThreadPool *pool;
87   GError *error = NULL;
88
89   context = g_main_context_default ();
90   sources = g_new0 (GSource *, NSOURCES);
91
92   getrusage (RUSAGE_SELF, &old_usage);
93   for (i = 0; i < NSOURCES; i++)
94     {
95       sources[i] = g_idle_source_new ();
96       g_source_set_callback (sources[i], callback, NULL, NULL);
97       g_source_attach (sources[i], context);
98     }
99   getrusage (RUSAGE_SELF, &new_usage);
100   g_print ("Add same-priority sources: %g\n",
101            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
102
103 #ifdef SLOW
104   getrusage (RUSAGE_SELF, &old_usage);
105   for (i = 0; i < NSOURCES; i++)
106     g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
107   getrusage (RUSAGE_SELF, &new_usage);
108   g_print ("Find each source: %g\n",
109            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
110 #endif
111
112   shuffle (sources, NSOURCES);
113
114   getrusage (RUSAGE_SELF, &old_usage);
115   for (i = 0; i < NSOURCES; i++)
116     g_source_destroy (sources[i]);
117   getrusage (RUSAGE_SELF, &new_usage);
118   g_print ("Remove in random order: %g\n",
119            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
120
121   /* Make sure they really did get removed */
122   g_main_context_iteration (context, FALSE);
123
124   getrusage (RUSAGE_SELF, &old_usage);
125   for (i = 0; i < NSOURCES; i++)
126     {
127       sources[i] = g_idle_source_new ();
128       g_source_set_callback (sources[i], callback, NULL, NULL);
129       g_source_set_priority (sources[i], i % 100);
130       g_source_attach (sources[i], context);
131     }
132   getrusage (RUSAGE_SELF, &new_usage);
133   g_print ("Add different-priority sources: %g\n",
134            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
135
136 #ifdef SLOW
137   getrusage (RUSAGE_SELF, &old_usage);
138   for (i = 0; i < NSOURCES; i++)
139     g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
140   getrusage (RUSAGE_SELF, &new_usage);
141   g_print ("Find each source: %g\n",
142            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
143 #endif
144
145   shuffle (sources, NSOURCES);
146
147   getrusage (RUSAGE_SELF, &old_usage);
148   for (i = 0; i < NSOURCES; i++)
149     g_source_destroy (sources[i]);
150   getrusage (RUSAGE_SELF, &new_usage);
151   g_print ("Remove in random order: %g\n",
152            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
153
154   /* Make sure they really did get removed */
155   g_main_context_iteration (context, FALSE);
156
157   pool = g_thread_pool_new (thread_pool_attach_func, context,
158                             20, TRUE, NULL);
159   getrusage (RUSAGE_SELF, &old_usage);
160   for (i = 0; i < NSOURCES; i++)
161     {
162       sources[i] = g_idle_source_new ();
163       g_source_set_callback (sources[i], callback, NULL, NULL);
164       g_thread_pool_push (pool, sources[i], &error);
165       g_assert_no_error (error);
166     }
167   g_thread_pool_free (pool, FALSE, TRUE);
168   getrusage (RUSAGE_SELF, &new_usage);
169   g_print ("Add sources from threads: %g\n",
170            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
171
172   pool = g_thread_pool_new (thread_pool_destroy_func, context,
173                             20, TRUE, NULL);
174   getrusage (RUSAGE_SELF, &old_usage);
175   for (i = 0; i < NSOURCES; i++)
176     {
177       g_thread_pool_push (pool, sources[i], &error);
178       g_assert_no_error (error);
179     }
180   g_thread_pool_free (pool, FALSE, TRUE);
181   getrusage (RUSAGE_SELF, &new_usage);
182   g_print ("Remove sources from threads: %g\n",
183            difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
184
185   /* Make sure they really did get removed */
186   g_main_context_iteration (context, FALSE);
187
188   return 0;
189 }