summaryrefslogtreecommitdiff
path: root/source/kit_test/bench.h
blob: 131d89d6877eb8839a1409ed34b778a6b91a29d9 (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
#ifndef KIT_BENCH_BENCH_H
#define KIT_BENCH_BENCH_H

#ifdef __cplusplus
extern "C" {
#endif

#include "test.h"
#include <stdint.h>

#ifndef KIT_BENCH_FILE
#  define KIT_BENCH_FILE kit_bench
#endif

#ifndef KIT_BENCHS_SIZE_LIMIT
#  define KIT_BENCHS_SIZE_LIMIT 0x1000
#endif

#ifndef KIT_BENCH_REPEATS
#  define KIT_BENCH_REPEATS 400
#endif

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_begin_fn kit_bench_begin_fn_,
    kit_bench_end_fn kit_bench_end_fn_);

typedef struct {
  char const      *bench_name;
  char const      *bench_file;
  kit_bench_run_fn bench_fn;
  int64_t          sec[KIT_BENCH_REPEATS];
  int32_t          nsec[KIT_BENCH_REPEATS];
  int64_t          duration_nsec[KIT_BENCH_REPEATS];
  int              repeats;
  int              signal;
} kit_benchmark_t;

typedef struct {
  int             size;
  kit_benchmark_t benchs[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_BENCH_FILE)(                     \
      int, kit_bench_begin_fn, kit_bench_end_fn);                    \
  KIT_TEST_ON_START_(                                                \
      KIT_TEST_CONCAT3_(kit_benchmark_, __LINE__, KIT_BENCH_FILE)) { \
    kit_bench_register(name, __FILE__,                               \
                       KIT_TEST_CONCAT3_(kit_bench_run_, __LINE__,   \
                                         KIT_BENCH_FILE));           \
  }                                                                  \
  static void KIT_TEST_CONCAT3_(kit_bench_run_, __LINE__,            \
                                KIT_BENCH_FILE)(                     \
      int kit_bench_index_, kit_bench_begin_fn kit_bench_begin_fn_,  \
      kit_bench_end_fn kit_bench_end_fn_)

#define KIT_BENCHMARK_BEGIN                                          \
  for (int kit_bench_repeat_ = 0;                                    \
       kit_bench_repeat_ < KIT_BENCH_REPEATS; kit_bench_repeat_++) { \
    kit_bench_begin_fn_(kit_bench_index_);                           \
    {

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

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

#ifndef KIT_DISABLE_SHORT_NAMES
#  define BENCHMARK KIT_BENCHMARK
#  define BENCHMARK_BEGIN KIT_BENCHMARK_BEGIN
#  define BENCHMARK_END KIT_BENCHMARK_END

#  define bench_register kit_bench_register
#  define run_benchmarks kit_run_benchmarks
#endif

#ifdef __cplusplus
}
#endif

#endif