summaryrefslogtreecommitdiff
path: root/source/kit_test/bench.h
blob: dd86c17383ac14b224af0ce6e6c37fa1d96eafc6 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef KIT_BENCH_BENCH_H
#define KIT_BENCH_BENCH_H

#ifdef __cplusplus
extern "C" {
#endif

#include "test.h"

#include <stdint.h>

#ifndef KIT_TEST_FILE
#  define KIT_TEST_FILE kit_bench
#endif

#ifndef KIT_BENCHS_SIZE_LIMIT
#  define KIT_BENCHS_SIZE_LIMIT 200
#endif

#ifndef KIT_BENCH_MAX_REPEATS
#  define KIT_BENCH_MAX_REPEATS 4000
#endif

#ifndef KIT_BENCH_MAX_CYCLES
#  define KIT_BENCH_MAX_CYCLES 40
#endif

#ifndef KIT_BENCH_REPEATS_DEFAULT_1
#  define KIT_BENCH_REPEATS_DEFAULT_1 40
#endif

#ifndef KIT_BENCH_REPEATS_DEFAULT_2
#  define KIT_BENCH_REPEATS_DEFAULT_2 400
#endif

typedef void (*kit_bench_set_repeats_limit_fn)(int bench_index,
                                               int repeats_limit);
typedef int (*kit_bench_loop_fn)(int bench_index);
typedef void (*kit_bench_begin_fn)(int bench_index);
typedef void (*kit_bench_end_fn)(int bench_index);

typedef void (*kit_bench_run_fn)(
    int                            kit_bench_index_,
    kit_bench_set_repeats_limit_fn kit_bench_set_repeats_limit_,
    kit_bench_loop_fn              kit_bench_loop_,
    kit_bench_begin_fn             kit_bench_begin_,
    kit_bench_end_fn               kit_bench_end_);

typedef struct {
  char const      *bench_name;
  char const      *bench_file;
  kit_bench_run_fn bench_fn;
  int64_t          sec[KIT_BENCH_MAX_REPEATS];
  int32_t          nsec[KIT_BENCH_MAX_REPEATS];
  int64_t          duration_nsec[KIT_BENCH_MAX_REPEATS];
  int64_t          duration_sorted_nsec[KIT_BENCH_MAX_REPEATS];
  int              repeats;
  int              cycles_size;
  int              cycles[KIT_BENCH_MAX_CYCLES];
  int              cycle;
  int              signal;
  int              ready;
} kit_benchmark_t;

typedef struct {
  int             size;
  kit_benchmark_t v[KIT_BENCHS_SIZE_LIMIT];
} kit_benchs_list_t;

extern kit_benchs_list_t kit_benchs_list;

void kit_bench_register(char const *name, char const *file,
                        kit_bench_run_fn fn);

#define KIT_BENCHMARK(name)                                          \
  static void KIT_TEST_CONCAT3_(kit_bench_run_, __LINE__,            \
                                KIT_TEST_FILE)(                      \
      int, kit_bench_set_repeats_limit_fn, kit_bench_loop_fn,        \
      kit_bench_begin_fn, kit_bench_end_fn);                         \
  KIT_TEST_ON_START_(                                                \
      KIT_TEST_CONCAT3_(kit_benchmark_, __LINE__, KIT_TEST_FILE)) {  \
    kit_bench_register(                                              \
        name, __FILE__,                                              \
        KIT_TEST_CONCAT3_(kit_bench_run_, __LINE__, KIT_TEST_FILE)); \
  }                                                                  \
  static void KIT_TEST_CONCAT3_(kit_bench_run_, __LINE__,            \
                                KIT_TEST_FILE)(                      \
      int                            kit_bench_index_,               \
      kit_bench_set_repeats_limit_fn kit_bench_set_repeats_limit_,   \
      kit_bench_loop_fn              kit_bench_loop_,                \
      kit_bench_begin_fn             kit_bench_begin_,               \
      kit_bench_end_fn               kit_bench_end_)

#define KIT_BENCHMARK_REPEAT(repeats_limit_) \
  kit_bench_set_repeats_limit_(kit_bench_index_, repeats_limit_)

#define KIT_BENCHMARK_BEGIN                   \
  while (kit_bench_loop_(kit_bench_index_)) { \
    kit_bench_begin_(kit_bench_index_);       \
    {

#define KIT_BENCHMARK_END           \
  }                                 \
  kit_bench_end_(kit_bench_index_); \
  }

/*  FIXME
 */
#define KIT_DO_NOT_OPTIMIZE(x)        \
  do {                                \
    volatile void *bench_ptr_ = &(x); \
    (void) bench_ptr_;                \
  } while (0)

int kit_run_benchmarks(int argc, char **argv);

#ifndef KIT_DISABLE_SHORT_NAMES
#  define BENCHMARK KIT_BENCHMARK
#  define BENCHMARK_REPEAT KIT_BENCHMARK_REPEAT
#  define BENCHMARK_BEGIN KIT_BENCHMARK_BEGIN
#  define BENCHMARK_END KIT_BENCHMARK_END
#  define DO_NOT_OPTIMIZE KIT_DO_NOT_OPTIMIZE

#  define bench_register kit_bench_register
#  define run_benchmarks kit_run_benchmarks
#endif

#ifdef __cplusplus
}
#endif

#endif