From 7d87d3f6a765bea943beb384f9bbf359e3768c0a Mon Sep 17 00:00:00 2001 From: nsz Date: Fri, 29 Jul 2011 14:33:48 +0200 Subject: [PATCH] bench: pass N as argument, update readme --- Makefile.inc | 10 +++++----- README | 20 +++++++++++++------- common/b.c | 25 +++++++++++++++++-------- common/t.c | 9 ++++----- common/test.h | 1 - src/foo/foo.c | 2 +- src/malloc/bench.c | 20 +++++++++++--------- src/stdio/bench.c | 4 ++-- 8 files changed, 53 insertions(+), 38 deletions(-) diff --git a/Makefile.inc b/Makefile.inc index 10f3e8a..5f1d614 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -35,30 +35,30 @@ endif all: t b clean: - rm -f $(OBJS) t t.o main.h b b.o tests.a + rm -f $(OBJS) t t.o b b.o tests.a tests.h .c.o: $(CC) $(CFLAGS) $(INC) -c -o $@ $< $(OBJS): $(ROOTDIR)/common/test.h $(ROOTDIR)/Makefile.conf -main.h: $(OBJS) +tests.h: $(OBJS) nm -f posix $+ |awk ' \ /^test/ && $$2=="T"{print "T(" $$1 ")"} \ /^bench/ && $$2=="T"{print "B(" $$1 ")"} \ - ' >main.h + ' >tests.h tests.a: $(OBJS) $(AR) rc $@ $+ $(RANLIB) $@ -t.o: $(ROOTDIR)/common/t.c $(ROOTDIR)/common/test.h main.h +t.o: $(ROOTDIR)/common/t.c $(ROOTDIR)/common/test.h tests.h $(CC) $(CFLAGS) $(INC) -I. -c -o $@ $< t: t.o tests.a $(CC) $+ $(LDFLAGS) -o $@ -b.o: $(ROOTDIR)/common/b.c $(ROOTDIR)/common/test.h main.h +b.o: $(ROOTDIR)/common/b.c $(ROOTDIR)/common/test.h tests.h $(CC) $(CFLAGS) $(INC) -I. -c -o $@ $< b: b.o tests.a diff --git a/README b/README index 856e2a8..80ca81e 100644 --- a/README +++ b/README @@ -1,6 +1,7 @@ simple libc tests based on the libc-testsuit and libc-bench of dalias see http://git.etalabs.net/cgi-bin/gitweb.cgi +see http://www.etalabs.net/libc-bench.html build tests: cp Makefile.conf.def Makefile.conf @@ -11,17 +12,14 @@ run tests: run benchmarks: ./b -about the framework: +framework: the only hook in the test framework is error(...) which prints a formatted message and sets the test to failed see common/test.h -benchmark functions are repeatedly called with increasing -N until runing time is long enough, then time/N is printed - -in the root dir make builds an executable with all tests -in a src/* dir make builds only local tests +in the root directory make builds an executable with all tests +in a src/* directory make builds only local tests see Makefile.inc a test function looks like @@ -33,6 +31,14 @@ extern functions with name ~ /^test/ are recognized to be test functions see Makefile.inc -edit the generated main.h to exclude certain tests +a benchmark function looks like + void bench_foo(int N) { + for (i = 0; i < N; i++) + foo(); + } +benchmark functions are repeatedly called with an increasing +N until the runing time is long enough, then time/N is printed + +edit the generated tests.h to exclude certain tests see common/t.c for tests see common/b.c for benchmarks diff --git a/common/b.c b/common/b.c index b82531c..6df833a 100644 --- a/common/b.c +++ b/common/b.c @@ -9,9 +9,9 @@ #include #include "test.h" -#define T(t) -#define B(t) void t(); -#include "main.h" +#define T(f) +#define B(f) void f(int); +#include "tests.h" #undef B static int verbose = 1; @@ -20,7 +20,7 @@ void error__(const char *n, int l, const char *s, ...) { fprintf(stderr, "use error in tests only\n"); } -int N; +static int N; static unsigned long long start; static unsigned long long dt; //static unsigned long long bytes; @@ -57,6 +57,7 @@ void reset_timer() { static int nextN() { unsigned long long n = dt/N; + unsigned long long i; if (n) n = SEC/2/n; @@ -69,7 +70,14 @@ static int nextN() { n = N+1; if (n > MAXN) n = MAXN; - return n; + + /* round up to a nice number */ + for (i = 1; i < n; i *= 10); + if (i/2 >= n) + i /= 2; + if (i/2 >= n) + i /= 2; + return i; } void vmstats() { @@ -103,7 +111,7 @@ void stats() { fputc('\n', stderr); } -static void run(const char *name, void (*f)()) { +static void run(const char *name, void (*f)(int)) { int p = fork(); if (p) { int s; @@ -113,9 +121,10 @@ static void run(const char *name, void (*f)()) { } fprintf(stderr, "%-32s", name); for (N=1; ; N=nextN()) { + // TODO: fork at each iteration and pass N,dt..? reset_timer(); start_timer(); - f(); + f(N); stop_timer(); // fprintf(stderr, "%10d%12llu next: %d\n", N, dt, nextN()); if (dt >= SEC/2 || N >= MAXN) { @@ -131,6 +140,6 @@ static void run(const char *name, void (*f)()) { int main() { #define B(t) run(#t, t); -#include "main.h" +#include "tests.h" return 0; } diff --git a/common/t.c b/common/t.c index 02d77c7..f42054b 100644 --- a/common/t.c +++ b/common/t.c @@ -9,9 +9,9 @@ #include #include "test.h" -#define B(t) -#define T(t) void t(); -#include "main.h" +#define B(f) +#define T(f) void f(); +#include "tests.h" #undef T static int failed; @@ -22,7 +22,6 @@ static int count; static int nfailed; static void errtimer() { error("use *_timer in benchmarks only\n"); } -int N = 0; void start_timer() { errtimer(); } void stop_timer() { errtimer(); } void reset_timer() { errtimer(); } @@ -79,6 +78,6 @@ static int summary() { int main() { #define T(t) run(#t, t); -#include "main.h" +#include "tests.h" return summary(); } diff --git a/common/test.h b/common/test.h index ed8adda..f1abf56 100644 --- a/common/test.h +++ b/common/test.h @@ -3,7 +3,6 @@ void error__(const char *n, int l, const char *s, ...); /* use it in bench_ functions */ -extern int N; void start_timer(void); void stop_timer(void); void reset_timer(void); diff --git a/src/foo/foo.c b/src/foo/foo.c index d9ebb82..033557d 100644 --- a/src/foo/foo.c +++ b/src/foo/foo.c @@ -14,7 +14,7 @@ static int foo(int n) { return n; } -void bench_foo() { +void bench_foo(int N) { int i; for (i = 0; i < N; i++) diff --git a/src/malloc/bench.c b/src/malloc/bench.c index 28ae66d..3cc33d2 100644 --- a/src/malloc/bench.c +++ b/src/malloc/bench.c @@ -4,7 +4,7 @@ #include #include "test.h" -void bench_malloc_sparse() { +void bench_malloc_sparse(int N) { void *p[N]; size_t i; for (i=0; i #include "test.h" -void bench_stdio_putcgetc() { +void bench_stdio_putcgetc(int N) { FILE *f = tmpfile(); size_t i; size_t cs = 0; @@ -19,7 +19,7 @@ void bench_stdio_putcgetc() { abort(); } -void bench_stdio_putcgetc_unlocked() { +void bench_stdio_putcgetc_unlocked(int N) { FILE *f = tmpfile(); size_t i; size_t cs = 0; -- 2.20.1