mirror of
https://github.com/esphome/esphome.git
synced 2025-08-06 18:37:47 +00:00
fixes
This commit is contained in:
parent
9c09a271f2
commit
e4c0f18ee3
@ -0,0 +1,19 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
defer_stress_component_ns = cg.esphome_ns.namespace("defer_stress_component")
|
||||
DeferStressComponent = defer_stress_component_ns.class_(
|
||||
"DeferStressComponent", cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(DeferStressComponent),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
@ -0,0 +1,86 @@
|
||||
#include "defer_stress_component.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
namespace esphome {
|
||||
namespace defer_stress_component {
|
||||
|
||||
static const char *const TAG = "defer_stress";
|
||||
|
||||
void DeferStressComponent::setup() { ESP_LOGCONFIG(TAG, "DeferStressComponent setup"); }
|
||||
|
||||
void DeferStressComponent::run_multi_thread_test() {
|
||||
// Use member variables instead of static to avoid issues
|
||||
this->total_defers_ = 0;
|
||||
this->executed_defers_ = 0;
|
||||
static constexpr int NUM_THREADS = 10;
|
||||
static constexpr int DEFERS_PER_THREAD = 100;
|
||||
|
||||
ESP_LOGI(TAG, "Starting defer stress test - multi-threaded concurrent defers");
|
||||
|
||||
// Ensure we're starting clean
|
||||
ESP_LOGI(TAG, "Initial counters: total=%d, executed=%d", this->total_defers_.load(), this->executed_defers_.load());
|
||||
|
||||
// Track start time
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
|
||||
// Create threads
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
ESP_LOGI(TAG, "Creating %d threads, each will defer %d callbacks", NUM_THREADS, DEFERS_PER_THREAD);
|
||||
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
threads.emplace_back([this, i]() {
|
||||
ESP_LOGV(TAG, "Thread %d starting", i);
|
||||
// Each thread directly calls defer() without any locking
|
||||
for (int j = 0; j < DEFERS_PER_THREAD; j++) {
|
||||
int defer_id = this->total_defers_.fetch_add(1);
|
||||
ESP_LOGV(TAG, "Thread %d calling defer for request %d", i, defer_id);
|
||||
|
||||
// Capture this pointer safely for the lambda
|
||||
auto *component = this;
|
||||
|
||||
// Directly call defer() from this thread - no locking!
|
||||
this->defer([component, defer_id]() {
|
||||
component->executed_defers_.fetch_add(1);
|
||||
ESP_LOGV(TAG, "Executed defer %d", defer_id);
|
||||
});
|
||||
|
||||
ESP_LOGV(TAG, "Thread %d called defer for request %d successfully", i, defer_id);
|
||||
|
||||
// Small random delay to increase contention
|
||||
if (j % 10 == 0) {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
}
|
||||
}
|
||||
ESP_LOGV(TAG, "Thread %d finished", i);
|
||||
});
|
||||
}
|
||||
|
||||
// Wait for all threads to complete
|
||||
for (auto &t : threads) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
auto end_time = std::chrono::steady_clock::now();
|
||||
auto thread_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
|
||||
ESP_LOGI(TAG, "All threads finished in %lldms. Created %d defer requests", thread_time, this->total_defers_.load());
|
||||
|
||||
// Store the final values for checking
|
||||
this->expected_total_ = NUM_THREADS * DEFERS_PER_THREAD;
|
||||
this->test_complete_ = true;
|
||||
}
|
||||
|
||||
int DeferStressComponent::get_total_defers() { return this->total_defers_.load(); }
|
||||
|
||||
int DeferStressComponent::get_executed_defers() { return this->executed_defers_.load(); }
|
||||
|
||||
bool DeferStressComponent::is_test_complete() { return this->test_complete_; }
|
||||
|
||||
int DeferStressComponent::get_expected_total() { return this->expected_total_; }
|
||||
|
||||
} // namespace defer_stress_component
|
||||
} // namespace esphome
|
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include <atomic>
|
||||
|
||||
namespace esphome {
|
||||
namespace defer_stress_component {
|
||||
|
||||
class DeferStressComponent : public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void run_multi_thread_test();
|
||||
|
||||
// Getters for test status
|
||||
int get_total_defers();
|
||||
int get_executed_defers();
|
||||
bool is_test_complete();
|
||||
int get_expected_total();
|
||||
|
||||
private:
|
||||
std::atomic<int> total_defers_{0};
|
||||
std::atomic<int> executed_defers_{0};
|
||||
bool test_complete_{false};
|
||||
int expected_total_{0};
|
||||
};
|
||||
|
||||
} // namespace defer_stress_component
|
||||
} // namespace esphome
|
Loading…
x
Reference in New Issue
Block a user