Skip to content

Commit ce06f33

Browse files
committed
feat(more): add profiler
1 parent 24e82b0 commit ce06f33

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/profiler/esp_utils_time_profiler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace esp_utils {
1515

16-
TimeProfiler &TimeProfiler::instance()
16+
TimeProfiler &TimeProfiler::get_instance()
1717
{
1818
static TimeProfiler inst;
1919
return inst;
@@ -299,13 +299,13 @@ std::vector<TimeProfiler::Node *> TimeProfiler::sorted_children(Node *node) cons
299299
return result;
300300
}
301301

302-
TimeProfileScope::TimeProfileScope(const std::string &name)
302+
TimeProfilerScope::TimeProfilerScope(const std::string &name)
303303
{
304-
TimeProfiler::instance().enter_scope(name);
304+
TimeProfiler::get_instance().enter_scope(name);
305305
}
306-
TimeProfileScope::~TimeProfileScope()
306+
TimeProfilerScope::~TimeProfilerScope()
307307
{
308-
TimeProfiler::instance().leave_scope();
308+
TimeProfiler::get_instance().leave_scope();
309309
}
310310

311311
} // namespace esp_utils

src/profiler/esp_utils_time_profiler.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TimeProfiler {
5757
using Clock = std::chrono::high_resolution_clock;
5858
using TimePoint = std::chrono::time_point<Clock>;
5959

60-
static TimeProfiler &instance();
60+
static TimeProfiler &get_instance();
6161

6262
// ---------- 配置 ----------
6363
void set_format_options(const FormatOptions &options);
@@ -111,12 +111,16 @@ class TimeProfiler {
111111
std::vector<Node *> sorted_children(Node *node) const;
112112
};
113113

114-
class TimeProfileScope {
114+
class TimeProfilerScope {
115115
public:
116-
explicit TimeProfileScope(const std::string &name);
117-
~TimeProfileScope();
116+
explicit TimeProfilerScope(const std::string &name);
117+
~TimeProfilerScope();
118118
};
119119

120120
} // namespace esp_utils
121121

122-
#define ESP_UTILS_TIME_PROFILE_SCOPE(name) esp_utils::TimeProfileScope _profile_scope_##__LINE__(name)
122+
#define ESP_UTILS_TIME_PROFILER_SCOPE(name) esp_utils::TimeProfilerScope _profile_scope_##__LINE__(name)
123+
124+
#define ESP_UTILS_TIME_PROFILER_START_EVENT(name) esp_utils::TimeProfiler::get_instance().start_event(name)
125+
126+
#define ESP_UTILS_TIME_PROFILER_END_EVENT(name) esp_utils::TimeProfiler::get_instance().end_event(name)

test_apps/main/test_profiler.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ using namespace esp_utils;
1818

1919
void do_test(size_t sleep_time)
2020
{
21-
ESP_UTILS_TIME_PROFILE_SCOPE("do_test");
21+
ESP_UTILS_TIME_PROFILER_SCOPE("do_test");
2222
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
2323
}
2424

2525
void work_task()
2626
{
27-
ESP_UTILS_TIME_PROFILE_SCOPE("work_task");
27+
ESP_UTILS_TIME_PROFILER_SCOPE("work_task");
2828
do_test(12 + rand() % 8);
2929
}
3030

3131
std::thread async_task(int index)
3232
{
3333
std::string event_name = "cross_thread_event";
34-
TimeProfiler::instance().start_event(event_name);
34+
TimeProfiler::get_instance().start_event(event_name);
3535
return std::thread([event_name, index] {
36-
ESP_UTILS_TIME_PROFILE_SCOPE("async_task_" + std::to_string(index));
36+
ESP_UTILS_TIME_PROFILER_SCOPE("async_task_" + std::to_string(index));
3737
do_test(40);
38-
TimeProfiler::instance().end_event(event_name);
38+
TimeProfiler::get_instance().end_event(event_name);
3939
});
4040
}
4141

4242
TEST_CASE("Test profiler functions on cpp", "[utils][plugin][CPP]")
4343
{
44-
auto &prof = TimeProfiler::instance();
44+
auto &prof = TimeProfiler::get_instance();
4545
TimeProfiler::FormatOptions opt;
4646
opt.use_unicode = true;
4747
opt.use_color = true; // 开启颜色高亮(>50%红色,>20%黄色,>5%青色)
@@ -58,7 +58,7 @@ TEST_CASE("Test profiler functions on cpp", "[utils][plugin][CPP]")
5858
std::vector<std::thread> workers;
5959
workers.reserve(5);
6060
for (int i = 0; i < 5; ++i) {
61-
ESP_UTILS_TIME_PROFILE_SCOPE("main_iteration");
61+
ESP_UTILS_TIME_PROFILER_SCOPE("main_iteration");
6262
work_task();
6363
workers.emplace_back(async_task(i));
6464
}
@@ -67,7 +67,7 @@ TEST_CASE("Test profiler functions on cpp", "[utils][plugin][CPP]")
6767
t.join();
6868
}
6969
}
70-
TimeProfiler::instance().report();
70+
TimeProfiler::get_instance().report();
7171

72-
TimeProfiler::instance().clear();
72+
TimeProfiler::get_instance().clear();
7373
}

0 commit comments

Comments
 (0)