简体   繁体   English

C ++测量函数调用和回调之间的时间

[英]C++ measure time between function call and callback

I am working on an automated test framework for asynchronous applications. 我正在为异步应用程序开发自动测试框架。 One of the aspects to abserve is the applications answering time behavior. 需要注意的方面之一是应用程序响应时间行为。 And I wonder if and how it would be possible to track the time between a function call, and the call of the corresponding event callback function. 而且我想知道是否以及如何跟踪函数调用与相应事件回调函数的调用之间的时间。

Example: 例:

I am setting the input gain of an audio input device 我正在设置音频输入设备的输入增益

virtual void setGain(unsigned int gain) = 0; 

This causes the underlaying library to perform some system calls, and the fire an event sooner or later 这会导致底层库执行一些系统调用,并且迟早会触发事件

virtual void onGainChanged(IRtcAudioRecordingDevicePtr device, unsigned int gain) = 0;

The aim is now, to get the current time when setGain is calles, and when onGainChanged is called, to determine the timespan. 现在的目标是获取调用setGain时和调用onGainChanged时的当前时间,以确定时间跨度。

This should be done by an external component (eg a RuntimeObserver), as generic as possible. 应该由尽可能通用的外部组件(例如RuntimeObserver)完成。

Do you have any clues on how to design such a module? 您对如何设计这样的模块有任何线索吗?

Thank you in advance 先感谢您

If everything is on the same platform it boils down to two parts for this system: 1:Time measurement and 2:Generic Implementation(more problematic) 如果所有内容都在同一平台上,则该系统可分为两部分:1:时间测量和2:通用实现(存在更多问题)

1 Time Measurement: 1时间测量:

The concept behind this is to measure the time when setGain and onGainChanged are called. 其背后的概念是测量setGainonGainChanged被调用的时间。 The difference between those two time measurement will be equal to time delay you want to measure. 这两个时间测量之间的差将等于您要测量的时间延迟。

Here is some basic example using made up types and functions (will explain those later): 这是一些使用组合类型和函数的基本示例(稍后将进行解释):

TimeValue s = GetNowTime();
setGain(...);

//after some computations:
TimeValue e = GetNowTime();
onGainChanged(...);
TimeDuration d = (e - s);
cout << "Lag:" << d;

On win32 platform TimeValue (and TimeDuration ) can be unsigned int and GetNowTime can be timeGetTime . 在win32平台上,可以将TimeValue (和TimeDuration )设置为unsigned intGetNowTime可以设置为timeGetTime The result of this will be in miliseconds. 其结果将以毫秒为单位。

If you are looking for a cross platform solution (in c++98) it would be best to choose a library such as boost::timer or boost::chrono (you should investigate first and check the available solutions first). 如果您正在寻找跨平台解决方案(在c ++ 98中),则最好选择诸如boost::timerboost::chrono (您应该先研究并首先检查可用的解决方案)。 Ideally, if you switch to c++ 11 you can use std::chrono (here TimeValue is std::chrono::time_point and TimeDuration is std::chrono::duration ) 理想情况下,如果切换到c ++ 11,则可以使用std::chrono (这里的TimeValuestd::chrono::time_pointTimeDurationstd::chrono::duration TimeDuration std::chrono::duration

2: Implementation 2:实施

There are a lot of ways you can implement this in a generic way and i don't think there is one perfect solution. 您可以通过多种方式以通用方式实现此功能,但我认为没有完美的解决方案。 One way i would do it is like this: 我会这样做的一种方式是这样的:

//some header
enum Events
{
    kGainEvent  
};

#ifdef USE_EVENT_TRACKING

    class EventTimeHandler
    {
    public:
        using Duration = std::chrono::high_resolution_clock::duration;
        using TimePoint = std::chrono::high_resolution_clock::time_point;

        static EventTimeHandler* Instance;
        EventTimeHandler();

        void TrackStartEvent(Events e);
        void TrackEndEvent(Events e);

    private:
        std::map<Events,TimePoint> m_times;
    };



    inline void TrackStartEvent(Events e)
    {
        EventTimeHandler::Instance->TrackStartEvent(e);
    }
    inline void TrackEndEvent(Events e)
    {
        EventTimeHandler::Instance->TrackEndEvent(e);
    }

#else

    inline void TrackStartEvent(Events e){}//empty implementaiton
    inline void TrackEndEvent(Events e){}//empty implementaiton

#endif

//cpp file:
EventTimeHandler::EventTimeHandler()
{
    Instance = this;
}
void EventTimeHandler::TrackStartEvent(Events e)
{
    m_times[e] = std::chrono::high_resolution_clock::now();
}
void EventTimeHandler::TrackEndEvent(Events e)
{
    TimePoint now_time = std::chrono::high_resolution_clock::now();
    Duration d = now_time - m_times[e];//should check here if m_times has key e for safety ?

    //print time in milseconds as double
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds<double>>(d).count();
}

Usage: 用法:

TrackStartEvent(kGainEvent);
setGain(...);

//after some computations:
TrackEndEvent(kGainEvent);
onGainChanged(...);

//code will do nothing if USE_EVENT_TRACKING is not defined. This will allow to turn the system on and off

I wrote the code out of my head , i hope i did not miss any errors. 我从脑海中写下了代码,希望我不会错过任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM