~kdsch/sampler

fed5213628d4313cc2c116e0f751c28511c5d115 — Karl Schultheisz 4 months ago eb4b5ac
posix: add benchmark
3 files changed, 76 insertions(+), 0 deletions(-)

A posix/benchmark/main.c
A posix/benchmark/meson.build
M posix/meson.build
A posix/benchmark/main.c => posix/benchmark/main.c +56 -0
@@ 0,0 1,56 @@
#include "app.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#define run(fn) bm_run(#fn, fn)

static void
benchmark_app_proc(uint32_t iterations)
{
	struct app app;
	app_init(&app);

	for (uint32_t i = 0; i < iterations; i++) {
		app_proc(&app);
	}
}

static float
clock_now()
{
	struct timespec now = {0};
	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
	return now.tv_sec + (float)now.tv_nsec / 1e9f;
}

static void
bm_report(const char *name, uint32_t iterations, float duration)
{
	printf("%s: %e s/call %e calls/s\n", name, duration/iterations,
		iterations/duration);
}

static void
bm_run(const char *name, void (*func)(uint32_t iterations))
{
	const float goal_duration = 1;

	for (uint32_t n = 1; n < 1000000000; n = 3*(n+1)/2) {
		const float start = clock_now();
		func(n);
		const float end = clock_now();
		const float duration = end - start;

		if (duration > goal_duration) {
			bm_report(name, n, duration);
			return;
		}
	}
}

int
main(void)
{
	run(benchmark_app_proc);
	return 0;
}

A posix/benchmark/meson.build => posix/benchmark/meson.build +19 -0
@@ 0,0 1,19 @@
benchmark = executable(
	'benchmark-pure',
	sources: files(
		'main.c',
	),
	c_args: flags_posix,
	dependencies: [
		libm,
	],
	link_with: [
		pure,
	],
	include_directories: [
		pure_inc,
	],
	native: true,
)

benchmark('benchmark-pure', benchmark)

M posix/meson.build => posix/meson.build +1 -0
@@ 4,5 4,6 @@ flags_posix = [
	'-D_POSIX_C_SOURCE=200809L',
]
subdir('app')
subdir('benchmark')
subdir('table-generator')
subdir('test')