Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / tests / benchmarks / gstbufferstress.c
1 /* GStreamer
2  * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.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 <stdio.h>
21 #include <stdlib.h>
22 #include <gst/gst.h>
23 #include "gst/glib-compat-private.h"
24
25 #define MAX_THREADS  1000
26
27 static guint64 nbbuffers;
28 static GMutex *mutex;
29
30
31 static void *
32 run_test (void *user_data)
33 {
34   gint threadid = GPOINTER_TO_INT (user_data);
35   guint64 nb;
36   GstBuffer *buf;
37   GstClockTime start, end;
38
39   g_mutex_lock (mutex);
40   g_mutex_unlock (mutex);
41
42   start = gst_util_get_timestamp ();
43
44   g_assert (nbbuffers > 0);
45
46   for (nb = nbbuffers; nb; nb--) {
47     buf = gst_buffer_new ();
48     gst_buffer_unref (buf);
49   }
50
51   end = gst_util_get_timestamp ();
52   g_print ("total %" GST_TIME_FORMAT " - average %" GST_TIME_FORMAT
53       "  - Thread %d\n", GST_TIME_ARGS (end - start),
54       GST_TIME_ARGS ((end - start) / nbbuffers), threadid);
55
56
57   g_thread_exit (NULL);
58   return NULL;
59 }
60
61 gint
62 main (gint argc, gchar * argv[])
63 {
64   GThread *threads[MAX_THREADS];
65   gint num_threads;
66   gint t;
67   GstBuffer *tmp;
68   GstClockTime start, end;
69
70   gst_init (&argc, &argv);
71   mutex = g_mutex_new ();
72
73   if (argc != 3) {
74     g_print ("usage: %s <num_threads> <nbbuffers>\n", argv[0]);
75     exit (-1);
76   }
77
78   num_threads = atoi (argv[1]);
79   nbbuffers = atoi (argv[2]);
80
81   if (num_threads <= 0 || num_threads > MAX_THREADS) {
82     g_print ("number of threads must be between 0 and %d\n", MAX_THREADS);
83     exit (-2);
84   }
85
86   if (nbbuffers <= 0) {
87     g_print ("number of buffers must be greater than 0\n");
88     exit (-3);
89   }
90
91   g_mutex_lock (mutex);
92   /* Let's just make sure the GstBufferClass is loaded ... */
93   tmp = gst_buffer_new ();
94
95   printf ("main(): Creating %d threads.\n", num_threads);
96   for (t = 0; t < num_threads; t++) {
97     GError *error = NULL;
98
99 #if !GLIB_CHECK_VERSION (2, 31, 0)
100     threads[t] = g_thread_create (run_test, GINT_TO_POINTER (t), TRUE, &error);
101 #else
102     threads[t] = g_thread_try_new ("bufferstresstest", run_test,
103         GINT_TO_POINTER (t), &error);
104 #endif
105     if (error) {
106       printf ("ERROR: g_thread_create() %s\n", error->message);
107       exit (-1);
108     }
109   }
110
111   /* Signal all threads to start */
112   start = gst_util_get_timestamp ();
113   g_mutex_unlock (mutex);
114
115   for (t = 0; t < num_threads; t++) {
116     if (threads[t])
117       g_thread_join (threads[t]);
118   }
119
120   end = gst_util_get_timestamp ();
121   g_print ("*** total %" GST_TIME_FORMAT " - average %" GST_TIME_FORMAT
122       "  - Done creating %" G_GUINT64_FORMAT " buffers\n",
123       GST_TIME_ARGS (end - start),
124       GST_TIME_ARGS ((end - start) / (num_threads * nbbuffers)),
125       num_threads * nbbuffers);
126
127
128   gst_buffer_unref (tmp);
129
130   return 0;
131 }