mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-10 03:04:05 +02:00
mutex testcase: add a performance test
Add a reasonable performance test for uncontended and contended cases.
This commit is contained in:
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_mutex1 (void)
|
test_mutex1 (void)
|
||||||
{
|
{
|
||||||
@@ -152,6 +154,58 @@ test_mutex5 (void)
|
|||||||
g_assert (owners[i] == NULL);
|
g_assert (owners[i] == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define COUNT_TO 100000000
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
do_addition (gint *value)
|
||||||
|
{
|
||||||
|
static GMutex lock = G_MUTEX_INIT;
|
||||||
|
gboolean more;
|
||||||
|
|
||||||
|
/* test performance of "good" cases (ie: short critical sections) */
|
||||||
|
g_mutex_lock (&lock);
|
||||||
|
if ((more = *value != COUNT_TO))
|
||||||
|
if (*value != -1)
|
||||||
|
(*value)++;
|
||||||
|
g_mutex_unlock (&lock);
|
||||||
|
|
||||||
|
return more;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gpointer
|
||||||
|
addition_thread (gpointer value)
|
||||||
|
{
|
||||||
|
while (do_addition (value));
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_mutex_perf (gconstpointer data)
|
||||||
|
{
|
||||||
|
gint n_threads = GPOINTER_TO_INT (data);
|
||||||
|
GThread *threads[THREADS];
|
||||||
|
gint64 start_time;
|
||||||
|
gdouble rate;
|
||||||
|
gint x = -1;
|
||||||
|
gint i;
|
||||||
|
|
||||||
|
for (i = 0; i < n_threads - 1; i++)
|
||||||
|
threads[i] = g_thread_create (addition_thread, &x, TRUE, NULL);
|
||||||
|
|
||||||
|
/* avoid measuring thread setup/teardown time */
|
||||||
|
start_time = g_get_monotonic_time ();
|
||||||
|
g_atomic_int_set (&x, 0);
|
||||||
|
addition_thread (&x);
|
||||||
|
g_assert_cmpint (g_atomic_int_get (&x), ==, COUNT_TO);
|
||||||
|
rate = g_get_monotonic_time () - start_time;
|
||||||
|
rate = x / rate;
|
||||||
|
|
||||||
|
for (i = 0; i < n_threads - 1; i++)
|
||||||
|
g_thread_join (threads[i]);
|
||||||
|
|
||||||
|
g_test_maximized_result (rate, "%f mips", rate);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
@@ -164,5 +218,19 @@ main (int argc, char *argv[])
|
|||||||
g_test_add_func ("/thread/mutex4", test_mutex4);
|
g_test_add_func ("/thread/mutex4", test_mutex4);
|
||||||
g_test_add_func ("/thread/mutex5", test_mutex5);
|
g_test_add_func ("/thread/mutex5", test_mutex5);
|
||||||
|
|
||||||
|
if (g_test_perf ())
|
||||||
|
{
|
||||||
|
gint i;
|
||||||
|
|
||||||
|
g_test_add_data_func ("/thread/mutex/perf/uncontended", NULL, test_mutex_perf);
|
||||||
|
|
||||||
|
for (i = 1; i <= 10; i++)
|
||||||
|
{
|
||||||
|
gchar name[80];
|
||||||
|
sprintf (name, "/thread/mutex/perf/contended/%d", i);
|
||||||
|
g_test_add_data_func (name, GINT_TO_POINTER (i), test_mutex_perf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user