//
// Created by zx on 22-11-3.
//

#ifndef SRC_LIO_LIVOX_SRC_UTILS_TIMERRECORD_H_
#define SRC_LIO_LIVOX_SRC_UTILS_TIMERRECORD_H_

#include <map>
#include <mutex>
#include <string>
#include <vector>
#include <chrono>
#include <numeric>
#include <iostream>
#include <algorithm>

struct FuncRecord
{
 public:
    FuncRecord()=default;
    FuncRecord(std::string name,double time)
    {
        name_=name;
        timesqueue_.emplace_back(time);
    }

    std::string name_;
    std::vector<double> timesqueue_;
};

class TimerRecord
{
 public:

 public:
    template<class F>
    inline static void Execute(F func, const std::string &func_name)
    {
      auto t1 = std::chrono::high_resolution_clock::now();
      func();
      auto t2 = std::chrono::high_resolution_clock::now();
      auto time_used = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count() * 1000;
      mutex_.lock();
      if (records_.find(func_name) != records_.end())
      {
        records_[func_name].timesqueue_.emplace_back(time_used);
      }
      else
      {
        records_.insert({func_name, FuncRecord(func_name, time_used)});
      }
      mutex_.unlock();
    }

    static void PrintAll();


 protected:
    static std::mutex mutex_;
    static std::map<std::string, FuncRecord> records_;
};




#endif //SRC_LIO_LIVOX_SRC_UTILS_TIMERRECORD_H_
