开发者

Boost C++ Libraries: Unit Test Assertion on % Processor Usage

I'm writing a unit test with Boost.Unit开发者_Go百科, and the code I'm testing must not exceed 50% of a single CPU during a portion of the unit test. How could I make this assertion from within the source code?


Use the times call - according to the man page:

NAME times - get process times

SYNOPSIS #include <sys/times.h>

   clock_t times(struct tms *buf);

DESCRIPTION times() stores the current process times in the struct tms that buf points to. The struct tms is as defined in :

       struct tms {
           clock_t tms_utime;  /* user time */
           clock_t tms_stime;  /* system time */
           clock_t tms_cutime; /* user time of children */
           clock_t tms_cstime; /* system time of children */
       };

   The tms_utime field contains the CPU  time  spent  executing  instructions  of  the  calling
   process.   The  tms_stime  field  contains  the CPU time spent in the system while executing
   tasks on behalf of the calling process.  The  tms_cutime  field  contains  the  sum  of  the
   tms_utime  and  tms_cutime  values  for  all waited-for terminated children.  The tms_cstime
   field contains the sum of the tms_stime and tms_cstime values for all waited-for  terminated
   children.


I would use GetProcessTimes Function if you are programming in a Windows enviorment. This function returns the kernal and user time used by the process. You will need call it before you need to start time and then at the end test.

Take the difference and see if it exceeds 50% of the wall clock time.

The times are in FILETIME format. This format is designed to support programs support different different integer formats, so choose the correct format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜