CppBenchmark  1.0.4.0
C++ Benchmark Library
settings.cpp
Go to the documentation of this file.
1 
9 #include "benchmark/settings.h"
10 
11 namespace CppBenchmark {
12 
14  : _attempts(5),
15  _infinite(false),
16  _duration(0),
17  _operations(0),
18  _latency_params(std::make_tuple(0, 0, 0)),
19  _latency_auto(false)
20 {
21  Duration(0);
22 }
23 
24 Settings::Settings(int64_t operations)
25  : Settings()
26 {
28 }
29 
31 {
32  _attempts = (attempts > 0) ? attempts : 5;
33  return *this;
34 }
35 
37 {
38  _infinite = true;
39  _duration = 0;
40  _operations = 0;
41  return *this;
42 }
43 
44 Settings& Settings::Operations(int64_t operations)
45 {
46  _infinite = false;
47  _duration = 0;
48  _operations = (operations > 0) ? operations : 1;
49  return *this;
50 }
51 
52 Settings& Settings::Duration(int64_t duration)
53 {
54  _infinite = false;
55  _duration = (duration > 0) ? duration : 5;
56  _operations = 0;
57  return *this;
58 }
59 
61 {
62  if (threads > 0)
63  _threads.emplace_back(threads);
64  return *this;
65 }
66 
67 Settings& Settings::ThreadsRange(int from, int to)
68 {
69  if ((from > 0) && (to > 0))
70  {
71  if (from > to)
72  std::swap(from, to);
73 
74  for (int i = from; i <= to; ++i)
75  _threads.emplace_back(i);
76  }
77  return *this;
78 }
79 
80 Settings& Settings::ThreadsRange(int from, int to, const std::function<int (int, int, int&)>& selector)
81 {
82  if ((from > 0) && (to > 0) && selector)
83  {
84  if (from > to)
85  std::swap(from, to);
86 
87  // Select the first value
88  int current = from;
89  int result = selector(from, to, current);
90  while ((result >= from) && (result <= to))
91  {
92  _threads.emplace_back(result);
93 
94  // Select the next value
95  result = selector(from, to, current);
96  }
97  }
98  return *this;
99 }
100 
101 Settings& Settings::PC(int producers, int consumers)
102 {
103  if ((producers > 0) && (consumers > 0))
104  _pc.emplace_back(producers, consumers);
105  return *this;
106 }
107 
108 Settings& Settings::PCRange(int producers_from, int producers_to, int consumers_from, int consumers_to)
109 {
110  if ((producers_from > 0) && (producers_to > 0) && (consumers_from > 0) && (consumers_to > 0))
111  {
112  if (producers_from > producers_to)
113  std::swap(producers_from, producers_to);
114 
115  if (consumers_from > consumers_to)
116  std::swap(consumers_from, consumers_to);
117 
118  for (int i = producers_from; i <= producers_to; ++i)
119  for (int j = consumers_from; j <= consumers_to; ++j)
120  _pc.emplace_back(i, j);
121  }
122  return *this;
123 }
124 
125 Settings& Settings::PCRange(int producers_from, int producers_to, const std::function<int (int, int, int&)>& producers_selector,
126  int consumers_from, int consumers_to, const std::function<int (int, int, int&)>& consumers_selector)
127 {
128  if ((producers_from > 0) && (producers_to > 0) && (consumers_from > 0) && (consumers_to > 0) && producers_selector && consumers_selector)
129  {
130  if (producers_from > producers_to)
131  std::swap(producers_from, producers_to);
132 
133  if (consumers_from > consumers_to)
134  std::swap(consumers_from, consumers_to);
135 
136  // Select the first value
137  int current1 = producers_from;
138  int result1 = producers_selector(producers_from, producers_to, current1);
139  while ((result1 >= producers_from) && (result1 <= producers_to))
140  {
141  // Select the second value
142  int current2 = consumers_from;
143  int result2 = consumers_selector(consumers_from, consumers_to, current2);
144  while ((result2 >= consumers_from) && (result2 <= consumers_to))
145  {
146  _pc.emplace_back(result1, result2);
147 
148  // Select the next value
149  result2 = consumers_selector(consumers_from, consumers_to, current2);
150  }
151 
152  // Select the next value
153  result1 = producers_selector(producers_from, producers_to, current1);
154  }
155  }
156  return *this;
157 }
158 
160 {
161  if (value >= 0)
162  _params.emplace_back(value, -1, -1);
163  return *this;
164 }
165 
166 Settings& Settings::ParamRange(int from, int to)
167 {
168  if ((from >= 0) && (to >= 0))
169  {
170  if (from > to)
171  std::swap(from, to);
172 
173  for (int i = from; i <= to; ++i)
174  _params.emplace_back(i, -1, -1);
175  }
176  return *this;
177 }
178 
179 Settings& Settings::ParamRange(int from, int to, const std::function<int (int, int, int&)>& selector)
180 {
181  if ((from >= 0) && (to >= 0) && selector)
182  {
183  if (from > to)
184  std::swap(from, to);
185 
186  // Select the first value
187  int current = from;
188  int result = selector(from, to, current);
189  while ((result >= from) && (result <= to))
190  {
191  _params.emplace_back(result, -1, -1);
192 
193  // Select the next value
194  result = selector(from, to, current);
195  }
196  }
197  return *this;
198 }
199 
200 Settings& Settings::Pair(int value1, int value2)
201 {
202  if ((value1 >= 0) && (value2 >= 0))
203  _params.emplace_back(value1, value2, -1);
204  return *this;
205 }
206 
207 Settings& Settings::PairRange(int from1, int to1, int from2, int to2)
208 {
209  if ((from1 >= 0) && (to1 >= 0) && (from2 >= 0) && (to2 >= 0))
210  {
211  if (from1 > to1)
212  std::swap(from1, to1);
213 
214  if (from2 > to2)
215  std::swap(from2, to2);
216 
217  for (int i = from1; i <= to1; ++i)
218  for (int j = from2; j <= to2; ++j)
219  _params.emplace_back(i, j, -1);
220  }
221  return *this;
222 }
223 
224 Settings& Settings::PairRange(int from1, int to1, const std::function<int (int, int, int&)>& selector1,
225  int from2, int to2, const std::function<int (int, int, int&)>& selector2)
226 {
227  if ((from1 >= 0) && (to1 >= 0) && (from2 >= 0) && (to2 >= 0) && selector1 && selector2)
228  {
229  if (from1 > to1)
230  std::swap(from1, to1);
231 
232  if (from2 > to2)
233  std::swap(from2, to2);
234 
235  // Select the first value
236  int current1 = from1;
237  int result1 = selector1(from1, to1, current1);
238  while ((result1 >= from1) && (result1 <= to1))
239  {
240  // Select the second value
241  int current2 = from2;
242  int result2 = selector2(from2, to2, current2);
243  while ((result2 >= from2) && (result2 <= to2))
244  {
245  _params.emplace_back(result1, result2, -1);
246 
247  // Select the next value
248  result2 = selector2(from2, to2, current2);
249  }
250 
251  // Select the next value
252  result1 = selector1(from1, to1, current1);
253  }
254  }
255  return *this;
256 }
257 
258 Settings& Settings::Triple(int value1, int value2, int value3)
259 {
260  if ((value1 >= 0) && (value2 >= 0) && (value3 >= 0))
261  _params.emplace_back(value1, value2, value3);
262  return *this;
263 }
264 
265 Settings& Settings::TripleRange(int from1, int to1, int from2, int to2, int from3, int to3)
266 {
267  if ((from1 >= 0) && (to1 >= 0) && (from2 >= 0) && (to2 >= 0) && (from3 >= 0) && (to3 >= 0))
268  {
269  if (from1 > to1)
270  std::swap(from1, to1);
271 
272  if (from2 > to2)
273  std::swap(from2, to2);
274 
275  if (from3 > to3)
276  std::swap(from3, to3);
277 
278  for (int i = from1; i <= to1; ++i)
279  for (int j = from2; j <= to2; ++j)
280  for (int k = from3; k <= to3; ++k)
281  _params.emplace_back(i, j, k);
282  }
283  return *this;
284 }
285 
286 Settings& Settings::TripleRange(int from1, int to1, const std::function<int (int, int, int&)>& selector1,
287  int from2, int to2, const std::function<int (int, int, int&)>& selector2,
288  int from3, int to3, const std::function<int (int, int, int&)>& selector3)
289 {
290  if ((from1 >= 0) && (to1 >= 0) && (from2 >= 0) && (to2 >= 0) && (from3 >= 0) && (to3 >= 0) && selector1 && selector2 && selector3)
291  {
292  if (from1 > to1)
293  std::swap(from1, to1);
294 
295  if (from2 > to2)
296  std::swap(from2, to2);
297 
298  if (from3 > to3)
299  std::swap(from3, to3);
300 
301  // Select the first value
302  int current1 = from1;
303  int result1 = selector1(from1, to1, current1);
304  while ((result1 >= from1) && (result1 <= to1))
305  {
306  // Select the second value
307  int current2 = from2;
308  int result2 = selector2(from2, to2, current2);
309  while ((result2 >= from2) && (result2 <= to2))
310  {
311  // Select the third value
312  int current3 = from3;
313  int result3 = selector3(from3, to3, current3);
314  while ((result3 >= from3) && (result3 <= to3))
315  {
316  _params.emplace_back(result1, result2, result3);
317 
318  // Select the next value
319  result3 = selector3(from3, to3, current3);
320  }
321 
322  // Select the next value
323  result2 = selector2(from2, to2, current2);
324  }
325 
326  // Select the next value
327  result1 = selector1(from1, to1, current1);
328  }
329  }
330  return *this;
331 }
332 
333 Settings& Settings::Latency(int64_t lowest, int64_t highest, int significant, bool automatic)
334 {
335  _latency_params = std::make_tuple(lowest, highest, significant);
336  _latency_auto = automatic;
337  return *this;
338 }
339 
340 } // namespace CppBenchmark
Benchmark settings.
Definition: settings.h:32
Settings & ParamRange(int from, int to)
Add new single parameter range to the benchmark running plan.
Definition: settings.cpp:166
Settings & Attempts(int attempts)
Set independent benchmark attempts.
Definition: settings.cpp:30
Settings & ThreadsRange(int from, int to)
Add new threads range to the benchmark running plan.
Definition: settings.cpp:67
Settings & Threads(int threads)
Add new threads count to the benchmark running plan.
Definition: settings.cpp:60
Settings & Triple(int value1, int value2, int value3)
Add new parameters triple to the benchmark running plan.
Definition: settings.cpp:258
Settings & PC(int producers, int consumers)
Add new producers/consumers count to the benchmark running plan.
Definition: settings.cpp:101
int attempts() const noexcept
Get count of independent benchmark attempts.
Definition: settings.h:53
Settings & Infinite()
Set infinite benchmark operations flag.
Definition: settings.cpp:36
int64_t duration() const noexcept
Get benchmark duration in milliseconds.
Definition: settings.h:57
Settings & Operations(int64_t operations)
Set count of operations.
Definition: settings.cpp:44
Settings & PairRange(int from1, int to1, int from2, int to2)
Add new parameters pairs range to the benchmark running plan.
Definition: settings.cpp:207
Settings & Latency(int64_t lowest, int64_t highest, int significant, bool automatic=true)
Set latency histogram parameters.
Definition: settings.cpp:333
Settings & TripleRange(int from1, int to1, int from2, int to2, int from3, int to3)
Add new parameters triple range to the benchmark running plan.
Definition: settings.cpp:265
int64_t operations() const noexcept
Get count of operations.
Definition: settings.h:59
Settings & PCRange(int producers_from, int producers_to, int consumers_from, int consumers_to)
Add new producers/consumers range to the benchmark running plan.
Definition: settings.cpp:108
Settings & Duration(int64_t duration)
Set benchmark duration in seconds.
Definition: settings.cpp:52
Settings & Pair(int value1, int value2)
Add new parameters pair to the benchmark running plan.
Definition: settings.cpp:200
Settings()
Initialize settings with the default benchmark duration (5 seconds)
Definition: settings.cpp:13
Settings & Param(int value)
Add new single parameter to the benchmark running plan.
Definition: settings.cpp:159
const std::vector< int > & threads() const noexcept
Get collection of independent threads counts in a benchmark plan.
Definition: settings.h:61
C++ Benchmark project definitions.
Definition: barrier.h:15
Benchmark settings definition.