升压 C++库:关于处理器使用百分比的单元测试断言

发布于 2024-09-25 01:04:42 字数 85 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-10-02 01:04:42

使用 times 调用 - 根据手册页:

NAME
times - 获取进程时间

概要
#include

   clock_t times(struct tms *buf);

描述
times() 将当前进程时间存储在 buf 指向的 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.

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.
向日葵 2024-10-02 01:04:42

我会使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文