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