22
33#include < memory/encoders/json.h>
44#include < utils/utils.h>
5+ #include < algorithm>
56
67void ResourceMonitor::Enable ()
78{
@@ -74,6 +75,27 @@ std::string ResourceMonitor::GenerateJSONPerformance(std::string plugin_id)
7475 if (plugin_id == " " ) {
7576 for (auto it = timings.begin (); it != timings.end (); ++it) {
7677 for (auto it2 = it->second .begin (); it2 != it->second .end (); ++it2) {
78+
79+ auto [min, max] = std::minmax_element (it2->second .begin (), it2->second .end ());
80+
81+ float avg = 0 ;
82+ uint64_t avgCount = 0 ;
83+ for (std::vector<float >::iterator ii = it2->second .begin (); ii != it2->second .end (); ++ii)
84+ {
85+ avg += *(ii);
86+ ++avgCount;
87+ }
88+
89+ avg /= (float )(avgCount);
90+
91+ std::string event_name = string_format (" %s [%s] (min=%s, avg=%s, max=%s, calls=%d)" ,
92+ it2->first .c_str (), it->first .c_str (),
93+ (*min) < 0.5 ? string_format (" %d.00μs" , (uint64_t )((*min) * 1000 .0f )).c_str () : string_format (" %.2fms" , *min).c_str (),
94+ avg < 0.5 ? string_format (" %d.00μs" , (uint64_t )(avg * 1000 .0f )).c_str () : string_format (" %.2fms" , avg).c_str (),
95+ (*max) < 0.5 ? string_format (" %d.00μs" , (uint64_t )((*max) * 1000 .0f )).c_str () : string_format (" %.2fms" , *max).c_str (),
96+ it2->second .size ()
97+ );
98+
7799 for (float time : it2->second ) {
78100 uint64_t uint_time = (uint64_t )(time * 1000 .0f );
79101
@@ -82,8 +104,6 @@ std::string ResourceMonitor::GenerateJSONPerformance(std::string plugin_id)
82104
83105 t++;
84106
85- std::string event_name = string_format (" %s [%s]" , it2->first .c_str (), it->first .c_str ());
86-
87107 val.AddMember (" name" , rapidjson::Value ().SetString (event_name.c_str (), doc.GetAllocator ()), doc.GetAllocator ());
88108 val.AddMember (" ph" , rapidjson::Value ().SetString (" B" , doc.GetAllocator ()), doc.GetAllocator ());
89109 val.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
@@ -107,6 +127,26 @@ std::string ResourceMonitor::GenerateJSONPerformance(std::string plugin_id)
107127 }
108128 else {
109129 for (auto it2 = timings[plugin_id].begin (); it2 != timings[plugin_id].end (); ++it2) {
130+ auto [min, max] = std::minmax_element (it2->second .begin (), it2->second .end ());
131+
132+ float avg = 0 ;
133+ uint64_t avgCount = 0 ;
134+ for (std::vector<float >::iterator ii = it2->second .begin (); ii != it2->second .end (); ++ii)
135+ {
136+ avg += *(ii);
137+ ++avgCount;
138+ }
139+
140+ avg /= (float )(avgCount);
141+
142+ std::string event_name = string_format (" %s (min=%s, avg=%s, max=%s, calls=%d)" ,
143+ it2->first .c_str (),
144+ (*min) < 0.5 ? string_format (" %d.00μs" , (uint64_t )((*min) * 1000 .0f )).c_str () : string_format (" %.2fms" , *min).c_str (),
145+ avg < 0.5 ? string_format (" %d.00μs" , (uint64_t )(avg * 1000 .0f )).c_str () : string_format (" %.2fms" , avg).c_str (),
146+ (*max) < 0.5 ? string_format (" %d.00μs" , (uint64_t )((*max) * 1000 .0f )).c_str () : string_format (" %.2fms" , *max).c_str (),
147+ it2->second .size ()
148+ );
149+
110150 for (float time : it2->second ) {
111151 uint64_t uint_time = (uint64_t )(time * 1000 .0f );
112152
@@ -115,7 +155,7 @@ std::string ResourceMonitor::GenerateJSONPerformance(std::string plugin_id)
115155
116156 t++;
117157
118- val.AddMember (" name" , rapidjson::Value ().SetString (it2-> first .c_str (), doc.GetAllocator ()), doc.GetAllocator ());
158+ val.AddMember (" name" , rapidjson::Value ().SetString (event_name .c_str (), doc.GetAllocator ()), doc.GetAllocator ());
119159 val.AddMember (" ph" , rapidjson::Value ().SetString (" B" , doc.GetAllocator ()), doc.GetAllocator ());
120160 val.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
121161 val.AddMember (" pid" , rapidjson::Value ().SetInt (1 ), doc.GetAllocator ());
@@ -125,7 +165,7 @@ std::string ResourceMonitor::GenerateJSONPerformance(std::string plugin_id)
125165
126166 t += uint_time;
127167
128- val2.AddMember (" name" , rapidjson::Value ().SetString (it2-> first .c_str (), doc.GetAllocator ()), doc.GetAllocator ());
168+ val2.AddMember (" name" , rapidjson::Value ().SetString (event_name .c_str (), doc.GetAllocator ()), doc.GetAllocator ());
129169 val2.AddMember (" ph" , rapidjson::Value ().SetString (" E" , doc.GetAllocator ()), doc.GetAllocator ());
130170 val2.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
131171 val2.AddMember (" pid" , rapidjson::Value ().SetInt (1 ), doc.GetAllocator ());
0 commit comments