From e08bcf1009e6a34b982164c310b6853a9368a705 Mon Sep 17 00:00:00 2001 From: wellenvogel Date: Sat, 11 Dec 2021 21:23:57 +0100 Subject: [PATCH] use esp us timer for statistics --- lib/log/GwLog.h | 2 +- lib/statistics/GwStatistics.h | 65 ++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/log/GwLog.h b/lib/log/GwLog.h index af49332..943b9e4 100644 --- a/lib/log/GwLog.h +++ b/lib/log/GwLog.h @@ -10,7 +10,7 @@ class GwLogWriter{ }; class GwLog{ private: - static const size_t bufferSize=200; + static const size_t bufferSize=250; char buffer[bufferSize]; int logLevel=1; GwLogWriter *writer; diff --git a/lib/statistics/GwStatistics.h b/lib/statistics/GwStatistics.h index 6db9151..2feb232 100644 --- a/lib/statistics/GwStatistics.h +++ b/lib/statistics/GwStatistics.h @@ -4,13 +4,13 @@ class TimeAverage{ double factor=0.3; double current=0; unsigned long count=0; - unsigned long max=0; + int64_t max=0; bool init=false; public: TimeAverage(double factor){ this->factor=factor; } - void add(unsigned long diff){ + void add(int64_t diff){ if (diff > max) max=diff; count++; if (! init){ @@ -32,61 +32,61 @@ class TimeAverage{ void resetMax(){ max=0; } - unsigned long getMax(){ + int64_t getMax(){ return max; } }; class TimeMonitor{ + public: TimeAverage **times=NULL; - struct timeval *current=NULL; - struct timeval start; + TimeAverage *loop=NULL; + int64_t *current=NULL; + int64_t start=0; + int64_t last=0; unsigned long lastLog=0; unsigned long lastLogCount=0; unsigned long count=0; - unsigned long max=0; + int64_t max=0; size_t len; - unsigned long tdiff(struct timeval &tnew,struct timeval &told){ - time_t sdiff=tnew.tv_sec-told.tv_sec; - time_t udiff=0; - if (tnew.tv_usec < told.tv_usec){ - if (sdiff > 0) { - sdiff--; - udiff=tnew.tv_usec+1000000UL-told.tv_usec; + ~TimeMonitor(){ + for (size_t i=0;ilen=len; + loop=new TimeAverage(factor); times=new TimeAverage*[len]; for (size_t i=0;iadd(last-start); + start=esp_timer_get_time(); + for (size_t i=0;i 0){ log+=String((double)(num*1000)/(double)tdif); - log+="/s["; - log+=String(max); + log+="/s"; + log+=String(loop->getCurrent()); + log+="["; + log+=String((unsigned long)max); log+="us]#"; } } @@ -99,7 +99,7 @@ class TimeMonitor{ log+=":"; log+=String(times[i]->getCurrent()); log+="["; - log+=String(times[i]->getMax()); + log+=String((unsigned long)(times[i]->getMax())); log+="],"; } } @@ -110,14 +110,15 @@ class TimeMonitor{ } void setTime(size_t index){ if (index < 1 || index >= len) return; - struct timeval *sv=&start; + int64_t sv=start; for (size_t i=0;i max) max=startDiff; + int64_t now=esp_timer_get_time(); + last=now; + current[index]=now; + int64_t currentv=now-sv; + if ((now-start) > max) max=now-start; times[index-1]->add(currentv); } };