-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcodspeed.cpp
More file actions
102 lines (84 loc) · 2.84 KB
/
codspeed.cpp
File metadata and controls
102 lines (84 loc) · 2.84 KB
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
#include <codspeed.h>
#include <iostream>
#include <string>
#include <vector>
#include "measurement.hpp"
namespace codspeed {
// Remove any `::` between brackets at the end to not mess with the URI
// parsing
// FIXME: Remove this bandaid when we migrate to structured benchmark metadata
std::string sanitize_bench_args(std::string &text) {
std::string search = "::";
std::string replace = "\\:\\:";
if (text.back() == ']') {
size_t pos_open = text.rfind('[');
if (pos_open != std::string::npos) {
// Extract the substring between '[' and ']'
size_t pos_close = text.size() - 1;
std::string substring =
text.substr(pos_open + 1, pos_close - pos_open - 1);
// Perform the search and replace within the substring
size_t pos = substring.find(search);
while (pos != std::string::npos) {
substring.replace(pos, search.length(), replace);
pos = substring.find(search, pos + replace.length());
}
// Replace the original substring with the modified one
text.replace(pos_open + 1, pos_close - pos_open - 1, substring);
}
}
return text;
}
std::string join(const std::vector<std::string> &elements,
const std::string &delimiter) {
std::string result;
for (size_t i = 0; i < elements.size(); ++i) {
result += elements[i];
if (i != elements.size() - 1) {
result += delimiter;
}
}
return result;
}
CodSpeed::CodSpeed() {
// First initialize `g_hooks` before calling any measurement functions
measurement_init();
is_instrumented = measurement_is_instrumented();
if (!is_instrumented) {
std::cerr
<< "NOTICE: codspeed is enabled, but no performance measurement will "
"be made since it's running in an unknown environment."
<< std::endl;
}
measurement_set_metadata();
}
// Member function definitions
void CodSpeed::push_group(const std::string &group) {
group_stack.push_back(group);
}
void CodSpeed::pop_group() {
if (!group_stack.empty()) {
group_stack.pop_back();
}
}
void CodSpeed::start_benchmark(const std::string &name) {
std::string uri = name;
uri = sanitize_bench_args(uri);
// Sanity check URI and add a placeholder if format is wrong
if (name.find("::") == std::string::npos) {
uri = "unknown_file::" + name;
std::cout << "WARNING: Benchmark name does not contain '::'. Using URI: "
<< uri << std::endl;
}
current_benchmark = uri;
}
void CodSpeed::end_benchmark() {
measurement_set_executed_benchmark(current_benchmark);
benchmarked.push_back(current_benchmark);
std::string action_str = is_instrumented ? "Measured" : "Checked";
std::string group_str =
group_stack.empty() ? "" : " (group: " + join(group_stack, "/") + ")";
std::cerr << action_str << ": " << current_benchmark << group_str
<< std::endl;
}
} // namespace codspeed