Created
July 13, 2021 12:13
-
-
Save a9QrX3Lu/81121d697e3e6831955d2aea77ff106f to your computer and use it in GitHub Desktop.
Breakdown latency of a C++ program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef LAT_BREAKDOWN_H_ | |
| #define LAT_BREAKDOWN_H_ | |
| #include <chrono> | |
| namespace utils { | |
| struct LatBreakdown { | |
| std::chrono::nanoseconds total_lat_ = static_cast<std::chrono::nanoseconds>(0); | |
| std::vector<std::chrono::nanoseconds> lats_; | |
| std::vector<std::pair<std::string, int>> pos_; | |
| std::chrono::steady_clock::time_point ts_; | |
| inline void Init() { | |
| lats_.reserve(32); | |
| ts_ = std::chrono::steady_clock::now(); | |
| } | |
| inline void Tracepoint(std::string_view file, int line) { | |
| std::chrono::steady_clock::time_point curr_ts = | |
| std::chrono::steady_clock::now(); | |
| std::chrono::nanoseconds time_elapse = | |
| std::chrono::duration_cast<std::chrono::nanoseconds>(curr_ts - ts_); | |
| lats_.push_back(time_elapse); | |
| pos_.emplace_back(file, line); | |
| total_lat_ += time_elapse; | |
| ts_ = curr_ts; | |
| } | |
| }; | |
| struct NullLatBreakdown { | |
| constexpr NullLatBreakdown() noexcept = default; | |
| inline void Init() noexcept { | |
| // Dummy Init | |
| } | |
| inline void Tracepoint(std::string_view file, int line) noexcept { | |
| // Dummy Tracepoint | |
| } | |
| }; | |
| #ifndef LAT_BREAKDOWN | |
| #define LatBreakdown NullLatBreakdown | |
| #endif | |
| } // namespace utils | |
| #endif // LAT_BREAKDOWN_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment