瀏覽代碼

add print time

leon 1 月之前
父節點
當前提交
c7f6555273
共有 4 個文件被更改,包括 70 次插入0 次删除
  1. 65 0
      src/common/utils.hpp
  2. 1 0
      src/nodes/draw/drawNode.cpp
  3. 2 0
      src/nodes/infer/inferNode.cpp
  4. 2 0
      src/nodes/stream/streamNode.cpp

+ 65 - 0
src/common/utils.hpp

@@ -18,4 +18,69 @@ static std::string str_format(const std::string &format, Args ... args)
 	return std::string(buf.get(), buf.get() + size_buf - 1); 
 }
 
+#pragma once // 或者使用传统的 include guards
+
+#include <chrono>   // 用于时间测量
+#include <iostream> // 用于打印输出
+#include <string>   // 用于计时器名称
+#include <iomanip>  // 用于格式化输出 (std::fixed, std::setprecision)
+
+class Timer {
+public:
+    // 构造函数:记录开始时间,并可选地接收一个名称
+    Timer(const std::string& name = "Timer")
+        : m_name(name),
+          m_startTimePoint(std::chrono::high_resolution_clock::now()),
+          m_stopped(false) // 初始化为未停止状态
+    {
+        // 可以选择在这里打印开始信息,但通常只在结束时打印
+        // std::cout << "[" << m_name << "] Timer started...\n";
+    }
+
+    // 析构函数:计算并打印耗时
+    ~Timer() {
+        // 确保只打印一次,即使 stop() 被显式调用过
+        if (!m_stopped) {
+            stopAndPrint();
+        }
+    }
+
+    // 可选:提供一个显式停止并打印的方法,如果需要在对象销毁前获取时间
+    void stopAndPrint() {
+        if (m_stopped) { // 如果已经停止并打印过,则直接返回
+            return;
+        }
+
+        auto endTimePoint = std::chrono::high_resolution_clock::now();
+
+        // 计算持续时间
+        auto duration = endTimePoint - m_startTimePoint;
+
+        // 将持续时间转换为更易读的单位(例如:微秒、毫秒、秒)
+        long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
+        double milliseconds = microseconds / 1000.0;
+        double seconds = milliseconds / 1000.0;
+
+        // 打印结果
+        std::cout << "[" << m_name << "] Elapsed time: "
+                  << microseconds << " us | "
+                  // 使用 iomanip 设置精度,使输出更整齐
+                  << std::fixed << std::setprecision(3) << milliseconds << " ms | "
+                  << std::fixed << std::setprecision(6) << seconds << " s\n";
+
+        m_stopped = true; // 标记为已停止并打印
+    }
+
+    // 为了遵循 RAII 和防止意外行为,禁止拷贝和移动
+    Timer(const Timer&) = delete;
+    Timer& operator=(const Timer&) = delete;
+    Timer(Timer&&) = delete;
+    Timer& operator=(Timer&&) = delete;
+
+private:
+    std::string m_name;                                               // 计时器名称
+    std::chrono::time_point<std::chrono::high_resolution_clock> m_startTimePoint; // 开始时间点
+    bool m_stopped;                                                   // 标记是否已手动停止并打印
+};
+
 #endif

+ 1 - 0
src/nodes/draw/drawNode.cpp

@@ -67,6 +67,7 @@ void DrawNode::work()
         bool has_data = false;
         for (auto& input_buffer : input_buffers_)
         {
+            printf("DrawNode %s queue size : %d\n", name_.c_str(), input_buffer.second->size());
             std::shared_ptr<meta::MetaData> metaData;
             if (!input_buffer.second->try_pop(metaData))
             {

+ 2 - 0
src/nodes/infer/inferNode.cpp

@@ -1,6 +1,7 @@
 #include "nodes/base/base.hpp"
 #include "nodes/infer/inferNode.hpp"
 #include "common/image.hpp"
+#include "common/utils.hpp"
 #include <unordered_map>
 #include <random>
 #include <algorithm>
@@ -25,6 +26,7 @@ void InferNode::work()
     printf("InferNode %s\n", name_.c_str());
     while (running_)
     {
+        Timer timer("InferNode");
         bool has_data = false;
         for (auto& input_buffer : input_buffers_)
         {

+ 2 - 0
src/nodes/stream/streamNode.cpp

@@ -1,5 +1,6 @@
 #include "nodes/base/base.hpp"
 #include "nodes/stream/streamNode.hpp"
+#include "common/utils.hpp"
 
 namespace GNode
 {
@@ -72,6 +73,7 @@ void StreamNode::work_gpu()
         }
         int ndecoded_frame = decoder_->decode(packet_data, packet_size, pts);
         for(int i = 0; i < ndecoded_frame; ++i){
+            Timer timer("StreamNode");
             cv::Mat frame(decoder_->get_height(), decoder_->get_width(), CV_8UC3, decoder_->get_frame(&pts, &frame_index));
             frame_index = frame_index + 1;