toys

toys.git
git clone git://git.lenczewski.org/toys.git
Log | Files | Refs | README | LICENSE

commit ab269344858660181fddd53b90e5a3f788f79aac
parent 027d9b04bb1fae62fbb1c842395ad1dacacc121f
Author: MikoĊ‚aj Lenczewski <mikolaj@lenczewski.org>
Date:   Tue,  9 Dec 2025 12:39:28 +0000

Update queue benchmark

Diffstat:
Mbuild.sh | 2+-
Mqueue.c | 28+++++++++++++++++++---------
2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/build.sh b/build.sh @@ -1,7 +1,7 @@ #!/bin/sh WARNINGS="-Wall -Wextra -Wno-format-pedantic -Wno-unused-variable" -FLAGS="$WARNINGS -std=c11 -O0 -g3" +FLAGS="$WARNINGS -std=c11 -O3 -g3" set -ex diff --git a/queue.c b/queue.c @@ -15,8 +15,8 @@ writer(void *data) { struct spsc_queue *queue = data; + size_t *ptr; for (size_t i = 0; i < limit; i++) { - size_t *ptr; do { ptr = spsc_queue_write(queue, sizeof *ptr); } while (!ptr); @@ -26,7 +26,7 @@ writer(void *data) spsc_queue_write_commit(queue, sizeof *ptr); } - return NULL; + return (void *) (sizeof *ptr * limit); // return bytes written } static void * @@ -34,8 +34,8 @@ reader(void *data) { struct spsc_queue *queue = data; + size_t *ptr; for (size_t i = 0; i < limit; i++) { - size_t *ptr; do { ptr = spsc_queue_read(queue, sizeof *ptr); } while (!ptr); @@ -45,7 +45,7 @@ reader(void *data) spsc_queue_read_commit(queue, sizeof *ptr); } - return NULL; + return (void *) (sizeof *ptr * limit); // return bytes read } static void @@ -55,6 +55,7 @@ benchmark(void) int res = spsc_queue_init(&queue, qsize, qsize); assert(res == 0); + const uint64_t nsecs = 1000000000, usecs = 1000000, msecs = 1000; struct timespec start, end; pthread_t writer_thread, reader_thread; @@ -63,16 +64,25 @@ benchmark(void) clock_gettime(CLOCK_MONOTONIC_RAW, &start); - pthread_join(writer_thread, NULL); - pthread_join(reader_thread, NULL); + void *nbytes_written; + pthread_join(writer_thread, &nbytes_written); + + void *nbytes_read; + pthread_join(reader_thread, &nbytes_read); clock_gettime(CLOCK_MONOTONIC_RAW, &end); - uint64_t ns = ((end.tv_sec - start.tv_sec) * 1000000000) + (end.tv_nsec - start.tv_nsec); + uint64_t ns = ((end.tv_sec - start.tv_sec) * nsecs) + (end.tv_nsec - start.tv_nsec); + uint64_t bytes = (uint64_t) nbytes_written + (uint64_t) nbytes_read; + double gigs = bytes / (double) GiB(1); printf("Iters: %zu\n", limit); - printf("Elapsed time: ms: %lu, ns: %lu\n", ns / 1000000, ns); - printf("Ops/sec: %lu\n", (limit * 1000000000) / ns); + printf("Elapsed time: ms: %lu, ns: %lu, avg. ns per iter: %lu\n", + ns / usecs, ns, ns / limit); + + printf("Ops/sec: %lu, bytes written: %lu, bytes read: %lu, total GiBps: %.03f\n", + (limit * nsecs) / ns, (uint64_t) nbytes_written, (uint64_t) nbytes_read, + (float) ((gigs * nsecs) / ns)); } int