【发布时间】:2010-09-25 03:03:10
【问题描述】:
我正在使用 Boost.Unit 编写单元测试,并且我正在测试的代码在单元测试的一部分期间不得超过单个 CPU 的 50%。我怎样才能从源代码中做出这个断言?
【问题讨论】:
标签: c++ linux boost real-time cpu-usage
我正在使用 Boost.Unit 编写单元测试,并且我正在测试的代码在单元测试的一部分期间不得超过单个 CPU 的 50%。我怎样才能从源代码中做出这个断言?
【问题讨论】:
标签: c++ linux boost real-time cpu-usage
使用 times 调用 - 根据手册页:
名字 times - 获取处理时间
概要
#include
clock_t times(struct tms *buf);
描述 times() 将当前进程时间存储在 buf 指向的 struct tms 中。结构 tms 的定义如下:
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.
【讨论】:
如果您在 Windows 环境中编程,我会使用 GetProcessTimes 函数。此函数返回进程使用的内核时间和用户时间。您需要在开始时间之前调用它,然后在结束测试时调用它。
取差值,看看它是否超过挂钟时间的 50%。
时间采用 FILETIME 格式。此格式旨在支持程序支持不同的不同整数格式,因此请选择正确的格式。
【讨论】: