Kagome
Polkadot Runtime Engine in C++17
profiler.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "log/logger.hpp"
4 
5 #include <chrono>
6 
7 class TicToc {
8  std::string name_;
10  std::chrono::time_point<std::chrono::high_resolution_clock> t_;
11 
12  public:
13  TicToc(const std::string &name, const kagome::log::Logger &log)
14  : name_(name), log_(log) {
15  t_ = std::chrono::high_resolution_clock::now();
16  }
17 
18  void toc(int line = -1) {
19  auto prev = t_;
20  t_ = std::chrono::high_resolution_clock::now();
21  auto str = name_;
22  if (line != -1) {
23  str += "at line " + std::to_string(line);
24  }
25  log_->info(
26  "{} lasted for {} sec",
27  str,
28  std::chrono::duration_cast<std::chrono::seconds>(t_ - prev).count());
29  }
30 
31  ~TicToc() {
32  toc();
33  }
34 };
std::string_view to_string(SlotType s)
Definition: slot.hpp:22
~TicToc()
Definition: profiler.hpp:31
void toc(int line=-1)
Definition: profiler.hpp:18
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::string name_
Definition: profiler.hpp:8
std::chrono::time_point< std::chrono::high_resolution_clock > t_
Definition: profiler.hpp:10
TicToc(const std::string &name, const kagome::log::Logger &log)
Definition: profiler.hpp:13
const kagome::log::Logger & log_
Definition: profiler.hpp:9