bench: pass N as argument, update readme
authornsz <nsz@port70.net>
Fri, 29 Jul 2011 12:33:48 +0000 (14:33 +0200)
committernsz <nsz@port70.net>
Fri, 29 Jul 2011 12:33:48 +0000 (14:33 +0200)
Makefile.inc
README
common/b.c
common/t.c
common/test.h
src/foo/foo.c
src/malloc/bench.c
src/stdio/bench.c

index 10f3e8a..5f1d614 100644 (file)
@@ -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 (file)
--- 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
index b82531c..6df833a 100644 (file)
@@ -9,9 +9,9 @@
 #include <unistd.h>
 #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;
 }
index 02d77c7..f42054b 100644 (file)
@@ -9,9 +9,9 @@
 #include <unistd.h>
 #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();
 }
index ed8adda..f1abf56 100644 (file)
@@ -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);
index d9ebb82..033557d 100644 (file)
@@ -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++)
index 28ae66d..3cc33d2 100644 (file)
@@ -4,7 +4,7 @@
 #include <pthread.h>
 #include "test.h"
 
-void bench_malloc_sparse() {
+void bench_malloc_sparse(int N) {
        void *p[N];
        size_t i;
        for (i=0; i<sizeof p/sizeof *p; i++) {
@@ -15,7 +15,7 @@ void bench_malloc_sparse() {
                if (i%150) free(p[i]);
 }
 
-void bench_malloc_bubble() {
+void bench_malloc_bubble(int N) {
        void *p[N];
        size_t i;
        for (i=0; i<sizeof p/sizeof *p; i++) {
@@ -26,7 +26,7 @@ void bench_malloc_bubble() {
                free(p[i]);
 }
 
-void bench_malloc_tiny1() {
+void bench_malloc_tiny1(int N) {
        void **p = malloc(N * sizeof *p);
        size_t i;
        for (i=0; i<N; i++) {
@@ -38,7 +38,7 @@ void bench_malloc_tiny1() {
        free(p);
 }
 
-void bench_malloc_tiny2() {
+void bench_malloc_tiny2(int N) {
        void **p = malloc(N * sizeof *p);
        size_t i;
        for (i=0; i<N; i++) {
@@ -50,7 +50,7 @@ void bench_malloc_tiny2() {
        free(p);
 }
 
-void bench_malloc_big1() {
+void bench_malloc_big1(int N) {
        void *p[N];
        size_t i;
        for (i=0; i<sizeof p/sizeof *p; i++) {
@@ -61,7 +61,7 @@ void bench_malloc_big1() {
        }
 }
 
-void bench_malloc_big2() {
+void bench_malloc_big2(int N) {
        void *p[N];
        size_t i;
        for (i=0; i<sizeof p/sizeof *p; i++) {
@@ -88,6 +88,7 @@ static unsigned rng(unsigned *r)
        return *r = *r * 1103515245 + 12345;
 }
 
+static int N;
 
 static void *stress(void *arg)
 {
@@ -116,26 +117,27 @@ static void *stress(void *arg)
        return 0;
 }
 
-void bench_malloc_thread_stress() {
+void bench_malloc_thread_stress(int n) {
        struct foo foo[SH_COUNT] = {{0}};
        pthread_t td1, td2;
        void *res;
 
+       N = n;
        pthread_create(&td1, 0, stress, foo);
        pthread_create(&td2, 0, stress, foo);
        pthread_join(td1, &res);
        pthread_join(td2, &res);
 }
 
-void bench_malloc_thread_local() {
+void bench_malloc_thread_local(int n) {
        struct foo foo1[SH_COUNT] = {{0}};
        struct foo foo2[SH_COUNT] = {{0}};
        pthread_t td1, td2;
        void *res;
 
+       N = n;
        pthread_create(&td1, 0, stress, foo1);
        pthread_create(&td2, 0, stress, foo2);
        pthread_join(td1, &res);
        pthread_join(td2, &res);
 }
-
index 625a217..ade4095 100644 (file)
@@ -4,7 +4,7 @@
 #include <string.h>
 #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;