From eb6a7cf3b9f89924e0197e80f6a564595053f4ab Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 20 Jun 2025 22:02:19 +0200 Subject: [PATCH] fix last component being charged for stats --- esphome/core/application.cpp | 4 ++++ esphome/core/runtime_stats.cpp | 17 ++++++++++++----- esphome/core/runtime_stats.h | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 49c1e5fd61..43e7b79b8a 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -136,6 +136,10 @@ void Application::loop() { this->in_loop_ = false; this->app_state_ = new_app_state; + // Process any pending runtime stats printing after all components have run + // This ensures stats printing doesn't affect component timing measurements + runtime_stats.process_pending_stats(last_op_end_time); + // Use the last component's end time instead of calling millis() again auto elapsed = last_op_end_time - this->last_loop_; if (elapsed >= this->loop_interval_ || HighFrequencyLoopRequester::is_high_frequency()) { diff --git a/esphome/core/runtime_stats.cpp b/esphome/core/runtime_stats.cpp index ec49835752..0ce0d29e8d 100644 --- a/esphome/core/runtime_stats.cpp +++ b/esphome/core/runtime_stats.cpp @@ -28,11 +28,7 @@ void RuntimeStatsCollector::record_component_time(Component *component, uint32_t return; } - if (current_time >= this->next_log_time_) { - this->log_stats_(); - this->reset_stats_(); - this->next_log_time_ = current_time + this->log_interval_; - } + // Don't print stats here anymore - let process_pending_stats handle it } void RuntimeStatsCollector::log_stats_() { @@ -82,4 +78,15 @@ void RuntimeStatsCollector::log_stats_() { } } +void RuntimeStatsCollector::process_pending_stats(uint32_t current_time) { + if (!this->enabled_ || this->next_log_time_ == 0) + return; + + if (current_time >= this->next_log_time_) { + this->log_stats_(); + this->reset_stats_(); + this->next_log_time_ = current_time + this->log_interval_; + } +} + } // namespace esphome \ No newline at end of file diff --git a/esphome/core/runtime_stats.h b/esphome/core/runtime_stats.h index ca5dcb9310..6ae80750a6 100644 --- a/esphome/core/runtime_stats.h +++ b/esphome/core/runtime_stats.h @@ -95,6 +95,9 @@ class RuntimeStatsCollector { void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time); + // Process any pending stats printing (should be called after component loop) + void process_pending_stats(uint32_t current_time); + protected: void log_stats_();