summaryrefslogtreecommitdiffstats
path: root/spa/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:28:17 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:28:17 +0000
commit7a46c07230b8d8108c0e8e80df4522d0ac116538 (patch)
treed483300dab478b994fe199a5d19d18d74153718a /spa/tests
parentInitial commit. (diff)
downloadpipewire-7a46c07230b8d8108c0e8e80df4522d0ac116538.tar.xz
pipewire-7a46c07230b8d8108c0e8e80df4522d0ac116538.zip
Adding upstream version 0.3.65.upstream/0.3.65upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'spa/tests')
-rw-r--r--spa/tests/benchmark-dict.c145
-rw-r--r--spa/tests/benchmark-pod.c294
-rw-r--r--spa/tests/meson.build58
-rw-r--r--spa/tests/spa-include-test-template.c5
-rw-r--r--spa/tests/stress-ringbuffer.c147
5 files changed, 649 insertions, 0 deletions
diff --git a/spa/tests/benchmark-dict.c b/spa/tests/benchmark-dict.c
new file mode 100644
index 0000000..c47845e
--- /dev/null
+++ b/spa/tests/benchmark-dict.c
@@ -0,0 +1,145 @@
+/* Spa
+ *
+ * Copyright © 2020 Wim Taymans
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <time.h>
+#include <assert.h>
+
+#include <spa/utils/dict.h>
+#include <spa/utils/string.h>
+
+#define MAX_COUNT 100000
+#define MAX_ITEMS 1000
+
+static struct spa_dict_item items[MAX_ITEMS];
+static char values[MAX_ITEMS][32];
+
+static void gen_values(void)
+{
+ uint32_t i, j, idx;
+ static const char chars[] = "abcdefghijklmnopqrstuvwxyz.:*ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ for (i = 0; i < MAX_ITEMS; i++) {
+ for (j = 0; j < 32; j++) {
+ idx = random() % sizeof(chars);
+ values[i][j] = chars[idx];
+ }
+ idx = random() % 16;
+ values[i][idx + 16] = 0;
+ }
+}
+
+static void gen_dict(struct spa_dict *dict, uint32_t n_items)
+{
+ uint32_t i, idx;
+
+ for (i = 0; i < n_items; i++) {
+ idx = random() % MAX_ITEMS;
+ items[i] = SPA_DICT_ITEM_INIT(values[idx], values[idx]);
+ }
+ dict->items = items;
+ dict->n_items = n_items;
+ dict->flags = 0;
+}
+
+static void test_query(const struct spa_dict *dict)
+{
+ uint32_t i, idx;
+ const char *str;
+
+ for (i = 0; i < MAX_COUNT; i++) {
+ idx = random() % dict->n_items;
+ str = spa_dict_lookup(dict, dict->items[idx].key);
+ assert(spa_streq(str, dict->items[idx].value));
+ }
+}
+
+static void test_lookup(struct spa_dict *dict)
+{
+ struct timespec ts;
+ uint64_t t1, t2, t3, t4;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t1 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ test_query(dict);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t2 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "%d elapsed %"PRIu64" count %u = %"PRIu64"/sec\n", dict->n_items,
+ t2 - t1, MAX_COUNT, MAX_COUNT * (uint64_t)SPA_NSEC_PER_SEC / (t2 - t1));
+
+ spa_dict_qsort(dict);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t3 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "%d sort elapsed %"PRIu64"\n", dict->n_items, t3 - t2);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t3 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ test_query(dict);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t4 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "%d elapsed %"PRIu64" count %u = %"PRIu64"/sec %f speedup\n", dict->n_items,
+ t4 - t3, MAX_COUNT, MAX_COUNT * (uint64_t)SPA_NSEC_PER_SEC / (t4 - t3),
+ (double)(t2 - t1) / (t4 - t2));
+}
+
+int main(int argc, char *argv[])
+{
+ struct spa_dict dict;
+
+ spa_zero(dict);
+ gen_values();
+
+ /* warmup */
+ gen_dict(&dict, 1000);
+ test_query(&dict);
+
+ gen_dict(&dict, 10);
+ test_lookup(&dict);
+
+ gen_dict(&dict, 20);
+ test_lookup(&dict);
+
+ gen_dict(&dict, 50);
+ test_lookup(&dict);
+
+ gen_dict(&dict, 100);
+ test_lookup(&dict);
+
+ gen_dict(&dict, 1000);
+ test_lookup(&dict);
+
+ return 0;
+}
diff --git a/spa/tests/benchmark-pod.c b/spa/tests/benchmark-pod.c
new file mode 100644
index 0000000..8af1a5f
--- /dev/null
+++ b/spa/tests/benchmark-pod.c
@@ -0,0 +1,294 @@
+/* Spa
+ *
+ * Copyright © 2019 Wim Taymans
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <time.h>
+
+#include <spa/pod/pod.h>
+#include <spa/pod/builder.h>
+#include <spa/pod/parser.h>
+#include <spa/param/video/format-utils.h>
+#include <spa/debug/pod.h>
+
+#define MAX_COUNT 10000000
+
+static void test_builder(void)
+{
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = { NULL, };
+ struct spa_pod_frame f[2];
+ struct timespec ts;
+ uint64_t t1, t2;
+ uint64_t count = 0;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t1 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "test_builder() : ");
+ for (count = 0; count < MAX_COUNT; count++) {
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+
+ spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Format, 0);
+ spa_pod_builder_prop(&b, SPA_FORMAT_mediaType, 0);
+ spa_pod_builder_id(&b, SPA_MEDIA_TYPE_video);
+ spa_pod_builder_prop(&b, SPA_FORMAT_mediaSubtype, 0);
+ spa_pod_builder_id(&b, SPA_MEDIA_SUBTYPE_raw);
+
+ spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_format, 0);
+ spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_Enum, 0);
+ spa_pod_builder_id(&b, SPA_VIDEO_FORMAT_I420);
+ spa_pod_builder_id(&b, SPA_VIDEO_FORMAT_I420);
+ spa_pod_builder_id(&b, SPA_VIDEO_FORMAT_YUY2);
+ spa_pod_builder_pop(&b, &f[1]);
+
+ struct spa_rectangle size_min_max[] = { {1, 1}, {INT32_MAX, INT32_MAX} };
+ spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_size, 0);
+ spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_Range, 0);
+ spa_pod_builder_rectangle(&b, 320, 240);
+ spa_pod_builder_raw(&b, size_min_max, sizeof(size_min_max));
+ spa_pod_builder_pop(&b, &f[1]);
+
+ struct spa_fraction rate_min_max[] = { {0, 1}, {INT32_MAX, 1} };
+ spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_framerate, 0);
+ spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_Range, 0);
+ spa_pod_builder_fraction(&b, 25, 1);
+ spa_pod_builder_raw(&b, rate_min_max, sizeof(rate_min_max));
+ spa_pod_builder_pop(&b, &f[1]);
+
+ spa_pod_builder_pop(&b, &f[0]);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t2 = SPA_TIMESPEC_TO_NSEC(&ts);
+ if (t2 - t1 > 1 * SPA_NSEC_PER_SEC)
+ break;
+ }
+ fprintf(stderr, "elapsed %"PRIu64" count %"PRIu64" = %"PRIu64"/sec\n",
+ t2 - t1, count, count * (uint64_t)SPA_NSEC_PER_SEC / (t2 - t1));
+}
+
+static void test_builder2(void)
+{
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = { NULL, };
+ struct timespec ts;
+ uint64_t t1, t2;
+ uint64_t count = 0;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t1 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "test_builder2() : ");
+ for (count = 0; count < MAX_COUNT; count++) {
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+
+ spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_Format, 0,
+ SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(3,
+ SPA_VIDEO_FORMAT_I420,
+ SPA_VIDEO_FORMAT_I420,
+ SPA_VIDEO_FORMAT_YUY2),
+ SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
+ &SPA_RECTANGLE(320, 240),
+ &SPA_RECTANGLE(1, 1),
+ &SPA_RECTANGLE(INT32_MAX, INT32_MAX)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
+ &SPA_FRACTION(25,1),
+ &SPA_FRACTION(0,1),
+ &SPA_FRACTION(INT32_MAX,1)));
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t2 = SPA_TIMESPEC_TO_NSEC(&ts);
+ if (t2 - t1 > 1 * SPA_NSEC_PER_SEC)
+ break;
+ }
+ fprintf(stderr, "elapsed %"PRIu64" count %"PRIu64" = %"PRIu64"/sec\n",
+ t2 - t1, count, count * (uint64_t)SPA_NSEC_PER_SEC / (t2 - t1));
+}
+
+static void test_parse(void)
+{
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = { NULL, };
+ struct timespec ts;
+ uint64_t t1, t2;
+ uint64_t count = 0;
+ struct spa_pod *fmt;
+
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+
+ fmt = spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_Format, 0,
+ SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(3,
+ SPA_VIDEO_FORMAT_I420,
+ SPA_VIDEO_FORMAT_I420,
+ SPA_VIDEO_FORMAT_YUY2),
+ SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
+ &SPA_RECTANGLE(320, 240),
+ &SPA_RECTANGLE(1, 1),
+ &SPA_RECTANGLE(INT32_MAX, INT32_MAX)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
+ &SPA_FRACTION(25,1),
+ &SPA_FRACTION(0,1),
+ &SPA_FRACTION(INT32_MAX,1)));
+
+ spa_pod_fixate(fmt);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t1 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "test_parse() : ");
+ for (count = 0; count < MAX_COUNT; count++) {
+ struct {
+ uint32_t media_type;
+ uint32_t media_subtype;
+ uint32_t format;
+ struct spa_rectangle size;
+ struct spa_fraction framerate;
+ } vals;
+ struct spa_pod_prop *prop;
+
+ spa_zero(vals);
+
+ SPA_POD_OBJECT_FOREACH((struct spa_pod_object*)fmt, prop) {
+ uint32_t n_vals, choice;
+ struct spa_pod *pod = spa_pod_get_values(&prop->value, &n_vals, &choice);
+
+ switch(prop->key) {
+ case SPA_FORMAT_mediaType:
+ spa_pod_get_id(pod, &vals.media_type);
+ break;
+ case SPA_FORMAT_mediaSubtype:
+ spa_pod_get_id(pod, &vals.media_subtype);
+ break;
+ case SPA_FORMAT_VIDEO_format:
+ spa_pod_get_id(pod, &vals.format);
+ break;
+ case SPA_FORMAT_VIDEO_size:
+ spa_pod_get_rectangle(pod, &vals.size);
+ break;
+ case SPA_FORMAT_VIDEO_framerate:
+ spa_pod_get_fraction(pod, &vals.framerate);
+ break;
+ default:
+ break;
+ }
+ }
+ spa_assert(vals.media_type == SPA_MEDIA_TYPE_video);
+ spa_assert(vals.media_subtype == SPA_MEDIA_SUBTYPE_raw);
+ spa_assert(vals.format == SPA_VIDEO_FORMAT_I420);
+ spa_assert(vals.size.width == 320 && vals.size.height == 240);
+ spa_assert(vals.framerate.num == 25 && vals.framerate.denom == 1);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t2 = SPA_TIMESPEC_TO_NSEC(&ts);
+ if (t2 - t1 > 1 * SPA_NSEC_PER_SEC)
+ break;
+ }
+ fprintf(stderr, "elapsed %"PRIu64" count %"PRIu64" = %"PRIu64"/sec\n",
+ t2 - t1, count, count * (uint64_t)SPA_NSEC_PER_SEC / (t2 - t1));
+}
+
+static void test_parser(void)
+{
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = { NULL, };
+ struct timespec ts;
+ uint64_t t1, t2;
+ uint64_t count = 0;
+ struct spa_pod *fmt;
+
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+
+ fmt = spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_Format, 0,
+ SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(3,
+ SPA_VIDEO_FORMAT_I420,
+ SPA_VIDEO_FORMAT_I420,
+ SPA_VIDEO_FORMAT_YUY2),
+ SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
+ &SPA_RECTANGLE(320, 240),
+ &SPA_RECTANGLE(1, 1),
+ &SPA_RECTANGLE(INT32_MAX, INT32_MAX)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
+ &SPA_FRACTION(25,1),
+ &SPA_FRACTION(0,1),
+ &SPA_FRACTION(INT32_MAX,1)));
+
+ spa_pod_fixate(fmt);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t1 = SPA_TIMESPEC_TO_NSEC(&ts);
+
+ fprintf(stderr, "test_parser() : ");
+ for (count = 0; count < MAX_COUNT; count++) {
+ struct {
+ uint32_t media_type;
+ uint32_t media_subtype;
+ uint32_t format;
+ struct spa_rectangle size;
+ struct spa_fraction framerate;
+ } vals;
+
+ spa_zero(vals);
+
+ spa_pod_parse_object(fmt,
+ SPA_TYPE_OBJECT_Format, NULL,
+ SPA_FORMAT_mediaType, SPA_POD_Id(&vals.media_type),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(&vals.media_subtype),
+ SPA_FORMAT_VIDEO_format, SPA_POD_Id(&vals.format),
+ SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&vals.size),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&vals.framerate));
+
+ spa_assert(vals.media_type == SPA_MEDIA_TYPE_video);
+ spa_assert(vals.media_subtype == SPA_MEDIA_SUBTYPE_raw);
+ spa_assert(vals.format == SPA_VIDEO_FORMAT_I420);
+ spa_assert(vals.size.width == 320 && vals.size.height == 240);
+ spa_assert(vals.framerate.num == 25 && vals.framerate.denom == 1);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t2 = SPA_TIMESPEC_TO_NSEC(&ts);
+ if (t2 - t1 > 1 * SPA_NSEC_PER_SEC)
+ break;
+ }
+ fprintf(stderr, "elapsed %"PRIu64" count %"PRIu64" = %"PRIu64"/sec\n",
+ t2 - t1, count, count * (uint64_t)SPA_NSEC_PER_SEC / (t2 - t1));
+}
+
+int main(int argc, char *argv[])
+{
+ test_builder();
+ test_builder2();
+ test_parse();
+ test_parser();
+ return 0;
+}
diff --git a/spa/tests/meson.build b/spa/tests/meson.build
new file mode 100644
index 0000000..c73c887
--- /dev/null
+++ b/spa/tests/meson.build
@@ -0,0 +1,58 @@
+# Generate a compilation test for each SPA header, excluding the type-info.h
+# ones which have circular dependencies and take some effort to fix.
+# Do it for C++ if possible (picks up C++-specific errors), otherwise for C.
+find = find_program('find', required: false)
+summary({'find (for header testing)': find.found()}, bool_yn: true, section: 'Optional programs')
+if find.found()
+ spa_headers = run_command(find,
+ meson.project_source_root() / 'spa' / 'include',
+ '-name', '*.h',
+ '-not', '-name', 'type-info.h',
+ '-type', 'f',
+ '-printf', '%P\n',
+ check: false)
+ foreach spa_header : spa_headers.stdout().split('\n')
+ if spa_header.endswith('.h') # skip empty lines
+ ext = have_cpp ? 'cpp' : 'c'
+ src = configure_file(input: 'spa-include-test-template.c',
+ output: 'spa-include-test-@0@.@1@'.format(spa_header.underscorify(), ext),
+ configuration: {
+ 'INCLUDE': spa_header,
+ })
+ executable('spa-include-test-@0@'.format(spa_header.underscorify()),
+ src,
+ dependencies: [ spa_dep ],
+ install: false)
+ endif
+ endforeach
+endif
+
+benchmark_apps = [
+ 'stress-ringbuffer',
+ 'benchmark-pod',
+ 'benchmark-dict',
+]
+
+foreach a : benchmark_apps
+ benchmark('spa-' + a,
+ executable('spa-' + a, a + '.c',
+ dependencies : [ spa_dep, dl_lib, pthread_lib, mathlib ],
+ install : installed_tests_enabled,
+ install_dir : installed_tests_execdir,
+ ),
+ env : [
+ 'SPA_PLUGIN_DIR=@0@'.format(spa_dep.get_variable('plugindir')),
+ ]
+ )
+
+ if installed_tests_enabled
+ test_conf = configuration_data()
+ test_conf.set('exec', installed_tests_execdir / 'spa-' + a)
+ configure_file(
+ input: installed_tests_template,
+ output: 'spa-' + a + '.test',
+ install_dir: installed_tests_metadir,
+ configuration: test_conf,
+ )
+ endif
+endforeach
diff --git a/spa/tests/spa-include-test-template.c b/spa/tests/spa-include-test-template.c
new file mode 100644
index 0000000..078e897
--- /dev/null
+++ b/spa/tests/spa-include-test-template.c
@@ -0,0 +1,5 @@
+#include <@INCLUDE@>
+
+int main(void) {
+ return 0;
+}
diff --git a/spa/tests/stress-ringbuffer.c b/spa/tests/stress-ringbuffer.c
new file mode 100644
index 0000000..6a7e98f
--- /dev/null
+++ b/spa/tests/stress-ringbuffer.c
@@ -0,0 +1,147 @@
+#include <unistd.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <sched.h>
+#include <errno.h>
+#include <semaphore.h>
+
+#include <spa/utils/ringbuffer.h>
+
+#define DEFAULT_SIZE 0x2000
+#define ARRAY_SIZE 63
+#define MAX_VALUE 0x10000
+
+#if defined(__FreeBSD__) || defined(__MidnightBSD__)
+#include <sys/param.h>
+#if (__FreeBSD_version >= 1400000 && __FreeBSD_version < 1400043) \
+ || (__FreeBSD_version < 1300523) || defined(__MidnightBSD__)
+static int sched_getcpu(void) { return -1; };
+#endif
+#endif
+
+static struct spa_ringbuffer rb;
+static uint32_t size;
+static void *data;
+static sem_t sem;
+
+static int fill_int_array(int *array, int start, int count)
+{
+ int i, j = start;
+ for (i = 0; i < count; i++) {
+ array[i] = j;
+ j = (j + 1) % MAX_VALUE;
+ }
+ return j;
+}
+
+static int cmp_array(int *array1, int *array2, int count)
+{
+ int i;
+ for (i = 0; i < count; i++)
+ if (array1[i] != array2[i]) {
+ printf("%d != %d at offset %d\n", array1[i], array2[i], i);
+ return 0;
+ }
+
+ return 1;
+}
+
+static void *reader_start(void *arg)
+{
+ int i = 0, a[ARRAY_SIZE], b[ARRAY_SIZE];
+
+ printf("reader started on cpu: %d\n", sched_getcpu());
+
+ i = fill_int_array(a, i, ARRAY_SIZE);
+
+ while (1) {
+ uint32_t index;
+ int32_t avail;
+
+ avail = spa_ringbuffer_get_read_index(&rb, &index);
+
+ if (avail >= (int32_t)(sizeof(b))) {
+ spa_ringbuffer_read_data(&rb, data, size, index % size, b, sizeof(b));
+ spa_ringbuffer_read_update(&rb, index + sizeof(b));
+
+ if (index >= INT32_MAX - sizeof(a))
+ break;
+
+ spa_assert(cmp_array(a, b, ARRAY_SIZE));
+ i = fill_int_array(a, i, ARRAY_SIZE);
+ }
+ }
+ sem_post(&sem);
+
+ return NULL;
+}
+
+static void *writer_start(void *arg)
+{
+ int i = 0, a[ARRAY_SIZE];
+ printf("writer started on cpu: %d\n", sched_getcpu());
+
+ i = fill_int_array(a, i, ARRAY_SIZE);
+
+ while (1) {
+ uint32_t index;
+ int32_t avail;
+
+ avail = size - spa_ringbuffer_get_write_index(&rb, &index);
+
+ if (avail >= (int32_t)(sizeof(a))) {
+ spa_ringbuffer_write_data(&rb, data, size, index % size, a, sizeof(a));
+ spa_ringbuffer_write_update(&rb, index + sizeof(a));
+
+ if (index >= INT32_MAX - sizeof(a))
+ break;
+
+ i = fill_int_array(a, i, ARRAY_SIZE);
+ }
+ }
+ sem_post(&sem);
+
+ return NULL;
+}
+
+#define exit_error(msg) \
+do { perror(msg); exit(EXIT_FAILURE); } while (0)
+
+int main(int argc, char *argv[])
+{
+ pthread_t reader_thread, writer_thread;
+ struct timespec ts;
+
+ printf("starting ringbuffer stress test\n");
+
+ if (argc > 1)
+ sscanf(argv[1], "%d", &size);
+ else
+ size = DEFAULT_SIZE;
+
+ printf("buffer size (bytes): %d\n", size);
+ printf("array size (bytes): %zd\n", sizeof(int) * ARRAY_SIZE);
+
+ spa_ringbuffer_init(&rb);
+ data = malloc(size);
+
+ if (sem_init(&sem, 0, 0) != 0)
+ exit_error("init_sem");
+
+ pthread_create(&reader_thread, NULL, reader_start, NULL);
+ pthread_create(&writer_thread, NULL, writer_start, NULL);
+
+ if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
+ exit_error("clock_gettime");
+
+ ts.tv_sec += 2;
+
+ while (sem_timedwait(&sem, &ts) == -1 && errno == EINTR)
+ continue;
+ while (sem_timedwait(&sem, &ts) == -1 && errno == EINTR)
+ continue;
+
+ printf("read %u, written %u\n", rb.readindex, rb.writeindex);
+
+ return 0;
+}