From 8e2d2225cba14bfdd5c34c569bf794ce5eae6bfc Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 29 Apr 2020 20:24:59 +0200 Subject: [PATCH] Better metrics. --- include/effects.h | 6 +++++- src/effect_cycle.cpp | 25 ++++++++++++++++++++----- src/mqtt.cpp | 2 +- src/tests.cpp | 6 +++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/effects.h b/include/effects.h index b44e3e7..40e64b1 100644 --- a/include/effects.h +++ b/include/effects.h @@ -8,8 +8,12 @@ struct EffectEntry { const char* name; bool use_in_cycle; std::function create; +#ifdef MQTT_REPORT_METRICS + int16_t heap_change_sum; + uint16_t run_count; +#endif }; -extern const EffectEntry effects[]; +extern EffectEntry effects[]; extern const uint8_t effects_size; extern Effect* current_effect; diff --git a/src/effect_cycle.cpp b/src/effect_cycle.cpp index 9ad5429..843c78d 100644 --- a/src/effect_cycle.cpp +++ b/src/effect_cycle.cpp @@ -26,31 +26,46 @@ void CycleEffect::changeEffect() { LOGln("CycleEffect * Changing effect from #%d to #%d", effect_id, new_id); delay(25); - if (effect) delete effect; + String old_effect_name = String("UNKNOWN"); + if (effect) { + old_effect_name = effect->get_name(); + delete effect; + } - int16_t diff; + int16_t diff = 0; uint16_t old_heap = _heap_free; _heap_free = ESP.getFreeHeap(); if (old_heap) { // diff positive = More heap used (baad) // diff negative = Less heap used (good-ish) diff = old_heap - _heap_free; - LOGln("CycleEffect * Heap usage: #%d,%d,%+d", effect_id, _heap_free, diff); + LOGln("CycleEffect * Heap usage: #%d,%s,%d,%+d", effect_id, old_effect_name.c_str(), _heap_free, diff); } delay(25); LOGln("CycleEffect * Searching for new effect #%d", new_id); uint8_t count = 0; + EffectEntry* e = nullptr; for (uint8_t i=0; icreate(); break; } count++; } } - if (effect) { + if (e) { +#ifdef MQTT_REPORT_METRICS + e->heap_change_sum += diff; + e->run_count++; + LOGln("CycleEffect * Last effect stats: name:%s, runs:%d, total_change:%d", old_effect_name.c_str(), e->run_count, e->heap_change_sum); + String topic = "metrics/effects/"; + topic.concat(old_effect_name); + String message = String("runs:") + e->run_count + ", total_heap_change:" + e->heap_change_sum; + mqtt_publish(topic.c_str(), message.c_str(), true); +#endif effect_id = new_id; effectSince = millis(); LOGln("CycleEffect * Effect %s found", effect->get_name().c_str()); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 947001c..f3561fd 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -23,7 +23,7 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { pl[length] = '\0'; String payload((char*)pl); String topic (original_topic); - if (topic.equals(MQTT_TOPIC "log") || topic.equals(MQTT_TOPIC "status") || topic.equals(MQTT_TOPIC "metrics")) { + if (topic.equals(MQTT_TOPIC "log") || topic.equals(MQTT_TOPIC "status") || topic.startsWith(MQTT_TOPIC "metrics")) { // Return our own messages return; } diff --git a/src/tests.cpp b/src/tests.cpp index 056d55f..4d2c546 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -16,9 +16,13 @@ namespace tests { for (int j=0; j<3; j++) { int free_at_start = ESP.getFreeHeap(); effect = select_effect(i); - effect->loop(1); if (effect == NULL) return; effect_name = effect->get_name(); + if (effect_name && !effect_name.equals("cycle")) { + LOGln("Testing effect %s...", effect_name.c_str()); + delay(1); + effect->loop(1); + } delete effect; diffs[i] = ESP.getFreeHeap() - free_at_start; }