librfn
An ad-hoc utility library
benchmark.c
Go to the documentation of this file.
1 /*
2  * benchmark.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <errno.h>
15 #include <stdbool.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <time.h>
20 
21 #include "librfn.h"
22 
23 void rf_benchmark_init(rf_benchmark_t *b, uint64_t runtime)
24 {
25  b->end = 0;
26  b->start = time64_now() + 2;
27  b->expiry = b->start + runtime;
28 
29  // wait until the test starts
30  while (b->start > time64_now())
31  ; // do nothing
32 }
33 
35 {
36  b->end = time64_now();
37  return (b->end < b->expiry);
38 }
39 
41 {
42  uint64_t now = time64_now();
43  if (b->end < b->expiry)
44  b->end = now;
45 
46  uint64_t elapsed = b->end - b->start;
47 
48  r->ratio = (double) nominal / (double) elapsed;
49  r->cpu_usage = 100.0 / r->ratio;
50 }
51 
53 {
54  char *prefix = getenv("BENCHMARK_PREFIX");
55 
56  printf("%s%-40s Bandwidth %6.2fx CPU load %6.3f%%\n",
57  (prefix ? prefix : ""),
58  tag, r->ratio, r->cpu_usage);
59 }
uint64_t time64_now(void)
bool rf_benchmark_running(rf_benchmark_t *b)
Definition: benchmark.c:34
uint64_t expiry
Definition: benchmark.h:50
struct bravo b
void rf_benchmark_finalize(rf_benchmark_t *b, uint64_t nominal, rf_benchmark_results_t *r)
Definition: benchmark.c:40
uint64_t start
Definition: benchmark.h:49
uint32_t now
Definition: fibre.c:31
void rf_benchmark_results_show(rf_benchmark_results_t *r, const char *tag)
Definition: benchmark.c:52
uint64_t end
Definition: benchmark.h:51
void rf_benchmark_init(rf_benchmark_t *b, uint64_t runtime)
Definition: benchmark.c:23
double ratio
Degree by which we run faster than real time (x2 mean twice)
Definition: benchmark.h:55
double cpu_usage
Estimated CPU usage at x1.
Definition: benchmark.h:56