English 中文(简体)
如何一贯占领80%的《刑事诉讼法》?
原标题:How to occupy 80% CPU consistently?

I m looking for a way to occupy exactly 80% (or any other number) of a single CPU in a consistent manner.
I need this for some unit test that tests a component that triggers under specific CPU utilization conditions
For this purpose I can assume that the machine is otherwise idle.

What s a robust and possibly OS independent way to do this?

最佳回答

没有任何东西,比如占用了80%的时间。 万国邮联经常被使用或闲置。 在一段时间内,你的平均使用率可以达到80%。 是否有一个具体的时间期限,你是否希望这一期限得到平均? 这种假体编码应当跨平台运行,而超过1个平台使用率达到80%:

while True:
    startTime = time.now()
    while date.now() - startTime < 0.8:
        Math.factorial(100) // Or any other computation here
    time.sleep(0.2)
问题回答

Its pretty easy to write a program that alternately spins and sleeps to get any particular load level you want. I threw this together in a couple of minutes:

#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#define INTERVAL    500000
volatile sig_atomic_t   flag;
void setflag(int sig) { flag = 1; }

int main(int ac, char **av) {
    int load = 80;
    struct sigaction sigact;
    struct itimerval interval = { { 0, INTERVAL }, { 0, INTERVAL } };
    struct timespec pausetime = { 0, 0 };
    memset(&sigact, 0, sizeof(sigact));
    sigact.sa_handler = setflag;
    sigaction(SIGALRM, &sigact, 0);
    setitimer(ITIMER_REAL, &interval, 0);
    if (ac == 2) load = atoi(av[1]);
    pausetime.tv_nsec = INTERVAL*(100 - load)*10;
    while (1) {
        flag = 0;
        nanosleep(&pausetime, 0);
        while (!flag) { /* spin */ } }
    return 0;
}

The trick is that if you want to occupy 80% of CPU, keep the processor busy for 0.8 seconds (or 80% of any time interval. Here I take it to be 1 second), then let it sleep for 0.2 seconds, though it is advisable not to utilize the CPU too much, or all your processes will start running slow. You could try around 20% or so. Here is an example done in Python:

import time
import math
time_of_run = 0.1
percent_cpu = 80 # Should ideally replace this with a smaller number e.g. 20
cpu_time_utilisation = float(percent_cpu)/100
on_time = time_of_run * cpu_time_utilisation
off_time = time_of_run * (1-cpu_time_utilisation)
while True:
    start_time = time.clock()
    while time.clock() - start_time < on_time:
        math.factorial(100) #Do any computation here
    time.sleep(off_time)




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签