185147189SShuo Chen#pragma once 285147189SShuo Chen 385147189SShuo Chen#include <sys/time.h> 485147189SShuo Chen 585147189SShuo Chen#include "absl/strings/str_format.h" 685147189SShuo Chen#include "muduo/base/ProcessInfo.h" 785147189SShuo Chen#include "muduo/base/Timestamp.h" 885147189SShuo Chen 985147189SShuo Chenclass Timer 1085147189SShuo Chen{ 1185147189SShuo Chen public: 1285147189SShuo Chen Timer() 1385147189SShuo Chen : start_(now()), 1485147189SShuo Chen start_cpu_(muduo::ProcessInfo::cpuTime()) 1585147189SShuo Chen { 1685147189SShuo Chen } 1785147189SShuo Chen 1885147189SShuo Chen std::string report(int64_t bytes) const 1985147189SShuo Chen { 2085147189SShuo Chen muduo::ProcessInfo::CpuTime end_cpu(muduo::ProcessInfo::cpuTime()); 2185147189SShuo Chen double seconds = now() - start_; 223e607da5SShuo Chen char buf[64]; 233e607da5SShuo Chen snprintf(buf, sizeof buf, "%'zd", bytes); 243e607da5SShuo Chen return absl::StrFormat("%.2fs real %.2fs cpu %6.2f MiB/s %s bytes", 2585147189SShuo Chen seconds, end_cpu.total() - start_cpu_.total(), 263e607da5SShuo Chen bytes / seconds / 1024 / 1024, buf); 2785147189SShuo Chen } 2885147189SShuo Chen 2985147189SShuo Chen static double now() 3085147189SShuo Chen { 3185147189SShuo Chen struct timeval tv = { 0, 0 }; 3285147189SShuo Chen gettimeofday(&tv, nullptr); 3385147189SShuo Chen return tv.tv_sec + tv.tv_usec / 1000000.0; 3485147189SShuo Chen } 3585147189SShuo Chen 3685147189SShuo Chen private: 3785147189SShuo Chen const double start_; 3885147189SShuo Chen const muduo::ProcessInfo::CpuTime start_cpu_; 3985147189SShuo Chen}; 40