r3/tests/bench.c
2014-05-22 17:03:33 +08:00

60 lines
1.2 KiB
C

/*
* 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->R) / (b->end - b->start);
}
double bench_duration(bench *b) {
return (b->end - b->start);
}
void bench_print_summary(bench *b) {
printf("%ld runs, ", b->R);
printf("%ld iterations each run, ", b->N);
printf("finished in %lf seconds\n", bench_duration(b) );
printf("%.2f i/sec\n", bench_iteration_speed(b) );
}