Boost C++库:%处理器使用情况下的单元测试声明
问题描述:
我正在使用Boost.Unit编写单元测试,并且我正在测试的代码在部分单元测试期间不得超过单个CPU的50%。我怎么能从源代码中做出这个断言?Boost C++库:%处理器使用情况下的单元测试声明
答
使用的时代呼唤 - 根据手册页:
名称 时间 - 获取进程倍
概要 的#include <SYS/times.h>
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.
答
我会用GetProcessTimes函数,如果你在Windows环境下编程。该函数返回进程使用的内核和用户时间。在需要开始时间然后进行最终测试之前,您需要调用它。
看看它是否超过挂钟时间的50%。
时间为FILETIME格式。此格式旨在支持程序支持不同的整数格式,因此请选择正确的格式。
不幸的是我在linux上 – 2010-09-25 03:22:55