Add benchmark macros

This commit is contained in:
c9s 2014-05-17 07:48:22 +08:00
parent 6747775908
commit f5863a956d
5 changed files with 125 additions and 35 deletions

View file

@ -7,7 +7,7 @@
TESTS = check_tree TESTS = check_tree
# noinst_PROGRAMS = $(TESTS) # noinst_PROGRAMS = $(TESTS)
check_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_LDADD=$(DEPS_LIBS) -L$(top_builddir)/src -lr3 @CHECK_LIBS@
check_tree_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include @CHECK_CFLAGS@ check_tree_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include @CHECK_CFLAGS@
# AM_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir)/include # AM_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir)/include

56
tests/bench.c Normal file
View file

@ -0,0 +1,56 @@
/*
* bench.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include "bench.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
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) );
}

51
tests/bench.h Normal file
View file

@ -0,0 +1,51 @@
/*
* bench.h
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#ifndef BENCH_H
#define BENCH_H
#include <stdlib.h>
#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 */

View file

@ -52,3 +52,11 @@
1400282802,11627473.22 1400282802,11627473.22
1400282839,10222898.61 1400282839,10222898.61
1400282930,11960583.69 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

1 1400242718 5649455.80
52 1400282802 11627473.22
53 1400282839 10222898.61
54 1400282930 11960583.69
55 1400283101 11659005.75
56 1400283479 11578012.31
57 1400283485 10742869.09
58 1400283755 11545078.70
59 1400283875 3785403.91
60 1400284002 2955730.95
61 1400284062 3701787.14
62 1400284077 3849171.15

View file

@ -5,37 +5,8 @@
#include "r3.h" #include "r3.h"
#include "r3_str.h" #include "r3_str.h"
#include "str_array.h" #include "str_array.h"
#include "bench.h"
#include <sys/time.h>
#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) START_TEST (test_ltrim_slash)
@ -640,19 +611,23 @@ START_TEST(benchmark_str)
printf("Benchmarking...\n"); printf("Benchmarking...\n");
BENCHMARK(string_dispatch)
/*
double s = microtime(); double s = microtime();
long N = 5000000; long N = 5000000;
for (int i = 0; i < 5000000 ; i++ ) { for (int i = 0; i < 5000000 ; i++ ) {
*/
r3_tree_match(n , "/qux/bar/corge", strlen("/qux/bar/corge"), NULL); r3_tree_match(n , "/qux/bar/corge", strlen("/qux/bar/corge"), NULL);
/*
} }
double e = microtime(); double e = microtime();
*/
END_BENCHMARK()
printf("%ld iterations ", N); bench_summary(&B);
printf("finished in %lf seconds\n", e - s );
printf("%.2f i/sec\n", N / (e - s) );
FILE *fp = fopen("bench_str.csv", "a+"); 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); fclose(fp);
} }