1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <sys/times.h>
5
6 #include <gst/gst.h>
7 #include <gst/base/gstadapter.h>
8
9 struct TestParams
10 {
11 gint tot_size;
12 gint read_size;
13 gint write_size;
14 };
15
16 /* This test pushes 'n' buffers of 'write size' into an adapter, then reads
17 * them out in 'read size' sized pieces, using take and then take_buffer,
18 * and prints the timings */
19
20 static struct TestParams param_sets[] = {
21 /* These values put ~256MB in 1MB chunks in an adapter, then reads them out
22 * in 250kb blocks */
23 {256000000, 250000, 1000000},
24 /* These values put ~256MB in 1000 byte chunks in an adapter, then reads them
25 * out in 200 byte blocks */
26 {25600000, 200, 1000},
27 /* These values put ~256MB in 200 chunks in an adapter, then reads them out
28 * in 1000 byte blocks */
29 {25600000, 1000, 200}
30 };
31
32 static const gint n_tests = sizeof (param_sets) / sizeof (struct TestParams);
33
34 static gint ticks_per_sec;
35
36 static void
run_test_take(struct TestParams * params)37 run_test_take (struct TestParams *params)
38 {
39 /* Create an adapter and feed it data of fixed size, then retrieve it in
40 * a different size */
41 GstAdapter *adapter = gst_adapter_new ();
42 guint8 *data;
43 GstBuffer *buf;
44 int i;
45 gint ntimes = params->tot_size / params->write_size;
46
47 for (i = 0; i < ntimes; i++) {
48 buf = gst_buffer_new_and_alloc (params->write_size);
49
50 gst_buffer_memset (buf, 0, 0, params->write_size);
51
52 gst_adapter_push (adapter, buf);
53 }
54
55 do {
56 data = gst_adapter_take (adapter, params->read_size);
57 if (data == NULL)
58 break;
59 g_free (data);
60 } while (TRUE);
61
62 g_object_unref (adapter);
63 }
64
65 static void
run_test_take_buffer(struct TestParams * params)66 run_test_take_buffer (struct TestParams *params)
67 {
68 /* Create an adapter and feed it data of fixed size, then retrieve it in
69 * a different size
70 */
71 GstAdapter *adapter = gst_adapter_new ();
72 GstBuffer *buf;
73 int i;
74 gint ntimes = params->tot_size / params->write_size;
75
76 for (i = 0; i < ntimes; i++) {
77 buf = gst_buffer_new_and_alloc (params->write_size);
78
79 gst_buffer_memset (buf, 0, 0, params->write_size);
80
81 gst_adapter_push (adapter, buf);
82 }
83
84 do {
85 buf = gst_adapter_take_buffer (adapter, params->read_size);
86 if (buf == NULL)
87 break;
88 gst_buffer_unref (buf);
89 } while (TRUE);
90
91 g_object_unref (adapter);
92 }
93
94 static void
run_tests(struct TestParams * params)95 run_tests (struct TestParams *params)
96 {
97 struct tms time_data;
98 gdouble start;
99 gdouble dur;
100
101 g_print ("Running on %d bytes, writing %d bytes/buf, reading %d bytes/buf\n",
102 params->tot_size, params->write_size, params->read_size);
103
104 start = 0.0;
105 run_test_take (params);
106
107 times (&time_data);
108 dur = (gdouble) (time_data.tms_utime + time_data.tms_stime) / ticks_per_sec;
109 g_print ("Time for take test: %g secs\n", dur - start);
110
111 start = dur;
112 run_test_take_buffer (params);
113
114 times (&time_data);
115 dur = (gdouble) (time_data.tms_utime + time_data.tms_stime) / ticks_per_sec;
116 g_print ("Time for TakeBuffer test: %g secs\n", dur - start);
117
118 g_print ("\n");
119 }
120
121 int
main(int argc,char ** argv)122 main (int argc, char **argv)
123 {
124 gint i;
125
126 ticks_per_sec = sysconf (_SC_CLK_TCK);
127
128 gst_init (&argc, &argv);
129
130 for (i = 0; i < n_tests; i++)
131 run_tests (param_sets + i);
132
133 return 0;
134 }
135