diff --git a/examples/benchmark.c b/examples/benchmark.c new file mode 100644 index 00000000..9655357f --- /dev/null +++ b/examples/benchmark.c @@ -0,0 +1,49 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +/* + * This example shows fiber context switch (yield) + * performance done in one second. +*/ + +#include + +static int csw = 0; + +static void +benchmark_worker(void *arg) +{ + machine_t machine = arg; + printf("worker started.\n"); + while (machine_active(machine)) { + csw++; + machine_sleep(machine, 0); + } + printf("worker done.\n"); +} + +static void +benchmark_runner(void *arg) +{ + machine_t machine = arg; + printf("benchmark started.\n"); + machine_create_fiber(machine, benchmark_worker, machine); + machine_sleep(machine, 1000); + printf("done.\n"); + printf("context switches %d in 1 sec.\n", csw); + machine_stop(machine); +} + +int +main(int argc, char *argv[]) +{ + machine_t machine = machine_create(); + machine_create_fiber(machine, benchmark_runner, machine); + machine_start(machine); + machine_free(machine); + return 0; +} diff --git a/examples/makefile b/examples/makefile new file mode 100644 index 00000000..d8f19de5 --- /dev/null +++ b/examples/makefile @@ -0,0 +1,11 @@ +CC = gcc +RM = rm +CFLAGS = -I. -Wall -g -O0 -I../src +LFLAGS_LIB = ../src/libmachinarium.a -pthread -lssl -lcrypto +LFLAGS = $(LFLAGS_LIB) +EXAMPLES = benchmark +all: clean $(EXAMPLES) +benchmark: + $(CC) $(CFLAGS) benchmark.c $(LFLAGS) -o benchmark +clean: + $(RM) -f $(EXAMPLES)