aboutsummaryrefslogtreecommitdiff
path: root/tests/examples/adapter/adapter_test.c
blob: 489a06fecea421ab1667d39de23e3b762a71a947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/times.h>

#include <gst/gst.h>
#include <gst/base/gstadapter.h>

struct TestParams
{
  gint tot_size;
  gint read_size;
  gint write_size;
};

/* This test pushes 'n' buffers of 'write size' into an adapter, then reads
 * them out in 'read size' sized pieces, using take and then take_buffer, 
 * and prints the timings */

static struct TestParams param_sets[] = {
/* These values put ~256MB in 1MB chunks in an adapter, then reads them out
 * in 250kb blocks */
  {256000000, 250000, 1000000},
/* These values put ~256MB in 1000 byte chunks in an adapter, then reads them 
 * out in 200 byte blocks */
  {25600000, 200, 1000},
/* These values put ~256MB in 200 chunks in an adapter, then reads them out
 * in 1000 byte blocks */
  {25600000, 1000, 200}
};

static const gint n_tests = sizeof (param_sets) / sizeof (struct TestParams);

static gint ticks_per_sec;

static void
run_test_take (struct TestParams *params)
{
  /* Create an adapter and feed it data of fixed size, then retrieve it in 
   * a different size */
  GstAdapter *adapter = gst_adapter_new ();
  guint8 *data;
  GstBuffer *buf;
  int i;
  gint ntimes = params->tot_size / params->write_size;

  for (i = 0; i < ntimes; i++) {
    buf = gst_buffer_new_and_alloc (params->write_size);

    gst_buffer_memset (buf, 0, 0, params->write_size);

    gst_adapter_push (adapter, buf);
  }

  do {
    data = gst_adapter_take (adapter, params->read_size);
    if (data == NULL)
      break;
    g_free (data);
  } while (TRUE);

  g_object_unref (adapter);
}

static void
run_test_take_buffer (struct TestParams *params)
{
  /* Create an adapter and feed it data of fixed size, then retrieve it in 
   * a different size
   */
  GstAdapter *adapter = gst_adapter_new ();
  GstBuffer *buf;
  int i;
  gint ntimes = params->tot_size / params->write_size;

  for (i = 0; i < ntimes; i++) {
    buf = gst_buffer_new_and_alloc (params->write_size);

    gst_buffer_memset (buf, 0, 0, params->write_size);

    gst_adapter_push (adapter, buf);
  }

  do {
    buf = gst_adapter_take_buffer (adapter, params->read_size);
    if (buf == NULL)
      break;
    gst_buffer_unref (buf);
  } while (TRUE);

  g_object_unref (adapter);
}

static void
run_tests (struct TestParams *params)
{
  struct tms time_data;
  gdouble start;
  gdouble dur;

  g_print ("Running on %d bytes, writing %d bytes/buf, reading %d bytes/buf\n",
      params->tot_size, params->write_size, params->read_size);

  start = 0.0;
  run_test_take (params);

  times (&time_data);
  dur = (gdouble) (time_data.tms_utime + time_data.tms_stime) / ticks_per_sec;
  g_print ("Time for take test: %g secs\n", dur - start);

  start = dur;
  run_test_take_buffer (params);

  times (&time_data);
  dur = (gdouble) (time_data.tms_utime + time_data.tms_stime) / ticks_per_sec;
  g_print ("Time for TakeBuffer test: %g secs\n", dur - start);

  g_print ("\n");
}

int
main (int argc, char **argv)
{
  gint i;

  ticks_per_sec = sysconf (_SC_CLK_TCK);

  gst_init (&argc, &argv);

  for (i = 0; i < n_tests; i++)
    run_tests (param_sets + i);

  return 0;
}