stress: add a clock stresstest
[platform/upstream/gstreamer.git] / tests / benchmarks / gstclockstress.c
1 /* GStreamer
2  * Copyright (C) <2009> Wim Taymans <wim taymans at gmail dot com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdlib.h>
21 #include <gst/gst.h>
22
23 #define MAX_THREADS  100
24
25 static gboolean running = TRUE;
26 static gint count = 0;
27
28 static void *
29 run_test (void *user_data)
30 {
31   gint prev;
32   GstClock *sysclock = GST_CLOCK_CAST (user_data);
33
34   while (running) {
35     gst_clock_get_time (sysclock);
36     prev = g_atomic_int_exchange_and_add (&count, 1);
37     if (prev == G_MAXINT)
38       g_warning ("overflow");
39   }
40   g_thread_exit (NULL);
41   return NULL;
42 }
43
44 gint
45 main (gint argc, gchar * argv[])
46 {
47   GThread *threads[MAX_THREADS];
48   gint num_threads;
49   gint t;
50   GstClock *sysclock;
51
52   gst_init (&argc, &argv);
53
54   if (argc != 2) {
55     g_print ("usage: %s <num_threads>\n", argv[0]);
56     exit (-1);
57   }
58
59   num_threads = atoi (argv[1]);
60
61   sysclock = gst_system_clock_obtain ();
62
63   for (t = 0; t < num_threads; t++) {
64     GError *error = NULL;
65
66     threads[t] = g_thread_create (run_test, sysclock, TRUE, &error);
67     if (error) {
68       printf ("ERROR: g_thread_create() %s\n", error->message);
69       exit (-1);
70     }
71   }
72   printf ("main(): Created %d threads.\n", t);
73
74   /* run for 5 seconds */
75   g_usleep (G_USEC_PER_SEC * 5);
76
77   printf ("main(): Stopping threads...\n");
78
79   running = FALSE;
80
81   for (t = 0; t < num_threads; t++) {
82     g_thread_join (threads[t]);
83   }
84
85   g_print ("performed %d get_time operations\n", count);
86
87   gst_object_unref (sysclock);
88
89   return 0;
90 }