From f5863a956d2565cdae1fc7bba302cbe5f382a462 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 17 May 2014 07:48:22 +0800 Subject: [PATCH] Add benchmark macros --- tests/Makefile.am | 2 +- tests/bench.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ tests/bench.h | 51 +++++++++++++++++++++++++++++++++++++++++ tests/bench_str.csv | 8 +++++++ tests/check_tree.c | 43 ++++++++-------------------------- 5 files changed, 125 insertions(+), 35 deletions(-) create mode 100644 tests/bench.c create mode 100644 tests/bench.h diff --git a/tests/Makefile.am b/tests/Makefile.am index 3a52c81..565646f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,7 +7,7 @@ TESTS = check_tree # noinst_PROGRAMS = $(TESTS) check_PROGRAMS = $(TESTS) -check_tree_SOURCES = check_tree.c +check_tree_SOURCES = check_tree.c bench.c check_tree_LDADD=$(DEPS_LIBS) -L$(top_builddir)/src -lr3 @CHECK_LIBS@ check_tree_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include @CHECK_CFLAGS@ # AM_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir)/include diff --git a/tests/bench.c b/tests/bench.c new file mode 100644 index 0000000..aa55b7c --- /dev/null +++ b/tests/bench.c @@ -0,0 +1,56 @@ +/* + * bench.c + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ + +#include "bench.h" +#include +#include +#include + +long unixtime() { + struct timeval tp; + long sec = 0L; + if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) { + return tp.tv_sec; + } + return 0; +} + +double microtime() { + struct timeval tp; + long sec = 0L; + double msec = 0.0; + char ret[100]; + + if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) { + msec = (double) (tp.tv_usec / MICRO_IN_SEC); + sec = tp.tv_sec; + if (msec >= 1.0) + msec -= (long) msec; + return sec + msec; + } + return 0; +} + + +void bench_start(bench *b) { + b->start = microtime(); +} + +void bench_stop(bench *b) { + b->end = microtime(); +} + +double bench_iteration_speed(bench *b) { + return b->N / (b->end - b->start); +} + +void bench_summary(bench *b) { + printf("%ld runs, ", b->R); + printf("%ld iterations, ", b->N); + printf("finished in %lf seconds\n", b->end - b->start ); + printf("%.2f i/sec\n", b->N / (b->end - b->start) ); +} diff --git a/tests/bench.h b/tests/bench.h new file mode 100644 index 0000000..3c8dc40 --- /dev/null +++ b/tests/bench.h @@ -0,0 +1,51 @@ +/* + * bench.h + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ + +#ifndef BENCH_H +#define BENCH_H + +#include + +#define MICRO_IN_SEC 1000000.00 +#define SEC_IN_MIN 60 +#define NUL '\0' + + +typedef struct { + long N; // N for each run + long R; // runs + double secs; + double start; + double end; +} bench; + + +#define BENCHMARK(name) \ + bench B; B.N = 5000000; B.R = 3; \ + bench_start(&B); \ + for (int _r = 0; _r < B.R ; _r++ ) { \ + for (int _i = 0; _i < B.N ; _i++ ) { + +#define END_BENCHMARK() \ + } \ + } \ + bench_stop(&B); + + +long unixtime(); + +double microtime(); + +void bench_start(bench *b); + +void bench_stop(bench *b); + +double bench_iteration_speed(bench *b); + +void bench_summary(bench *b); + +#endif /* !BENCH_H */ diff --git a/tests/bench_str.csv b/tests/bench_str.csv index b776393..fcf39ae 100644 --- a/tests/bench_str.csv +++ b/tests/bench_str.csv @@ -52,3 +52,11 @@ 1400282802,11627473.22 1400282839,10222898.61 1400282930,11960583.69 +1400283101,11659005.75 +1400283479,11578012.31 +1400283485,10742869.09 +1400283755,11545078.70 +1400283875,3785403.91 +1400284002,2955730.95 +1400284062,3701787.14 +1400284077,3849171.15 diff --git a/tests/check_tree.c b/tests/check_tree.c index f36e2af..e57b060 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -5,37 +5,8 @@ #include "r3.h" #include "r3_str.h" #include "str_array.h" +#include "bench.h" -#include - -#define MICRO_IN_SEC 1000000.00 -#define SEC_IN_MIN 60 -#define NUL '\0' - -long unixtime() { - struct timeval tp; - long sec = 0L; - if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) { - return tp.tv_sec; - } - return 0; -} - -double microtime() { - struct timeval tp; - long sec = 0L; - double msec = 0.0; - char ret[100]; - - if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) { - msec = (double) (tp.tv_usec / MICRO_IN_SEC); - sec = tp.tv_sec; - if (msec >= 1.0) - msec -= (long) msec; - return sec + msec; - } - return 0; -} START_TEST (test_ltrim_slash) @@ -640,19 +611,23 @@ START_TEST(benchmark_str) printf("Benchmarking...\n"); + BENCHMARK(string_dispatch) + /* double s = microtime(); long N = 5000000; for (int i = 0; i < 5000000 ; i++ ) { + */ r3_tree_match(n , "/qux/bar/corge", strlen("/qux/bar/corge"), NULL); + /* } double e = microtime(); + */ + END_BENCHMARK() - printf("%ld iterations ", N); - printf("finished in %lf seconds\n", e - s ); - printf("%.2f i/sec\n", N / (e - s) ); + bench_summary(&B); FILE *fp = fopen("bench_str.csv", "a+"); - fprintf(fp, "%ld,%.2f\n", unixtime(), N / (e - s)); + fprintf(fp, "%ld,%.2f\n", unixtime(), B.N / (B.end - B.start)); fclose(fp); }