benchmarks: update the tracer benchmark and add a shell benchmark
[platform/upstream/gstreamer.git] / tests / benchmarks / tracerserialize.c
1 /* GStreamer
2  * Copyright (C) 2016 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * tracerserialize.c: benchmark for log serialisation
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 FITcreating %d capsNESS 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /* to check the sizes run:
23  *
24  * GST_DEBUG="default:7" GST_DEBUG_FILE=trace.log ./tracerserialize
25  *
26  * grep "log_gst_structure" trace.log >tracerserialize.gststructure.log
27  * grep "log_g_variant" trace.log >tracerserialize.gvariant.log
28  *
29  */
30
31 #include <gst/gst.h>
32
33 #define NUM_LOOPS 100000
34
35 static void
36 log_gst_structure (const gchar * name, const gchar * first, ...)
37 {
38   va_list var_args;
39   GstStructure *s;
40   gchar *l;
41
42   va_start (var_args, first);
43   s = gst_structure_new_valist (name, first, var_args);
44   l = gst_structure_to_string (s);
45   GST_TRACE ("%s", l);
46   g_free (l);
47   gst_structure_free (s);
48   va_end (var_args);
49 }
50
51 static void
52 log_gst_structure_tmpl (const gchar * format, ...)
53 {
54   va_list var_args;
55
56   va_start (var_args, format);
57   if (G_LIKELY (GST_LEVEL_TRACE <= _gst_debug_min)) {
58     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_TRACE, __FILE__,
59         GST_FUNCTION, __LINE__, NULL, format, var_args);
60   }
61   va_end (var_args);
62 }
63
64 static void
65 log_g_variant (const gchar * format, ...)
66 {
67   va_list var_args;
68   GVariant *v;
69   gchar *l;
70
71   va_start (var_args, format);
72   v = g_variant_new_va (format, NULL, &var_args);
73   l = g_variant_print (v, FALSE);
74   GST_TRACE ("%s", l);
75   g_free (l);
76   g_variant_unref (v);
77   va_end (var_args);
78 }
79
80 gint
81 main (gint argc, gchar * argv[])
82 {
83   GstClockTime start, end;
84   gint i;
85
86   gst_init (&argc, &argv);
87
88   start = gst_util_get_timestamp ();
89   for (i = 0; i < NUM_LOOPS; i++) {
90     log_gst_structure ("name",
91         "ts", G_TYPE_UINT64, (guint64) 0,
92         "index", G_TYPE_UINT, 10,
93         "test", G_TYPE_STRING, "hallo",
94         "bool", G_TYPE_BOOLEAN, TRUE,
95         "flag", GST_TYPE_PAD_DIRECTION, GST_PAD_SRC, NULL);
96   }
97   end = gst_util_get_timestamp ();
98   g_print ("%" GST_TIME_FORMAT ": GstStructure\n", GST_TIME_ARGS (end - start));
99
100   start = gst_util_get_timestamp ();
101   for (i = 0; i < NUM_LOOPS; i++) {
102     log_gst_structure_tmpl ("name, ts=(guint64)%" G_GUINT64_FORMAT
103         ", index=(uint)%u, test=(string)%s, bool=(boolean)%s, flag=(GstPadDirection)%d;",
104         (guint64) 0, 10, "hallo", (TRUE ? "true" : "false"), GST_PAD_SRC);
105   }
106   end = gst_util_get_timestamp ();
107   g_print ("%" GST_TIME_FORMAT ": GstStructure template\n",
108       GST_TIME_ARGS (end - start));
109
110   start = gst_util_get_timestamp ();
111   for (i = 0; i < NUM_LOOPS; i++) {
112     log_g_variant ("(stusbu)", "name", (guint64) 0, 10, "hallo", TRUE,
113         GST_PAD_SRC);
114   }
115   end = gst_util_get_timestamp ();
116   g_print ("%" GST_TIME_FORMAT ": GVariant\n", GST_TIME_ARGS (end - start));
117 }