1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-13 11:42:18 +00:00
ton/tdutils/td/utils/Timer.cpp

88 lines
2.4 KiB
C++
Raw Normal View History

2019-09-07 10:03:22 +00:00
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
2020-04-10 19:06:01 +00:00
Copyright 2017-2020 Telegram Systems LLP
2019-09-07 10:03:22 +00:00
*/
#include "td/utils/Timer.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/Time.h"
namespace td {
2019-09-23 21:10:57 +00:00
Timer::Timer(bool is_paused) : is_paused_(is_paused) {
if (is_paused_) {
start_time_ = 0;
} else {
start_time_ = Time::now();
}
}
void Timer::pause() {
if (is_paused_) {
return;
}
elapsed_ += Time::now() - start_time_;
is_paused_ = true;
}
void Timer::resume() {
if (!is_paused_) {
return;
}
start_time_ = Time::now();
is_paused_ = false;
2019-09-07 10:03:22 +00:00
}
double Timer::elapsed() const {
2019-09-23 21:10:57 +00:00
double res = elapsed_;
if (!is_paused_) {
res += Time::now() - start_time_;
}
return res;
2019-09-07 10:03:22 +00:00
}
StringBuilder &operator<<(StringBuilder &string_builder, const Timer &timer) {
2019-09-23 21:10:57 +00:00
return string_builder << format::as_time(timer.elapsed());
2019-09-07 10:03:22 +00:00
}
PerfWarningTimer::PerfWarningTimer(string name, double max_duration, std::function<void(double)>&& callback)
: name_(std::move(name)), start_at_(Time::now()), max_duration_(max_duration), callback_(std::move(callback)) {
2019-09-07 10:03:22 +00:00
}
PerfWarningTimer::PerfWarningTimer(PerfWarningTimer &&other)
: name_(std::move(other.name_)), start_at_(other.start_at_), max_duration_(other.max_duration_), callback_(std::move(other.callback_)) {
2019-09-07 10:03:22 +00:00
other.start_at_ = 0;
}
PerfWarningTimer::~PerfWarningTimer() {
reset();
}
void PerfWarningTimer::reset() {
if (start_at_ == 0) {
return;
}
double duration = Time::now() - start_at_;
//LOG_IF(WARNING, duration > max_duration_)
//<< "SLOW: " << tag("name", name_) << tag("duration", format::as_time(duration));
callback_(duration);
2019-09-07 10:03:22 +00:00
start_at_ = 0;
}
} // namespace td