-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
118 lines (92 loc) · 3.14 KB
/
benchmark.cpp
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
/*
* main.cpp
*
* Created on: May 27, 2020
* Author: sajeeb
*/
#include <assert.h>
#include <iostream>
#include <thread>
#include "CPPLOGGER_SYNC.h"
/**
* The following should always be called only in the main translation unit.
* This is to default initialize the logger flags.
*/
logger_init();
FILE* null_file = fopen("/dev/null", "w");
void bench(size_t iters, const char* msg) {
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
auto start = high_resolution_clock::now();
for(size_t i=0; i<iters; ++i) {
logger_info("Hello logger: msg number %ld", i);
}
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();
logger_output_stream(stdout);
logger_enable_global(true);
logger_info("%-16s| Elapsed: %04.2f secs | Throughput: %'d/sec", msg, delta_d, int(iters/delta_d));
// assert(logger::queue._last.load()->stream == stdout);
}
void single_threaded(size_t iters) {
logger_info("*******************************************************************");
logger_info("Single threaded benchmark with %'ld iterations", iters);
logger_info("*******************************************************************");
logger_output_stream(null_file);
bench(iters, "basic");
logger_output_stream(null_file);
logger_enable_global(false);
bench(iters, "disabled");
}
void multi_thread_bench(size_t n_threads, size_t iters, string type) {
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
std::thread _threads[n_threads];
for (size_t i=0; i<n_threads; ++i) _threads[i] = std::thread([&]{
for (int j = 0; j < int(iters / n_threads); j++) {
logger_info_mt("Hello logger: msg number %d", j);
}
});
auto start = high_resolution_clock::now();
for (auto &t : _threads) {
t.join();
};
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();
logger_output_stream(stdout);
logger_enable_global(true);
logger_info("%-16s| Elapsed: %04.2f secs | Throughput: %'d/sec", type.c_str(), delta_d, int(iters/delta_d));
}
void multi_threaded(size_t threads, size_t iters) {
logger_info("*******************************************************************");
logger_info("%'d thread benchmark with %'ld iterations", threads, iters);
logger_info("*******************************************************************");
logger_output_stream(null_file);
multi_thread_bench(threads, iters, "basic");
logger_output_stream(null_file);
logger_enable_global(false);
multi_thread_bench(threads, iters, "disabled");
}
int main(int argc, char** argv) {
logger_print_file(false);
logger_print_line(false);
// logger_print_log_type(false);
setlocale(LC_NUMERIC, "");
unsigned int iters = 1000000;
size_t threads = 10;
for (uint8_t i=0; i<argc; ++i) {
if (strcmp(argv[i], "--threads") == 0) {
threads = atoi(argv[i+1]);
i += 1;
} else if (strcmp(argv[i], "--iters") == 0) {
iters = atoi(argv[i+1]);
i += 1;
}
}
single_threaded(iters);
logger_info("");
multi_threaded(threads, iters);
fclose(null_file);
}