From e92c4c49ca251c29e70717410e38b9a1352bd958 Mon Sep 17 00:00:00 2001 From: insunaa Date: Sat, 31 Aug 2024 15:12:22 +0200 Subject: [PATCH] Timer: Add a timer for code-benchmarking with RAII Closes https://github.com/cmangos/mangos-wotlk/pull/555 --- src/shared/Util/CodeBench.cpp | 27 ++++++++++++++++++ src/shared/Util/CodeBench.h | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/shared/Util/CodeBench.cpp create mode 100644 src/shared/Util/CodeBench.h diff --git a/src/shared/Util/CodeBench.cpp b/src/shared/Util/CodeBench.cpp new file mode 100644 index 00000000000..2676031e377 --- /dev/null +++ b/src/shared/Util/CodeBench.cpp @@ -0,0 +1,27 @@ +/* + * This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "CodeBench.h" +#include "Log/Log.h" + +ChronoTimeTracker::~ChronoTimeTracker() +{ + std::chrono::nanoseconds elapsed = elapsedNanos(); + sLog.outError("%s: time elapsed: %ldns, %ldµs, %ldms, %lds", + m_name.c_str(), nanos(elapsed).count(), micros(elapsed).count(), millis(elapsed).count(), secs(elapsed).count()); +} diff --git a/src/shared/Util/CodeBench.h b/src/shared/Util/CodeBench.h new file mode 100644 index 00000000000..397038fd17a --- /dev/null +++ b/src/shared/Util/CodeBench.h @@ -0,0 +1,53 @@ +/* + * This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MANGOS_TIMER_H +#define MANGOS_TIMER_H + +struct ChronoTimeTracker +{ + public: + ChronoTimeTracker() : m_startTime(std::chrono::steady_clock::now()), m_name("Unnamed Timer") {} + ChronoTimeTracker(std::string name) : m_startTime(std::chrono::steady_clock::now()), m_name(name) {} + ~ChronoTimeTracker(); + std::chrono::seconds elapsedSeconds() + { + return std::chrono::duration_cast(std::chrono::steady_clock::now() - m_startTime); + } + std::chrono::milliseconds elapsedMillis() + { + return std::chrono::duration_cast(std::chrono::steady_clock::now() - m_startTime); + } + std::chrono::microseconds elapsedMicros() + { + return std::chrono::duration_cast(std::chrono::steady_clock::now() - m_startTime); + } + std::chrono::nanoseconds elapsedNanos() + { + return std::chrono::steady_clock::now() - m_startTime; + } + private: + std::chrono::time_point m_startTime; + std::string m_name; + inline constexpr std::chrono::nanoseconds nanos(std::chrono::nanoseconds nanos) { return nanos; } + inline constexpr std::chrono::microseconds micros(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast(nanos); } + inline constexpr std::chrono::milliseconds millis(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast(nanos); } + inline constexpr std::chrono::seconds secs(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast(nanos); } +}; + +#endif \ No newline at end of file