-
Notifications
You must be signed in to change notification settings - Fork 30
/
benchmark.hpp
253 lines (198 loc) · 6.8 KB
/
benchmark.hpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#pragma once
#include "benchmark_utility.hpp"
template <typename Subject, typename Foo>
class Benchmark
{
static ChronoTimer s_timer;
static Rng s_rng;
static double calculate_score(std::size_t N, std::size_t limit, std::size_t count)
{
// Return the test size N divided by (the sample size / number of samples)
return N / std::chrono::duration_cast<Delta_u>(Timer_u(limit / count)).count();
}
public:
static void validation_assert(std::size_t N)
{
Rng rng;
std::size_t count = 0;
for (auto i = N; i != 0; --i, count += N)
{
Subject subject;
{
std::vector<Foo> foo_array(N);
for (auto& foo : foo_array)
{
Foo::connect_method(subject, foo);
}
Foo::emit_method(subject, rng);
}
// Make sure disconnect is working correctly
Foo::emit_method(subject, rng);
}
// If the PRNG state is different
// There is an error in the signal implementation
Rng test;
test.discard(count);
if (rng != test) std::cerr << "Validation Failed: " << Foo::C_LIB_NAME << std::endl;
}
//--------------------------------------------------------------------------
static double construction(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
for (; elapsed < limit; ++count)
{
s_timer.reset();
std::vector<Subject> subjects(N * N);
elapsed += s_timer.count<Timer_u>();
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double destruction(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
for (; elapsed < limit; ++count)
{
{
std::vector<Subject> subjects(N * N);
s_timer.reset();
}
elapsed += s_timer.count<Timer_u>();
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double connection(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
for (; elapsed < limit; ++count)
{
Subject subject;
std::vector<Foo> foo(N);
s_timer.reset();
for (auto& foo_instance : foo)
{
Foo::connect_method(subject, foo_instance);
}
elapsed += s_timer.count<Timer_u>();
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double disconnect(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
for (; elapsed < limit; ++count)
{
Subject subject;
{
std::vector<Foo> foo(N);
for (auto& foo_instance : foo)
{
Foo::connect_method(subject, foo_instance);
}
s_timer.reset();
// Disconnect N Foo from Subject
}
elapsed += s_timer.count<Timer_u>();
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double reconnect(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
Subject subject;
for (; elapsed < limit; ++count)
{
std::vector<Foo> foo(N);
s_timer.reset();
for (auto& foo_instance : foo)
{
Foo::connect_method(subject, foo_instance);
}
elapsed += s_timer.count<Timer_u>();
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double emission(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
for (; elapsed < limit; ++count)
{
Subject subject;
std::vector<Foo> foo(N);
for (auto& foo_instance : foo)
{
Foo::connect_method(subject, foo_instance);
}
s_timer.reset();
Foo::emit_method(subject, s_rng);
elapsed += s_timer.count<Timer_u>();
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double combined(std::size_t N, std::size_t limit)
{
std::size_t count = 1;
std::size_t elapsed = 0;
s_timer.reset();
Subject subject;
for (; elapsed < limit; ++count, elapsed = s_timer.count<Timer_u>())
{
std::vector<Foo> foo(N);
for (auto& foo_instance : foo)
{
Foo::connect_method(subject, foo_instance);
}
Foo::emit_method(subject, s_rng);
}
return calculate_score(N, elapsed, count);
}
//--------------------------------------------------------------------------
static double threaded(std::size_t N, std::size_t limit)
{
Subject subject;
auto context = [&]()
{
std::size_t count = 1;
std::size_t elapsed = 0;
Rng rng(s_rng);
ChronoTimer timer;
for (; elapsed < limit; ++count, elapsed = timer.count<Timer_u>())
{
std::vector<Foo> foo(N);
for (auto& foo_instance : foo)
{
Foo::connect_method(subject, foo_instance);
}
Foo::emit_method(subject, rng);
}
return calculate_score(N, elapsed, count);
};
std::vector<double> results;
std::vector<std::future<double>> future_results;
for (std::size_t i = std::thread::hardware_concurrency() / 2; i > 0; --i)
{
future_results.emplace_back(std::async(std::launch::async, context));
}
for (auto& future_result : future_results)
{
results.emplace_back(future_result.get());
}
return std::accumulate(results.begin(), results.end(), 0.0) / results.size();
}
};
//------------------------------------------------------------------------------
template <typename Subject, typename Foo>
ChronoTimer Benchmark<Subject, Foo>::s_timer;
template <typename Subject, typename Foo>
Rng Benchmark<Subject, Foo>::s_rng;