Better metrics.

This commit is contained in:
Fabian Schlenz 2020-04-29 20:24:59 +02:00
parent 65dd09ca0d
commit 8e2d2225cb
4 changed files with 31 additions and 8 deletions

View File

@ -8,8 +8,12 @@ struct EffectEntry {
const char* name; const char* name;
bool use_in_cycle; bool use_in_cycle;
std::function<Effect*()> create; std::function<Effect*()> 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 const uint8_t effects_size;
extern Effect* current_effect; extern Effect* current_effect;

View File

@ -26,31 +26,46 @@ void CycleEffect::changeEffect() {
LOGln("CycleEffect * Changing effect from #%d to #%d", effect_id, new_id); LOGln("CycleEffect * Changing effect from #%d to #%d", effect_id, new_id);
delay(25); 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; uint16_t old_heap = _heap_free;
_heap_free = ESP.getFreeHeap(); _heap_free = ESP.getFreeHeap();
if (old_heap) { if (old_heap) {
// diff positive = More heap used (baad) // diff positive = More heap used (baad)
// diff negative = Less heap used (good-ish) // diff negative = Less heap used (good-ish)
diff = old_heap - _heap_free; 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); delay(25);
LOGln("CycleEffect * Searching for new effect #%d", new_id); LOGln("CycleEffect * Searching for new effect #%d", new_id);
uint8_t count = 0; uint8_t count = 0;
EffectEntry* e = nullptr;
for (uint8_t i=0; i<effects_size; i++) { for (uint8_t i=0; i<effects_size; i++) {
if (effects[i].use_in_cycle) { if (effects[i].use_in_cycle) {
if (count == new_id) { if (count == new_id) {
effect = effects[i].create(); e = &effects[i];
effect = e->create();
break; break;
} }
count++; 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; effect_id = new_id;
effectSince = millis(); effectSince = millis();
LOGln("CycleEffect * Effect %s found", effect->get_name().c_str()); LOGln("CycleEffect * Effect %s found", effect->get_name().c_str());

View File

@ -23,7 +23,7 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
pl[length] = '\0'; pl[length] = '\0';
String payload((char*)pl); String payload((char*)pl);
String topic (original_topic); 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 our own messages
return; return;
} }

View File

@ -16,9 +16,13 @@ namespace tests {
for (int j=0; j<3; j++) { for (int j=0; j<3; j++) {
int free_at_start = ESP.getFreeHeap(); int free_at_start = ESP.getFreeHeap();
effect = select_effect(i); effect = select_effect(i);
effect->loop(1);
if (effect == NULL) return; if (effect == NULL) return;
effect_name = effect->get_name(); 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; delete effect;
diffs[i] = ESP.getFreeHeap() - free_at_start; diffs[i] = ESP.getFreeHeap() - free_at_start;
} }