1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

improve statistics

This commit is contained in:
wellenvogel
2021-12-05 20:45:49 +01:00
parent b089ae2a39
commit 2e5b3b37d7
4 changed files with 117 additions and 97 deletions

View File

@@ -72,6 +72,7 @@ const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting
#include "GwXDRMappings.h"
#include "GwSynchronized.h"
#include "GwUserCode.h"
#include "GwStatistics.h"
//NMEA message channels
@@ -845,90 +846,8 @@ class NMEAMessageReceiver : public GwMessageFetcher{
return true;
}
};
class TimeMonitor{
public:
unsigned long *times=NULL;
size_t len;
TimeMonitor(size_t len){
this->len=len;
times=new unsigned long[len];
reset();
}
void reset(){
if (len>0) times[0]=millis();
for (size_t i=1;i<len;i++){
times[i]=0;
}
}
unsigned long getMax(){
unsigned long maxTime=0;
for (size_t i=1;i< len;i++){
if (times[i] != 0){
if (times[i] > maxTime) maxTime=times[i];
}
}
if (maxTime > 0) return maxTime-times[0];
return 0;
}
void writeLog(unsigned long minTime){
unsigned long maxTime=getMax();
if (maxTime == 0 || maxTime < minTime) return;
String log;
for (size_t i=1;i<len;i++){
if (times[i] != 0){
log+=String(i);
log+=":";
log+=String((times[i]-times[0]));
log+=",";
}
}
logger.logDebug(GwLog::LOG,"times: %s",log.c_str());
}
void setTime(size_t index){
if (index < 1 || index >= len) return;
times[index]=millis();
}
};
class TimeAverage{
size_t len;
size_t wp=0;
size_t filled=0;
unsigned long *diffs=NULL;
unsigned long count=0;
unsigned long sum=0;
public:
TimeAverage(size_t len){
this->len=len;
wp=0;
filled=0;
diffs=new unsigned long[len];
}
void add(unsigned long diff){
count++;
sum+=diff;
diffs[wp]=diff;
wp++;
if (filled < len){
filled++;
}
else{
if (wp >= len) wp=0;
sum-=diffs[wp];
}
}
unsigned long current(){
if (filled == 0) return 0;
return sum*1000/filled;
}
unsigned long getCount(){
return count;
}
};
TimeMonitor monitor(20);
TimeAverage average(50);
unsigned long lastReportCount=0;
TimeMonitor monitor(20,0.2);
NMEAMessageReceiver receiver;
unsigned long lastHeapReport=0;
void loop() {
@@ -946,11 +865,7 @@ void loop() {
(long)xPortGetFreeHeapSize(),
(long)xPortGetMinimumEverFreeHeapSize()
);
logger.logDebug(GwLog::DEBUG,"Main loop count=%d, average=%dus",
(int)(average.getCount()-lastReportCount),
(int)average.current()
);
lastReportCount=average.getCount();
logger.logDebug(GwLog::DEBUG,"Main loop %s",monitor.getLog().c_str());
}
}
monitor.setTime(3);
@@ -999,8 +914,4 @@ void loop() {
msg->unref();
}
monitor.setTime(15);
average.add(monitor.getMax());
if (logger.isActive(GwLog::LOG)){
monitor.writeLog(6);
}
}