controller: use real world number in benchmark
[platform/upstream/gstreamer.git] / tests / benchmarks / controller.c
1 /* GStreamer
2  * Copyright (C) 2009 Stefan Kost <ensonic@users.sf.net>
3  *
4  * controller.c: benchmark for interpolation control-source
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/gst.h>
23 #include <gst/controller/gstcontroller.h>
24 #include <gst/controller/gstinterpolationcontrolsource.h>
25
26 /* a song in buzztard can easily reach 30000 here */
27 #define NUM_CP 15000
28 #define BLOCK_SIZE 64
29
30 static void
31 event_loop (GstElement * pipe)
32 {
33   GstBus *bus;
34   GstMessage *message = NULL;
35
36   bus = gst_element_get_bus (GST_ELEMENT (pipe));
37
38   while (TRUE) {
39     message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
40
41     g_assert (message != NULL);
42
43     switch (message->type) {
44       case GST_MESSAGE_EOS:
45         gst_message_unref (message);
46         return;
47       case GST_MESSAGE_WARNING:
48       case GST_MESSAGE_ERROR:{
49         GError *gerror;
50         gchar *debug;
51
52         gst_message_parse_error (message, &gerror, &debug);
53         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
54         gst_message_unref (message);
55         g_error_free (gerror);
56         g_free (debug);
57         return;
58       }
59       default:
60         gst_message_unref (message);
61         break;
62     }
63   }
64 }
65
66 gint
67 main (gint argc, gchar * argv[])
68 {
69   gint res = 1;
70   gint i, j;
71   GstElement *src, *sink;
72   GstElement *bin;
73   GstController *ctrl;
74   GstInterpolationControlSource *csource;
75   GValue freq = { 0, };
76   GstClockTime bt, ct;
77   GstClockTimeDiff elapsed;
78   GstClockTime tick;
79
80   gst_init (&argc, &argv);
81   gst_controller_init (&argc, &argv);
82
83   /* build pipeline */
84   bin = gst_pipeline_new ("pipeline");
85   src = gst_element_factory_make ("audiotestsrc", "gen_audio");
86   if (!src) {
87     GST_WARNING ("need audiotestsrc from gst-plugins-base");
88     goto Error;
89   }
90   sink = gst_element_factory_make ("fakesink", "swallow_audio");
91
92   gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
93   if (!gst_element_link (src, sink)) {
94     GST_WARNING ("can't link elements");
95     goto Error;
96   }
97
98   g_object_set (G_OBJECT (src), "wave", 7,      /* sine table - we don't want to benchmark the fpu */
99       "num-buffers", NUM_CP, "samplesperbuffer", BLOCK_SIZE, NULL);
100
101   tick = BLOCK_SIZE * GST_SECOND / 44100;
102
103   /* add a controller to the source */
104   if (!(ctrl = gst_controller_new (G_OBJECT (src), "freq", NULL))) {
105     GST_WARNING ("can't control source element");
106     goto Error;
107   }
108
109   /* create and configure control source */
110   csource = gst_interpolation_control_source_new ();
111   gst_controller_set_control_source (ctrl, "freq",
112       GST_CONTROL_SOURCE (csource));
113   gst_interpolation_control_source_set_interpolation_mode (csource,
114       GST_INTERPOLATE_LINEAR);
115   g_value_init (&freq, G_TYPE_DOUBLE);
116
117
118   /* set control values, we set them in a linear order as we would when loading
119    * a stored project
120    */
121   bt = gst_util_get_timestamp ();
122
123   for (i = 0; i < NUM_CP; i++) {
124     g_value_set_double (&freq, g_random_double_range (50.0, 3000.0));
125     gst_interpolation_control_source_set (csource, i * tick, &freq);
126   }
127
128   ct = gst_util_get_timestamp ();
129   elapsed = GST_CLOCK_DIFF (bt, ct);
130   printf ("linear insert of control-points: %" GST_TIME_FORMAT "\n",
131       GST_TIME_ARGS (elapsed));
132
133
134   /* set extra control values, we set them in arbitrary order to simulate
135    * the user editing a project from the ui
136    */
137   bt = gst_util_get_timestamp ();
138
139   for (i = 0; i < 100; i++) {
140     j = g_random_int_range (0, NUM_CP - 1);
141     g_value_set_double (&freq, g_random_double_range (50.0, 3000.0));
142     gst_interpolation_control_source_set (csource, j * tick, &freq);
143   }
144
145   ct = gst_util_get_timestamp ();
146   elapsed = GST_CLOCK_DIFF (bt, ct);
147   printf ("random insert of control-points: %" GST_TIME_FORMAT "\n",
148       GST_TIME_ARGS (elapsed));
149
150   g_object_unref (csource);
151
152   /* play, this test sequential reads */
153   bt = gst_util_get_timestamp ();
154
155   if (gst_element_set_state (bin, GST_STATE_PLAYING)) {
156     /* wait for EOS */
157     event_loop (bin);
158     gst_element_set_state (bin, GST_STATE_NULL);
159   }
160
161   ct = gst_util_get_timestamp ();
162   elapsed = GST_CLOCK_DIFF (bt, ct);
163   printf ("linear read   of control-points: %" GST_TIME_FORMAT "\n",
164       GST_TIME_ARGS (elapsed));
165
166   /* cleanup */
167   g_object_unref (G_OBJECT (ctrl));
168   gst_object_unref (G_OBJECT (bin));
169   res = 0;
170 Error:
171   return res;
172 }