mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-24 14:36:13 +01:00
tests: add a timing test for adding lots of GSources
https://bugzilla.gnome.org/show_bug.cgi?id=619329
This commit is contained in:
parent
4780ee5d4a
commit
56de38da7d
1
tests/.gitignore
vendored
1
tests/.gitignore
vendored
@ -53,6 +53,7 @@ slice-concurrent
|
||||
slice-test
|
||||
slice-threadinit
|
||||
slist-test
|
||||
sources
|
||||
spawn-test
|
||||
testgdate
|
||||
testgdateparser
|
||||
|
@ -104,6 +104,7 @@ test_programs = \
|
||||
slice-color \
|
||||
slice-concurrent \
|
||||
slice-threadinit \
|
||||
sources \
|
||||
spawn-test \
|
||||
$(spawn_test_win32_gui) \
|
||||
thread-test \
|
||||
@ -153,6 +154,7 @@ slice_concurrent_SOURCES = slice-concurrent.c
|
||||
slice_concurrent_LDADD = $(thread_ldadd)
|
||||
slice_threadinit_SOURCES = slice-threadinit.c
|
||||
slice_threadinit_LDADD = $(thread_ldadd)
|
||||
sources_LDADD = $(progs_ldadd)
|
||||
spawn_test_LDADD = $(progs_ldadd)
|
||||
thread_test_LDADD = $(thread_ldadd)
|
||||
threadpool_test_LDADD = $(thread_ldadd)
|
||||
|
189
tests/sources.c
Normal file
189
tests/sources.c
Normal file
@ -0,0 +1,189 @@
|
||||
/* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Copyright 2012 Red Hat, Inc
|
||||
*/
|
||||
|
||||
#undef G_DISABLE_ASSERT
|
||||
#undef G_LOG_DOMAIN
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#define NSOURCES 50000
|
||||
|
||||
static gboolean
|
||||
callback (gpointer user_data)
|
||||
{
|
||||
g_assert_not_reached ();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
shuffle (GSource **sources, int num)
|
||||
{
|
||||
int i, a, b;
|
||||
GSource *tmp;
|
||||
|
||||
for (i = 0; i < num * 10; i++)
|
||||
{
|
||||
a = g_random_int_range (0, num);
|
||||
b = g_random_int_range (0, num);
|
||||
tmp = sources[a];
|
||||
sources[a] = sources[b];
|
||||
sources[b] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
thread_pool_attach_func (gpointer data,
|
||||
gpointer user_data)
|
||||
{
|
||||
GMainContext *context = user_data;
|
||||
GSource *source = data;
|
||||
|
||||
g_source_attach (source, context);
|
||||
}
|
||||
|
||||
static void
|
||||
thread_pool_destroy_func (gpointer data,
|
||||
gpointer user_data)
|
||||
{
|
||||
GSource *source = data;
|
||||
|
||||
g_source_destroy (source);
|
||||
}
|
||||
|
||||
static double
|
||||
difftimeval (struct timeval *old, struct timeval *new)
|
||||
{
|
||||
return
|
||||
(new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
struct rusage old_usage;
|
||||
struct rusage new_usage;
|
||||
GMainContext *context;
|
||||
GSource **sources;
|
||||
GThreadPool *pool;
|
||||
GError *error = NULL;
|
||||
|
||||
context = g_main_context_default ();
|
||||
sources = g_new0 (GSource *, NSOURCES);
|
||||
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
{
|
||||
sources[i] = g_idle_source_new ();
|
||||
g_source_set_callback (sources[i], callback, NULL, NULL);
|
||||
g_source_attach (sources[i], context);
|
||||
}
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Add same-priority sources: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
|
||||
#ifdef SLOW
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Find each source: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
#endif
|
||||
|
||||
shuffle (sources, NSOURCES);
|
||||
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
g_source_destroy (sources[i]);
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Remove in random order: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
|
||||
/* Make sure they really did get removed */
|
||||
g_main_context_iteration (context, FALSE);
|
||||
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
{
|
||||
sources[i] = g_idle_source_new ();
|
||||
g_source_set_callback (sources[i], callback, NULL, NULL);
|
||||
g_source_set_priority (sources[i], i % 100);
|
||||
g_source_attach (sources[i], context);
|
||||
}
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Add different-priority sources: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
|
||||
#ifdef SLOW
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Find each source: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
#endif
|
||||
|
||||
shuffle (sources, NSOURCES);
|
||||
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
g_source_destroy (sources[i]);
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Remove in random order: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
|
||||
/* Make sure they really did get removed */
|
||||
g_main_context_iteration (context, FALSE);
|
||||
|
||||
pool = g_thread_pool_new (thread_pool_attach_func, context,
|
||||
20, TRUE, NULL);
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
{
|
||||
sources[i] = g_idle_source_new ();
|
||||
g_source_set_callback (sources[i], callback, NULL, NULL);
|
||||
g_thread_pool_push (pool, sources[i], &error);
|
||||
g_assert_no_error (error);
|
||||
}
|
||||
g_thread_pool_free (pool, FALSE, TRUE);
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Add sources from threads: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
|
||||
pool = g_thread_pool_new (thread_pool_destroy_func, context,
|
||||
20, TRUE, NULL);
|
||||
getrusage (RUSAGE_SELF, &old_usage);
|
||||
for (i = 0; i < NSOURCES; i++)
|
||||
{
|
||||
g_thread_pool_push (pool, sources[i], &error);
|
||||
g_assert_no_error (error);
|
||||
}
|
||||
g_thread_pool_free (pool, FALSE, TRUE);
|
||||
getrusage (RUSAGE_SELF, &new_usage);
|
||||
g_print ("Remove sources from threads: %g\n",
|
||||
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
||||
|
||||
/* Make sure they really did get removed */
|
||||
g_main_context_iteration (context, FALSE);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user