forked from fabian-jung/tsmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.cpp
91 lines (73 loc) · 1.96 KB
/
tracing.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
#include <fmt/format.h>
#include <functional>
#include <map>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include <chrono>
#include "tsmp/introspect.hpp"
#include "tsmp/proxy.hpp"
#include "tsmp/reflect.hpp"
struct entry_t
{
std::chrono::duration<double> duration;
size_t calls;
};
struct trace_data_t
{
struct collector_t
{
~collector_t()
{
const auto end = std::chrono::high_resolution_clock::now();
data.duration += end - begin;
++data.calls;
}
entry_t& data;
const std::chrono::high_resolution_clock::time_point begin = std::chrono::high_resolution_clock::now();
};
trace_data_t(std::string class_name)
: class_name(std::move(class_name))
{
}
collector_t collect(std::string name) { return {data[std::move(name)]}; }
~trace_data_t()
{
for (const auto& [name, entry] : data) {
fmt::print("{}::{} took {}s in {} calls\n", class_name, name, entry.duration.count(), entry.calls);
}
}
std::string class_name;
std::map<std::string, entry_t> data;
};
struct tracing_functor_t
{
tracing_functor_t(const char* class_name)
: data(class_name)
{
}
trace_data_t data;
template<class... Args>
decltype(auto) operator()(auto& fn, std::string name, Args&&... args)
{
const auto collector = data.collect(std::move(name));
return std::invoke(fn, std::forward<Args>(args)...);
}
};
template<class T>
using tracing_proxy = tsmp::value_proxy<T, tracing_functor_t>;
int main(int, char*[])
{
auto test_vector = tsmp::value_proxy{std::vector<int>{}, tracing_functor_t{"std::vector<int>"}};
for (auto i = 0u; i < 1000000u; ++i) {
test_vector.push_back(5);
}
for (auto i = 0u; i < 1000000u; ++i) {
test_vector.front() = 5;
}
for (auto i = 0u; i < 1000000u; ++i) {
test_vector.at(0) = 5;
}
return 0;
}