Github User Fetcher
1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
c++/benchmarks/bench_user.cpp
Go to the documentation of this file.
1
#include "user.hpp"
2
#include <benchmark/benchmark.h>
3
4
#include <iostream>
5
#include <sstream>
6
7
// Benchmark for User constructor
8
static
void
BM_UserConstructor
(
const
benchmark::State &state) {
9
for
(
auto
_
: state) {
10
// Prevent compiler from optimizing away the object
11
benchmark::DoNotOptimize(
12
User
(
"octocat"
,
"The Octocat"
,
"GitHub"
,
"San Francisco"
));
13
}
14
}
15
BENCHMARK
(
BM_UserConstructor
);
16
17
// Benchmark for User::print()
18
static
void
BM_UserPrint
(
const
benchmark::State &state) {
19
User
user(
"octocat"
,
"The Octocat"
,
"GitHub"
,
"San Francisco"
);
20
21
for
(
auto
_
: state) {
22
std::ostringstream oss;
23
std::streambuf *old = std::cout.rdbuf(oss.rdbuf());
// Redirect std::cout
24
user.print();
25
std::cout.rdbuf(old);
// Restore original buffer
26
27
benchmark::DoNotOptimize(oss.str());
// Prevent optimization
28
}
29
}
30
BENCHMARK
(
BM_UserPrint
);
31
32
// Main entry point
33
BENCHMARK_MAIN
();
34
35
// IN catch2 this becomes:
36
/*#include <catch2/catch_test_macros.hpp>*/
37
/*#include <catch2/benchmark/catch_benchmark.hpp>*/
38
/**/
39
/*#include "user.hpp"*/
40
/*#include <iostream>*/
41
/*#include <sstream>*/
42
/**/
43
/*TEST_CASE("Benchmark User object initialization", "[benchmark]") {*/
44
/* BENCHMARK("User constructor") {*/
45
/* return User("octocat", "The Octocat", "GitHub", "San Francisco");*/
46
/* };*/
47
/*}*/
48
/**/
49
/*TEST_CASE("Benchmark User::print()", "[benchmark]") {*/
50
/* User user("octocat", "The Octocat", "GitHub", "San Francisco");*/
51
/**/
52
/* BENCHMARK("User::print() to stringstream") {*/
53
/* std::ostringstream oss;*/
54
/* std::streambuf* old = std::cout.rdbuf(oss.rdbuf());*/
55
/* user.print();*/
56
/* std::cout.rdbuf(old);*/
57
/* return oss.str(); // Needed to prevent optimization*/
58
/* };*/
59
/*}*/
BM_UserConstructor
static void BM_UserConstructor(const benchmark::State &state)
Definition
c++/benchmarks/bench_user.cpp:8
BENCHMARK_MAIN
BENCHMARK_MAIN()
BM_UserPrint
static void BM_UserPrint(const benchmark::State &state)
Definition
c++/benchmarks/bench_user.cpp:18
BENCHMARK
BENCHMARK(BM_UserConstructor)
_
#define _(String)
Definition
gi18n-lib.h:32
User
Structure representing a GitHub user.
Definition
user.h:9
c++
benchmarks
bench_user.cpp
Generated by
1.10.0