add a benchmark framework

This commit is contained in:
Yanyan Jiang 2017-04-20 14:41:59 -04:00
parent 9a8c62fb5c
commit 687f400a57
6 changed files with 132 additions and 0 deletions

View File

@ -1,6 +1,8 @@
#ifndef __TYPES_H__
#define __TYPES_H__
#include <unistd.h>
typedef char i8;
typedef unsigned char u8;
typedef short int i16;

View File

@ -0,0 +1,48 @@
#ifndef __BENCHMARK_H__
#define __BENCHMARK_H__
#include <am.h>
#ifdef __cplusplus
extern "C" {
#endif
#define REPEAT 10
// memory usages
#define MB * 1024 * 1024
#define KB * 1024
// the list of benchmarks
// name
#define BENCHMARK_LIST(V) \
V( qsort, "qsort", 64 MB, true) \
V( rbtree, "rbtree", 1024 MB, true) \
#define DECL(name, sname, mlim, enabled) \
void bench_##name##_prepare(); \
void bench_##name##_run();
BENCHMARK_LIST(DECL)
typedef struct Benchmark {
void (*prepare)();
void (*run)();
const char *name;
ulong mlim;
int enabled;
} Benchmark;
typedef struct Result {
ulong tsc, msec;
ulong score;
} Result;
void prepare(Result *res);
void done(Result *res);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,14 @@
#include <benchmark.h>
void prepare(Result *res) {
res->tsc = _cycles();
res->msec = _uptime();
// try best to flush cache and other resources
// log timer, tsc
}
void done(Result *res) {
// get timer, tsc
// generate report
}

View File

@ -0,0 +1,53 @@
#include <am.h>
#include <benchmark.h>
#define ENTRY(nm, snm, ml, en) \
{ .prepare = bench_##nm##_prepare, \
.run = bench_##nm##_run, \
.name = snm, .mlim = ml, .enabled = en },
Benchmark benchmarks[] = {
BENCHMARK_LIST(ENTRY)
};
const char *check(Benchmark &bench) {
if (!bench.enabled) {
return "not enabled";
}
// insufficient heap
return NULL;
}
int main() {
// the benchmark driver
_trm_init();
_ioe_init();
Result res;
for (auto &bench: benchmarks) {
const char *msg = check(bench);
if (msg != NULL) {
// printf("Not executed. %s", msg);
continue;
}
ulong tsc = 0, msec = 0;
for (int i = 0; i < REPEAT; i ++) {
_putc(bench.name[0]);
bench.prepare(); // prepare first
prepare(&res); // clean everything, start timer
bench.run(); // run it
done(&res); // collect results
tsc += res.tsc;
msec += res.msec;
}
tsc /= REPEAT;
msec /= REPEAT; // TODO: handle overflow
_putc('\n');
}
_halt(0);
}

View File

@ -0,0 +1,7 @@
#include <benchmark.h>
void bench_qsort_prepare() {
}
void bench_qsort_run() {
}

View File

@ -0,0 +1,8 @@
#include <benchmark.h>
void bench_rbtree_prepare() {
}
void bench_rbtree_run() {
}