11#include "imgui/imgui.h"
26 "Initialized performance integration for canvas: %s",
37 LOG_DEBUG(
"CanvasPerformance",
"Started performance monitoring for canvas: %s",
60 LOG_DEBUG(
"CanvasPerformance",
"Stopped performance monitoring for canvas: %s",
83 static auto last_save = std::chrono::steady_clock::now();
84 auto now = std::chrono::steady_clock::now();
85 if (std::chrono::duration_cast<std::chrono::seconds>(now - last_save).count() >= 5) {
127 size_t bitmap_memory,
128 size_t palette_memory) {
143 std::ostringstream summary;
145 summary <<
"Canvas Performance Summary (" <<
canvas_id_ <<
")\n";
146 summary <<
"=====================================\n\n";
148 summary <<
"Timing Metrics:\n";
154 summary <<
"Operation Counts:\n";
160 summary <<
"Canvas Operations:\n";
167 summary <<
"Memory Usage:\n";
172 summary <<
"Cache Performance:\n";
173 summary <<
" Hit Ratio: " << std::fixed << std::setprecision(1)
178 return summary.str();
182 std::vector<std::string> recommendations;
186 recommendations.push_back(
"Frame time is high - consider reducing draw calls or optimizing rendering");
191 recommendations.push_back(
"Draw time is high - consider using texture atlases or reducing texture switches");
198 if (total_memory > 100) {
199 recommendations.push_back(
"Memory usage is high - consider implementing texture streaming or compression");
204 recommendations.push_back(
"Cache hit ratio is low - consider increasing cache size or improving cache strategy");
209 recommendations.push_back(
"High draw call count - consider batching operations or using instanced rendering");
213 recommendations.push_back(
"Frequent texture updates - consider using texture arrays or atlases");
216 return recommendations;
220 std::ostringstream report;
222 report <<
"Canvas Performance Report\n";
223 report <<
"========================\n\n";
225 report <<
"Canvas ID: " <<
canvas_id_ <<
"\n";
232 report <<
"Performance History:\n";
233 report <<
"===================\n\n";
237 report <<
"Sample " << (i + 1) <<
":\n";
238 report <<
" Frame Time: " <<
FormatTime(metrics.frame_time_ms) <<
"\n";
239 report <<
" Draw Calls: " << metrics.draw_calls <<
"\n";
240 report <<
" Memory: " <<
FormatMemory((metrics.texture_memory_mb +
241 metrics.bitmap_memory_mb +
242 metrics.palette_memory_mb) * 1024 * 1024) <<
"\n\n";
248 if (!recommendations.empty()) {
249 report <<
"Recommendations:\n";
250 report <<
"===============\n\n";
251 for (
const auto& rec : recommendations) {
252 report <<
"• " << rec <<
"\n";
278 if (ImGui::Button(
"Toggle Detailed Metrics")) {
282 if (ImGui::Button(
"Toggle Recommendations")) {
286 if (ImGui::Button(
"Export Report")) {
328 if (total_requests > 0) {
349 double frame_time_trend = 0.0;
350 double memory_trend = 0.0;
356 frame_time_trend += (curr.frame_time_ms - prev.frame_time_ms);
357 memory_trend += ((curr.texture_memory_mb + curr.bitmap_memory_mb + curr.palette_memory_mb) -
358 (prev.texture_memory_mb + prev.bitmap_memory_mb + prev.palette_memory_mb));
365 if (std::abs(frame_time_trend) > 1.0) {
366 LOG_DEBUG(
"CanvasPerformance",
"Canvas %s: Frame time trend: %.2f ms/sample",
370 if (std::abs(memory_trend) > 1.0) {
371 LOG_DEBUG(
"CanvasPerformance",
"Canvas %s: Memory trend: %.2f MB/sample",
377 ImGui::Text(
"Performance Overview");
393 ImGui::TextColored(memory_color,
"Memory: %s",
FormatMemory(total_memory * 1024 * 1024).c_str());
401 ImGui::Text(
"Detailed Metrics");
415 if (ImGui::CollapsingHeader(
"Memory Usage")) {
423 ImGui::Text(
"Total Memory: %s",
FormatMemory(total * 1024 * 1024).c_str());
428 if (ImGui::CollapsingHeader(
"Operation Counts")) {
435 ImGui::Text(
"Canvas Operations:");
445 if (ImGui::CollapsingHeader(
"Cache Performance")) {
456 ImGui::Text(
"Performance Recommendations");
460 if (recommendations.empty()) {
461 ImGui::TextColored(ImVec4(0.2F, 1.0F, 0.2F, 1.0F),
"✓ Performance looks good!");
463 for (
const auto& rec : recommendations) {
464 ImGui::TextColored(ImVec4(1.0F, 0.8F, 0.2F, 1.0F),
"⚠ %s", rec.c_str());
470 if (ImGui::CollapsingHeader(
"Performance Graph")) {
472 static std::vector<float> frame_times;
473 static std::vector<float> draw_times;
480 if (frame_times.size() > 100) {
481 frame_times.erase(frame_times.begin());
482 draw_times.erase(draw_times.begin());
485 if (!frame_times.empty()) {
486 ImGui::PlotLines(
"Frame Time (ms)", frame_times.data(),
487 static_cast<int>(frame_times.size()), 0,
nullptr, 0.0F, 50.0F,
489 ImGui::PlotLines(
"Draw Time (ms)", draw_times.data(),
490 static_cast<int>(draw_times.size()), 0,
nullptr, 0.0F, 30.0F,
498 return std::to_string(
static_cast<int>(time_ms * 1000)) +
" μs";
499 }
else if (time_ms < 1000.0) {
500 return std::to_string(
static_cast<int>(time_ms * 10) / 10.0) +
" ms";
502 return std::to_string(
static_cast<int>(time_ms / 1000)) +
" s";
508 return std::to_string(bytes) +
" B";
509 }
else if (bytes < 1024 * 1024) {
510 return std::to_string(bytes / 1024) +
" KB";
512 return std::to_string(bytes / (1024 * 1024)) +
" MB";
517 double threshold_good,
518 double threshold_warning)
const {
519 if (value <= threshold_good) {
520 return ImVec4(0.2F, 1.0F, 0.2F, 1.0F);
521 }
else if (value <= threshold_warning) {
522 return ImVec4(1.0F, 1.0F, 0.2F, 1.0F);
524 return ImVec4(1.0F, 0.2F, 0.2F, 1.0F);
536 const std::string& canvas_id,
537 std::shared_ptr<CanvasPerformanceIntegration> integration) {
540 "Registered performance integration for canvas: %s",
544std::shared_ptr<CanvasPerformanceIntegration>
555 integration->UpdateMetrics();
560 std::ostringstream summary;
562 summary <<
"Global Canvas Performance Summary\n";
563 summary <<
"=================================\n\n";
565 summary <<
"Registered Canvases: " <<
integrations_.size() <<
"\n\n";
568 summary <<
"Canvas: " <<
id <<
"\n";
569 summary <<
"----------------------------------------\n";
570 summary << integration->GetPerformanceSummary() <<
"\n\n";
573 return summary.str();
577 std::ostringstream report;
579 report <<
"Global Canvas Performance Report\n";
580 report <<
"================================\n\n";
585 report <<
"Global Recommendations:\n";
586 report <<
"=======================\n\n";
589 auto recommendations = integration->GetPerformanceRecommendations();
590 if (!recommendations.empty()) {
591 report <<
"Canvas " <<
id <<
":\n";
592 for (
const auto& rec : recommendations) {
593 report <<
" • " << rec <<
"\n";
604 integration->StopMonitoring();
607 LOG_DEBUG(
"CanvasPerformance",
"Cleared all canvas performance integrations");
#define LOG_DEBUG(category, format,...)
CanvasUsage
Canvas usage patterns and tracking.
Main namespace for the application.