2014-09-04 48 views
0

我正在为异步应用程序开发自动化测试框架。 需要注意的一个方面是应用程序应答时间行为。 我想知道是否以及如何跟踪函数调用和相应事件回调函数的调用之间的时间。函数调用和回调之间的C++度量时间

例子:

我设置音频输入设备

virtual void setGain(unsigned int gain) = 0; 

这将导致衬垫库执行某些系统调用的输入增益和火灾的事件迟早

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

现在的目标是获取当setGain被调用的时间和onGainChanged被调用时的当前时间,以确定时间间隔。

这应该由外部组件(例如RuntimeObserver)完成,尽可能通用。

你有没有关于如何设计这样一个模块的线索?

预先感谢您

+0

是在同一设备上调用的'setGain'和'onGainChanged'吗?另外什么是目标平台(IOS,Win32等)? – Raxvan 2014-09-04 14:38:41

+0

是的,它们在同一个设备上被调用,并且可以确保,如果之前没有收到onGainChanged,则不能有两个或更多的setGain调用。目标平台是Win32以及Mac OSX的第一步,移动平台是重点,所以它应该是平台独立的... – Thomas 2014-09-04 14:41:14

+0

好吧,还有一个问题,你使用的是C++ 11还是C++ 98?另外什么是所需的定时器精度(毫秒/秒)? – Raxvan 2014-09-04 14:43:08

回答

0

如果一切是它归结为两个部分该系统在同一个平台上:1:时间测量和2:通用实现(更多的问题)

1时间测量:

这背后的概念是测量调用setGainonGainChanged时的时间。这两次测量之间的差异将等于您要测量的时间延迟。

下面是使用由类型和功能的一些基本的例子(会解释这些更高版本):

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

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

在Win32平台上TimeValue(和TimeDuration)可以unsigned intGetNowTime可以timeGetTime。这个结果将以毫秒为单位。

如果您正在寻找跨平台解决方案(在C++ 98中),最好选择一个库,例如boost::timerboost::chrono(您应该首先进行调查并首先检查可用的解决方案)。 理想的情况下,如果切换到C++ 11可以使用std::chrono(这里TimeValuestd::chrono::time_pointTimeDurationstd::chrono::duration

2:实施

有很多的方式,你可以在一个通用的方式实现这一我不认为有一个完美的解决方案。一种方式,我会做到这一点是这样的:

//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(); 
} 

用法:

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 

我写的代码了我的头,我希望我没有错过任何错误。

+0

这看起来相当不错!本周我会试一试=)到目前为止感谢。 – Thomas 2014-09-08 09:41:04

相关问题