diff --git a/example/benchmark.c b/example/benchmark.c new file mode 100644 index 00000000..fa051c31 --- /dev/null +++ b/example/benchmark.c @@ -0,0 +1,49 @@ + +/* + * fluent. + * + * 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) +{ + ft_t env = arg; + printf("worker started.\n"); + while (ft_is_online(env)) { + csw++; + ft_sleep(env, 0); + } + printf("worker done.\n"); +} + +static void +benchmark_runner(void *arg) +{ + ft_t env = arg; + printf("benchmark started.\n"); + ft_create(env, benchmark_worker, env); + ft_sleep(env, 1000); + printf("done.\n"); + printf("context switches %d in 1 sec.\n", csw); + ft_stop(env); +} + +int +main(int argc, char *argv[]) +{ + ft_t env = ft_new(); + ft_create(env, benchmark_runner, env); + ft_start(env); + ft_free(env); + return 0; +} diff --git a/example/makefile b/example/makefile new file mode 100644 index 00000000..db749454 --- /dev/null +++ b/example/makefile @@ -0,0 +1,18 @@ +CC = gcc +RM = rm +CFLAGS = -I. -Wall -g -O0 -I../lib +LFLAGS = ../lib/libfluent.a -luv +all: validate clean benchmark +benchmark: benchmark.o + $(CC) benchmark.o $(LFLAGS) -o benchmark +validate: + @if [ ! -f ../lib/libfluent.a ]; then \ + echo ""; \ + echo "Please build the library first."; \ + echo ""; \ + exit 1; \ + fi +.c.o: + $(CC) $(CFLAGS) -c $< +clean: + $(RM) -f benchmark benchmark.o