From d0901aa7933ac31b0bf7648a31ec5c13de864457 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 11 Jul 2023 09:29:27 -0700 Subject: Add perf benchmark utility. (#2977) * Add perf benchmark utility. * Update documentation. * Fix. * Fix doc. --------- Co-authored-by: Yong He --- source/core/slang-performance-profiler.cpp | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 source/core/slang-performance-profiler.cpp (limited to 'source/core/slang-performance-profiler.cpp') diff --git a/source/core/slang-performance-profiler.cpp b/source/core/slang-performance-profiler.cpp new file mode 100644 index 000000000..f5f74f99d --- /dev/null +++ b/source/core/slang-performance-profiler.cpp @@ -0,0 +1,52 @@ +#include "slang-performance-profiler.h" +#include "slang-dictionary.h" + +namespace Slang +{ + struct FuncProfileInfo + { + int invocationCount = 0; + std::chrono::nanoseconds duration = std::chrono::nanoseconds::zero(); + }; + class PerformanceProfilerImpl : public PerformanceProfiler + { + public: + OrderedDictionary data; + + virtual FuncProfileContext enterFunction(const char* funcName) override + { + auto entry = data.tryGetValue(funcName); + if (!entry) + { + data.add(funcName, FuncProfileInfo()); + entry = data.tryGetValue(funcName); + } + entry->invocationCount++; + FuncProfileContext ctx; + ctx.funcName = funcName; + ctx.startTime = std::chrono::high_resolution_clock::now(); + return ctx; + } + virtual void exitFunction(FuncProfileContext ctx) override + { + auto endTime = std::chrono::high_resolution_clock::now(); + auto duration = endTime - ctx.startTime; + auto entry = data.tryGetValue(ctx.funcName); + entry->duration += duration; + } + virtual void getResult(StringBuilder& out) override + { + for (auto func : data) + { + out << func.key << ": \t"; + out << func.value.invocationCount << "\t" << func.value.duration.count()/1000000 << "\n"; + } + } + }; + + PerformanceProfiler* Slang::PerformanceProfiler::getProfiler() + { + static PerformanceProfilerImpl profiler = PerformanceProfilerImpl(); + return &profiler; + } +} -- cgit v1.2.3