blob: 9bf04f45afcf435637d588b0563db5a542b4e4d8 [file] [log] [blame]
Brian Silverman20350ac2021-11-17 18:19:55 -08001// -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following disclaimer
10// in the documentation and/or other materials provided with the
11// distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived from
14// this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "run_benchmark.h"
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/time.h>
34
35struct internal_bench {
36 bench_body body;
37 uintptr_t param;
38};
39
40static void run_body(struct internal_bench *b, long iterations)
41{
42 b->body(iterations, b->param);
43}
44
45static double measure_once(struct internal_bench *b, long iterations)
46{
47 struct timeval tv_before, tv_after;
48 int rv;
49 double time;
50
51 rv = gettimeofday(&tv_before, NULL);
52 if (rv) {
53 perror("gettimeofday");
54 abort();
55 }
56
57 run_body(b, iterations);
58
59 rv = gettimeofday(&tv_after, NULL);
60 if (rv) {
61 perror("gettimeofday");
62 abort();
63 }
64 tv_after.tv_sec -= tv_before.tv_sec;
65 time = tv_after.tv_sec * 1E6 + tv_after.tv_usec;
66 time -= tv_before.tv_usec;
67 time *= 1000;
68 return time;
69}
70
71#define TRIAL_NSEC 0.3E9
72#define TARGET_NSEC 3E9
73
74static double run_benchmark(struct internal_bench *b)
75{
76 long iterations = 128;
77 double nsec;
78 while (1) {
79 nsec = measure_once(b, iterations);
80 if (nsec > TRIAL_NSEC) {
81 break;
82 }
83 iterations <<= 1;
84 }
85 while (nsec < TARGET_NSEC) {
86 iterations = (long)(iterations * TARGET_NSEC * 1.1 / nsec);
87 nsec = measure_once(b, iterations);
88 }
89 return nsec / iterations;
90}
91
92void report_benchmark(const char *name, bench_body body, uintptr_t param)
93{
94 int i;
95 struct internal_bench b = {.body = body, .param = param};
96 for (i = 0; i < 3; i++) {
97 double nsec = run_benchmark(&b);
98 int slen;
99 int padding_size;
100
101 slen = printf("Benchmark: %s", name);
102 if (param && name[strlen(name)-1] != ')') {
103 slen += printf("(%lld)", (long long)param);
104 }
105 padding_size = 60 - slen;
106 if (padding_size < 1) {
107 padding_size = 1;
108 }
109 printf("%*c%f nsec\n", padding_size, ' ', nsec);
110 fflush(stdout);
111 }
112}