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
8static 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}
16
17// Benchmark for User::print()
18static 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}
31
32// Main entry point
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/*}*/
static void BM_UserConstructor(const benchmark::State &state)
BENCHMARK_MAIN()
static void BM_UserPrint(const benchmark::State &state)
BENCHMARK(BM_UserConstructor)
#define _(String)
Definition gi18n-lib.h:32
Structure representing a GitHub user.
Definition user.h:9