Add performance monitoring feature toggle and integration

- Introduced a new feature flag for performance monitoring in the FeatureFlags class, allowing users to enable or disable monitoring.
- Updated the ScopedTimer class to respect the performance monitoring flag, ensuring that timing operations are no-ops when monitoring is disabled.
- Enhanced the PerformanceMonitor class with methods to set and check the enabled state of performance monitoring, improving flexibility for production builds.
This commit is contained in:
scawful
2025-09-28 22:42:19 -04:00
parent b911256687
commit b82751f9ea
3 changed files with 32 additions and 3 deletions

View File

@@ -38,6 +38,9 @@ class FeatureFlags {
// Log to the console. // Log to the console.
bool kLogToConsole = false; bool kLogToConsole = false;
// Enable performance monitoring and timing.
bool kEnablePerformanceMonitoring = true;
// Use NFD (Native File Dialog) instead of bespoke file dialog implementation. // Use NFD (Native File Dialog) instead of bespoke file dialog implementation.
#if defined(YAZE_ENABLE_NFD) && YAZE_ENABLE_NFD #if defined(YAZE_ENABLE_NFD) && YAZE_ENABLE_NFD
bool kUseNativeFileDialog = true; bool kUseNativeFileDialog = true;
@@ -164,6 +167,7 @@ struct FlagsMenu {
void DrawSystemFlags() { void DrawSystemFlags() {
Checkbox("Enable Console Logging", &FeatureFlags::get().kLogToConsole); Checkbox("Enable Console Logging", &FeatureFlags::get().kLogToConsole);
Checkbox("Enable Performance Monitoring", &FeatureFlags::get().kEnablePerformanceMonitoring);
Checkbox("Log Instructions to Emulator Debugger", Checkbox("Log Instructions to Emulator Debugger",
&FeatureFlags::get().kLogInstructions); &FeatureFlags::get().kLogInstructions);
Checkbox("Use Native File Dialog (NFD)", &FeatureFlags::get().kUseNativeFileDialog); Checkbox("Use Native File Dialog (NFD)", &FeatureFlags::get().kUseNativeFileDialog);

View File

@@ -3,6 +3,8 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include "app/core/features.h"
namespace yaze { namespace yaze {
namespace core { namespace core {
@@ -85,12 +87,16 @@ void PerformanceMonitor::PrintSummary() const {
} }
ScopedTimer::ScopedTimer(const std::string& operation_name) ScopedTimer::ScopedTimer(const std::string& operation_name)
: operation_name_(operation_name) { : operation_name_(operation_name), enabled_(core::FeatureFlags::get().kEnablePerformanceMonitoring) {
PerformanceMonitor::Get().StartTimer(operation_name_); if (enabled_) {
PerformanceMonitor::Get().StartTimer(operation_name_);
}
} }
ScopedTimer::~ScopedTimer() { ScopedTimer::~ScopedTimer() {
PerformanceMonitor::Get().EndTimer(operation_name_); if (enabled_) {
PerformanceMonitor::Get().EndTimer(operation_name_);
}
} }
} // namespace core } // namespace core

View File

@@ -22,6 +22,23 @@ class PerformanceMonitor {
static PerformanceMonitor instance; static PerformanceMonitor instance;
return instance; return instance;
} }
/**
* @brief Enable or disable performance monitoring
*
* When disabled, ScopedTimer operations become no-ops for better performance
* in production builds or when monitoring is not needed.
*/
static void SetEnabled(bool enabled) {
Get().enabled_ = enabled;
}
/**
* @brief Check if performance monitoring is enabled
*/
static bool IsEnabled() {
return Get().enabled_;
}
/** /**
* @brief Start timing an operation * @brief Start timing an operation
@@ -72,6 +89,7 @@ class PerformanceMonitor {
}; };
std::unordered_map<std::string, OperationData> operations_; std::unordered_map<std::string, OperationData> operations_;
bool enabled_ = true; // Performance monitoring enabled by default
}; };
/** /**
@@ -91,6 +109,7 @@ class ScopedTimer {
private: private:
std::string operation_name_; std::string operation_name_;
bool enabled_;
}; };
} // namespace core } // namespace core