|
| 1 | +#include <benchmarking.h> |
| 2 | + |
| 3 | +static size_t next_id = 0; |
| 4 | +static struct bench_entry **benchs; |
| 5 | + |
| 6 | +/******************************************************************************* |
| 7 | + * Diff functions |
| 8 | + ******************************************************************************/ |
| 9 | + |
| 10 | +static double |
| 11 | +timespec_diff(struct timespec *end, struct timespec *start) |
| 12 | +{ |
| 13 | + return (end->tv_sec - start->tv_sec) |
| 14 | + + (end->tv_nsec - start->tv_nsec) * pow(10, -9); |
| 15 | +} |
| 16 | + |
| 17 | +/******************************************************************************* |
| 18 | + * Benchmark one function |
| 19 | + ******************************************************************************/ |
| 20 | + |
| 21 | +size_t |
| 22 | +bench_init(const char *fn_name) |
| 23 | +{ |
| 24 | + return 0; |
| 25 | + struct bench_entry *new_be = malloc(sizeof(struct bench_entry)); |
| 26 | + new_be->fn_name = fn_name; |
| 27 | + next_id += 1; |
| 28 | + benchs = realloc(benchs, next_id * sizeof(struct bench_entry *)); |
| 29 | + benchs[next_id - 1] = new_be; |
| 30 | + return next_id - 1; |
| 31 | +} |
| 32 | + |
| 33 | +void |
| 34 | +bench_start(size_t id) |
| 35 | +{ |
| 36 | + return; |
| 37 | + benchs[id]->res_start = clock_gettime(CLOCK_MONOTONIC, &benchs[id]->start); |
| 38 | +} |
| 39 | + |
| 40 | +void |
| 41 | +bench_end(size_t id) |
| 42 | +{ |
| 43 | + return; |
| 44 | + int res_end = clock_gettime(CLOCK_MONOTONIC, &benchs[id]->end); |
| 45 | + assert(benchs[id]->res_start != -1); |
| 46 | + assert(res_end != -1); |
| 47 | + benchs[id]->diff = timespec_diff(&benchs[id]->end, &benchs[id]->start); |
| 48 | + bench_report_one_id(id); |
| 49 | +} |
| 50 | + |
| 51 | +/******************************************************************************* |
| 52 | + * Printing |
| 53 | + ******************************************************************************/ |
| 54 | + |
| 55 | +static void |
| 56 | +print_timespec(char *buf, struct timespec *ts) |
| 57 | +{ |
| 58 | + sprintf(buf, "%lld.%9ld", (long long) ts->tv_sec, ts->tv_nsec); |
| 59 | +} |
| 60 | + |
| 61 | +static void |
| 62 | +bench_report_one(struct bench_entry *entry) |
| 63 | +{ |
| 64 | + |
| 65 | + const unsigned short buf_sz = 30; |
| 66 | + char *buf_st = alloca(buf_sz); |
| 67 | + print_timespec(buf_st, &entry->start); |
| 68 | + char *buf_en = alloca(buf_sz); |
| 69 | + print_timespec(buf_en, &entry->end); |
| 70 | + printf("Func <%s> -- start %s -- end %s -- seconds %f\n", entry->fn_name, |
| 71 | + buf_st, buf_en, entry->diff); |
| 72 | +} |
| 73 | + |
| 74 | +void |
| 75 | +bench_report_one_id(size_t id) |
| 76 | +{ |
| 77 | + printf("ID %zu == ", id); |
| 78 | + bench_report_one(benchs[id]); |
| 79 | +} |
0 commit comments