升压 C++库:关于处理器使用百分比的单元测试断言
我正在使用 Boost.Unit 编写单元测试,并且在单元测试的一部分期间,我正在测试的代码不得超过单个 CPU 的 50%。我如何从源代码中做出这个断言?
I'm writing a unit test with Boost.Unit, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 times 调用 - 根据手册页:
NAME
times - 获取进程时间
概要
#include
描述
times() 将当前进程时间存储在 buf 指向的 tms 结构体中。结构体
tms 的定义如下:
Use the times call - according to the man page:
NAME
times - get process times
SYNOPSIS
#include <sys/times.h>
DESCRIPTION
times() stores the current process times in the struct tms that buf points to. The struct
tms is as defined in :
我会使用 GetProcessTimes 函数,如果您正在Windows环境中编程。该函数返回进程使用的内核时间和用户时间。您需要在需要开始时间之前调用它,然后在测试结束时调用它。
计算差值,看看它是否超过挂钟时间的 50%。
时间采用 FILETIME 格式。这种格式的目的是为了支持不同的程序支持不同的整数格式,因此请选择正确的格式。
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.