-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.main.cpp
63 lines (52 loc) · 1.82 KB
/
example.main.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
#include <chrono> // for milliseconds
#include <future> // for future, async
#include <iostream> // for operator<<, endl, basic_ost...
#include <random> // for mt19937, random_device, uni...
#include <thread> // for std::this_thread
#include <vector> // for vector
#include "ProfilerLib/DurationEvent.hpp" // for DurationEvent
#include "ProfilerLib/Profiler.hpp" // for Profiler
#include "ProfilerLib/Scope.hpp" // for Scope, Scope::Process
#include "ProfilerLib/ScopeEvent.hpp" // for ScopeEvent
std::random_device rd;
std::mt19937 gen(rd());
Profiler p("MainProfiler", "trace.json");
void importantFunction(int recurse = 5) {
ScopeEvent e(p, "importantFunction");
if (recurse > 0) {
importantFunction(recurse - 1);
}
std::uniform_int_distribution distribution(5, 10);
std::this_thread::sleep_for(std::chrono::milliseconds(distribution(gen)));
static int i = 0;
p.submitCounterEvent("some counter", {{"i", i}, {"c", 0}});
i++;
}
void asyncCalls() {
ScopeEvent e(p, "asyncCalls");
std::vector<std::future<void>> futures;
futures.reserve(10);
DurationEvent threadsStarting(p, "Starting threads");
threadsStarting.start();
for (int i = 0; i < 10; i++) {
futures.emplace_back(std::async([i]() {
ScopeEvent e(p, "lambda in asyncCalls");
p.setThreadName("async call nr. " + std::to_string(i));
importantFunction(0);
}));
}
threadsStarting.stop();
p.submitInstantEvent("tasks started, now waiting", Scope::Process);
for (auto &f : futures) {
f.wait();
}
}
int main() {
ScopeEvent e(p, "main function");
p.setProcessName("TestProcess");
p.setThreadName("main thread");
std::cout << "Hello, World!" << std::endl;
importantFunction();
asyncCalls();
return 0;
}