benchmarks: Fix the complexity and mass-elements benchmarks
[platform/upstream/gstreamer.git] / tests / benchmarks / mass-elements.c
1 /*
2  * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
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 IDENTITY_COUNT (1000)
24 #define BUFFER_COUNT (1000)
25 #define SRC_ELEMENT "fakesrc"
26 #define SINK_ELEMENT "fakesink"
27
28
29 static GstClockTime
30 gst_get_current_time (void)
31 {
32   GTimeVal tv;
33
34   g_get_current_time (&tv);
35   return GST_TIMEVAL_TO_TIME (tv);
36 }
37
38 gint
39 main (gint argc, gchar * argv[])
40 {
41   GstMessage *msg;
42   GstElement *pipeline, *src, *sink, *current, *last;
43   guint i, buffers = BUFFER_COUNT, identities = IDENTITY_COUNT;
44   GstClockTime start, end;
45   gchar *src_name = SRC_ELEMENT, *sink_name = SINK_ELEMENT;
46
47   gst_init (&argc, &argv);
48
49   if (argc > 1)
50     identities = atoi (argv[1]);
51   if (argc > 2)
52     buffers = atoi (argv[2]);
53   if (argc > 3)
54     src_name = argv[3];
55   if (argc > 4)
56     sink_name = argv[4];
57
58   g_print
59       ("*** benchmarking this pipeline: %s num-buffers=%u ! %u * identity ! %s\n",
60       src_name, buffers, identities, sink_name);
61   start = gst_get_current_time ();
62   pipeline = gst_element_factory_make ("pipeline", NULL);
63   g_assert (pipeline);
64   src = gst_element_factory_make (src_name, NULL);
65   if (!src) {
66     g_print ("no element named \"%s\" found, aborting...\n", src_name);
67     return 1;
68   }
69   g_object_set (src, "num-buffers", buffers, NULL);
70   sink = gst_element_factory_make (sink_name, NULL);
71   if (!sink) {
72     g_print ("no element named \"%s\" found, aborting...\n", sink_name);
73     return 1;
74   }
75   last = src;
76   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
77   for (i = 0; i < identities; i++) {
78     current = gst_element_factory_make ("identity", NULL);
79     g_assert (current);
80     /* shut this element up (no g_strdup_printf please) */
81     g_object_set (current, "silent", TRUE, NULL);
82     gst_bin_add (GST_BIN (pipeline), current);
83     if (!gst_element_link (last, current))
84       g_assert_not_reached ();
85     last = current;
86   }
87   if (!gst_element_link (last, sink))
88     g_assert_not_reached ();
89   end = gst_get_current_time ();
90   g_print ("%" GST_TIME_FORMAT " - creating %u identity elements\n",
91       GST_TIME_ARGS (end - start), identities);
92
93   start = gst_get_current_time ();
94   if (gst_element_set_state (pipeline,
95           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
96     g_assert_not_reached ();
97   if (gst_element_get_state (pipeline, NULL, NULL,
98           GST_CLOCK_TIME_NONE) == GST_STATE_CHANGE_FAILURE)
99     g_assert_not_reached ();
100   end = gst_get_current_time ();
101   g_print ("%" GST_TIME_FORMAT " - setting pipeline to playing\n",
102       GST_TIME_ARGS (end - start));
103
104   start = gst_get_current_time ();
105   msg = gst_bus_poll (gst_element_get_bus (pipeline),
106       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
107   end = gst_get_current_time ();
108   gst_message_unref (msg);
109   g_print ("%" GST_TIME_FORMAT " - putting %u buffers through\n",
110       GST_TIME_ARGS (end - start), buffers);
111
112   start = gst_get_current_time ();
113   if (gst_element_set_state (pipeline,
114           GST_STATE_NULL) != GST_STATE_CHANGE_SUCCESS)
115     g_assert_not_reached ();
116   end = gst_get_current_time ();
117   g_print ("%" GST_TIME_FORMAT " - setting pipeline to NULL\n",
118       GST_TIME_ARGS (end - start));
119
120   start = gst_get_current_time ();
121   g_object_unref (pipeline);
122   end = gst_get_current_time ();
123   g_print ("%" GST_TIME_FORMAT " - unreffing pipeline\n",
124       GST_TIME_ARGS (end - start));
125
126   return 0;
127 }