1- #include < limits >
1+ #include " util/stat.h "
22
33#include < QStringList>
4+ #include < chrono>
5+ #include < limits>
46
5- #include " util/stat.h"
6- #include " util/time.h"
77#include " util/math.h"
88#include " util/statsmanager.h"
9+ #include " util/time.h"
910
1011Stat::Stat ()
1112 : m_type(UNSPECIFIED),
@@ -20,18 +21,18 @@ Stat::Stat()
2021
2122QString Stat::valueUnits () const {
2223 switch (m_type) {
23- case DURATION_MSEC:
24- return " ms" ;
25- case DURATION_NANOSEC:
26- return " ns" ;
27- case DURATION_SEC:
28- return " s" ;
29- case EVENT:
30- case EVENT_START:
31- case EVENT_END:
32- case UNSPECIFIED:
33- default :
34- return " " ;
24+ case DURATION_MSEC:
25+ return " ms" ;
26+ case DURATION_NANOSEC:
27+ return " ns" ;
28+ case DURATION_SEC:
29+ return " s" ;
30+ case EVENT:
31+ case EVENT_START:
32+ case EVENT_END:
33+ case UNSPECIFIED:
34+ default :
35+ return " " ;
3536 }
3637}
3738
@@ -40,7 +41,7 @@ void Stat::processReport(const StatReport& report) {
4041 if (m_compute & (Stat::SUM | Stat::AVERAGE)) {
4142 m_sum += report.value ;
4243 }
43- if ((m_compute & Stat::MAX)&& report.value > m_max) {
44+ if ((m_compute & Stat::MAX) && report.value > m_max) {
4445 m_max = report.value ;
4546 }
4647 if ((m_compute & Stat::MIN) && report.value < m_min) {
@@ -69,20 +70,41 @@ void Stat::processReport(const StatReport& report) {
6970 }
7071}
7172
72- QDebug operator <<(QDebug dbg, const Stat & stat) {
73+ QDebug operator <<(QDebug dbg, const Stat& stat) {
7374 QStringList stats;
75+
76+ auto formatTime = [](double ns) -> QString {
77+ using namespace std ::chrono;
78+
79+ // Converting input to integral nanoseconds for safe duration_cast
80+ auto nanosecondsVal = duration_cast<nanoseconds>(duration<double , std::nano>(ns));
81+
82+ if (nanosecondsVal >= seconds (1 )) {
83+ double sec = ns / 1e9 ;
84+ return QString::number (sec, ' f' , 2 ) + " s" ;
85+ } else if (nanosecondsVal >= milliseconds (1 )) {
86+ double ms = ns / 1e6 ;
87+ return QString::number (ms, ' f' , 2 ) + " ms" ;
88+ } else if (nanosecondsVal >= microseconds (1 )) {
89+ double us = ns / 1e3 ;
90+ return QString::number (us, ' f' , 2 ) + " µs" ;
91+ } else {
92+ return QString::number (ns, ' f' , 2 ) + " ns" ;
93+ }
94+ };
95+
7496 if (stat.m_compute & Stat::COUNT) {
7597 stats << " count=" + QString::number (stat.m_report_count );
7698 }
7799
78100 if (stat.m_compute & Stat::SUM) {
79- stats << " sum=" + QString::number (stat.m_sum ) + stat. valueUnits ( );
101+ stats << " sum=" + formatTime (stat.m_sum );
80102 }
81103
82104 if (stat.m_compute & Stat::AVERAGE) {
83105 QString value = " average=" ;
84106 if (stat.m_report_count > 0 ) {
85- value += QString::number (stat.m_sum / stat.m_report_count ) + stat. valueUnits ( );
107+ value += formatTime (stat.m_sum / stat.m_report_count );
86108 } else {
87109 value += " XXX" ;
88110 }
@@ -92,7 +114,7 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
92114 if (stat.m_compute & Stat::MIN) {
93115 QString value = " min=" ;
94116 if (stat.m_report_count > 0 ) {
95- value += QString::number (stat.m_min ) + stat. valueUnits ( );
117+ value += formatTime (stat.m_min );
96118 } else {
97119 value += " XXX" ;
98120 }
@@ -102,7 +124,7 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
102124 if (stat.m_compute & Stat::MAX) {
103125 QString value = " max=" ;
104126 if (stat.m_report_count > 0 ) {
105- value += QString::number (stat.m_max ) + stat. valueUnits ( );
127+ value += formatTime (stat.m_max );
106128 } else {
107129 value += " XXX" ;
108130 }
@@ -111,9 +133,9 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
111133
112134 if (stat.m_compute & Stat::SAMPLE_VARIANCE) {
113135 double variance = stat.variance ();
114- stats << " variance=" + QString::number (variance) + stat. valueUnits ( ) + " ^2" ;
136+ stats << " variance=" + formatTime (variance) + " ^2" ;
115137 if (variance >= 0.0 ) {
116- stats << " stddev=" + QString::number (sqrt (variance)) + stat. valueUnits ( );
138+ stats << " stddev=" + formatTime (sqrt (variance));
117139 }
118140 }
119141
@@ -123,10 +145,9 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
123145
124146 if (stat.m_compute & Stat::HISTOGRAM) {
125147 QStringList histogram;
126- for (auto it = stat.m_histogram .constBegin ();
127- it != stat.m_histogram .constEnd (); ++it) {
128- histogram << QString::number (it.key ()) + stat.valueUnits () + " :" +
129- QString::number (it.value ());
148+ for (auto it = stat.m_histogram .constBegin (); it != stat.m_histogram .constEnd (); ++it) {
149+ histogram << formatTime (static_cast <double >(it.key ())) + " :" +
150+ QString::number (it.value ());
130151 }
131152 stats << " histogram=" + histogram.join (" ," );
132153 }
@@ -137,9 +158,9 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
137158
138159// static
139160bool Stat::track (QString tag,
140- Stat::StatType type,
141- Stat::ComputeFlags compute,
142- double value) {
161+ Stat::StatType type,
162+ Stat::ComputeFlags compute,
163+ double value) {
143164 if (!StatsManager::s_bStatsManagerEnabled) {
144165 return false ;
145166 }
0 commit comments