turbonss/src/cmph_benchmark.c

130 lines
3.4 KiB
C
Raw Normal View History

2011-02-14 00:40:26 +02:00
// A simple benchmark tool around getrusage
2011-01-24 14:29:22 +02:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
2011-02-15 21:49:08 +02:00
#include <string.h>
2011-02-19 00:15:10 +02:00
#include <sys/resource.h>
2011-01-24 14:29:22 +02:00
#include "cmph_benchmark.h"
typedef struct {
const char* name;
void (*func)(int);
2011-02-15 21:49:08 +02:00
int iters;
2011-01-24 14:29:22 +02:00
struct rusage begin;
struct rusage end;
} benchmark_t;
static benchmark_t* global_benchmarks = NULL;
/* Subtract the `struct timeval' values X and Y,
storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0. */
int timeval_subtract (
struct timeval *result, struct timeval *x, struct timeval* y) {
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_usec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
benchmark_t* find_benchmark(const char* name) {
benchmark_t* benchmark = global_benchmarks;
2011-02-14 00:40:26 +02:00
while (benchmark && benchmark->name != NULL) {
if (strcmp(benchmark->name, name) == 0) break;
++benchmark;
}
if (!benchmark || !benchmark->name) return NULL;
2011-01-24 14:29:22 +02:00
return benchmark;
}
int global_benchmarks_length() {
2011-02-14 00:40:26 +02:00
benchmark_t* benchmark = global_benchmarks;
2011-01-24 14:29:22 +02:00
int length = 0;
2011-02-14 00:40:26 +02:00
if (benchmark == NULL) return 0;
while (benchmark->name != NULL) ++length, ++benchmark;
2011-01-24 14:29:22 +02:00
return length;
}
void bm_register(const char* name, void (*func)(int), int iters) {
benchmark_t benchmark;
int length = global_benchmarks_length();
benchmark.name = name;
benchmark.func = func;
2011-02-15 21:49:08 +02:00
benchmark.iters = iters;
2011-01-24 14:29:22 +02:00
assert(!find_benchmark(name));
2011-02-14 00:40:26 +02:00
global_benchmarks = realloc(
global_benchmarks, (length + 2)*sizeof(benchmark_t));
2011-01-24 14:29:22 +02:00
global_benchmarks[length] = benchmark;
2011-02-14 00:40:26 +02:00
memset(&benchmark, 0, sizeof(benchmark_t)); // pivot
global_benchmarks[length + 1] = benchmark;
2011-01-24 14:29:22 +02:00
}
void bm_start(const char* name) {
benchmark_t* benchmark;
struct rusage rs;
benchmark = find_benchmark(name);
2011-02-14 00:40:26 +02:00
assert(benchmark);
2011-01-24 14:29:22 +02:00
int ret = getrusage(RUSAGE_SELF, &rs);
if (ret != 0) {
perror("rusage failed");
exit(-1);
}
benchmark->begin = rs;
2011-02-15 21:49:08 +02:00
(*benchmark->func)(benchmark->iters);
2011-01-24 14:29:22 +02:00
}
void bm_end(const char* name) {
benchmark_t* benchmark;
struct rusage rs;
int ret = getrusage(RUSAGE_SELF, &rs);
if (ret != 0) {
perror("rusage failed");
exit(-1);
}
benchmark = find_benchmark(name);
benchmark->end = rs;
struct timeval utime;
timeval_subtract(&utime, &benchmark->end.ru_utime, &benchmark->begin.ru_utime);
struct timeval stime;
timeval_subtract(&stime, &benchmark->end.ru_stime, &benchmark->begin.ru_stime);
2011-02-14 00:40:26 +02:00
printf("Benchmark: %s\n", benchmark->name);
2011-06-13 08:16:19 +03:00
printf("User time used : %ld.%06ld\n",
utime.tv_sec, (long int)utime.tv_usec);
printf("System time used: %ld.%06ld\n",
stime.tv_sec, (long int)stime.tv_usec);
2011-02-14 00:40:26 +02:00
printf("\n");
2011-01-24 14:29:22 +02:00
}
2011-02-14 00:40:26 +02:00
void run_benchmarks(int argc, char** argv) {
benchmark_t* benchmark = global_benchmarks;
while (benchmark && benchmark->name != NULL) {
bm_start(benchmark->name);
bm_end(benchmark->name);
++benchmark;
}
}