forked from c9s/bench
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench.c
108 lines (92 loc) · 2.51 KB
/
bench.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* bench.c
* Copyright (C) 2014 c9s <[email protected]>
*
* Distributed under terms of the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
#include "bench.h"
unsigned long unixtime() {
struct timeval tp;
if (gettimeofday((struct timeval *) &tp, NULL) == 0) {
return tp.tv_sec;
}
return 0;
}
double microtime() {
struct timeval tp;
long sec = 0L;
double msec = 0.0;
if (gettimeofday((struct timeval *) &tp, NULL) == 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("%s -> %s%ld%s runs, %s%ld%s iterations each run, finished in %s%lf%s seconds\n", BENCH_PRIMARY_COLOR, BENCH_SECONDARY_COLOR, b->R, BENCH_PRIMARY_COLOR, BENCH_SECONDARY_COLOR, b->N, BENCH_PRIMARY_COLOR, BENCH_SECONDARY_COLOR, bench_duration(b), BENCH_PRIMARY_COLOR );
printf("%s -> %s%.2f%s i/sec\n", BENCH_PRIMARY_COLOR, BENCH_SECONDARY_COLOR, bench_iteration_speed(b), BENCH_PRIMARY_COLOR );
printf("%s\n", BENCH_RESET_COLOR);
}
void bench_csv_write(char *filename, int countOfB, ...) {
FILE *fp = fopen(filename, "w");
if(!fp) {
return;
}
unsigned long ts = unixtime();
fprintf(fp, "%ld", ts);
int i;
bench * b;
va_list vl;
va_start(vl,countOfB);
for (i=0 ; i < countOfB ; i++) {
b = va_arg(vl, bench*);
fprintf(fp, ",%.2f", bench_iteration_speed(b) );
}
va_end(vl);
fprintf(fp, "\n");
fclose(fp);
}
/**
* Combine multiple benchmark result into one measure entry.
*
* bench_csv_append("benchmark.csv", 3, &b1, &b2)
*/
void bench_csv_append(char *filename, int countOfB, ...) {
FILE *fp = fopen(filename, "a+");
if(!fp) {
return;
}
unsigned long ts = unixtime();
fprintf(fp, "%ld", ts);
int i;
bench * b;
va_list vl;
va_start(vl,countOfB);
for (i=0 ; i < countOfB ; i++) {
b = va_arg(vl, bench*);
fprintf(fp, ",%.2f", bench_iteration_speed(b) );
}
va_end(vl);
fprintf(fp, "\n");
fclose(fp);
}