简体   繁体   English

C#将CPU保持在指定使用率附近/用于测试目的

[英]C# Keep CPU at/near designated percent usage for testing purposes

I am currently working on a little in-house testing app to create multiple types of load on the computer so that we can see how our software works under load. 我目前正在开发一个小型内部测试应用程序,以在计算机上创建多种类型的负载,以便我们可以看到我们的软件在负载下如何工作。 I am getting hung up on one request, in that I make some sort of way to specify a percent usage of the CPU (say, 40%) before the user runs the program, and that will hopefully keep the cpu in a range +-10% of what they put in. I've been told that they had seen programs doing it before, but every one that I had seen that claimed to do so did not work. 我正在挂接到一个请求,因为我采取了某种方式在用户运行程序之前指定CPU的使用百分比(例如40%),这有望使CPU保持在+-他们投入了10%的资金。有人告诉我他们以前见过这样做的程序,但是我见过的每个声称这样做的程序都行不通。 I am attempting to read an average usage and sleep the thread running so that the CPU usage lowers, but I have found that is not accurate. 我试图读取平均使用率并让线程处于休眠状态,以便降低CPU使用率,但是我发现这是不正确的。 It is also going to be used on multiple servers, each with different CPUs, so I can't do very much hard coding as it will need to work on each server. 它还将在多台服务器上使用,每台服务器具有不同的CPU,因此我不能做太多的硬编码,因为它将需要在每台服务器上工作。 I personally do not think its possible to accurately do, but I'm asking here as a last ditch effort to see if anyone else has come across software similar to this, and knows how it would be written. 我个人认为不可能准确地做到这一点,但我想在这里做最后的努力,看看是否有人遇到过与此类似的软件,并且知道如何编写。

Edit: I have attempted to use a lower priority thread, and was also told that would not be useful to them (though I had the option created just for the purpose of testing before this was requested of me). 编辑:我试图使用优先级较低的线程,并被告知这对他们没有用(尽管我有一个仅出于测试目的而创建的选项)。

We did something analogous a while back tuning an application server. 我们做了一些类似的事情来回调应用服务器。 We found that our overall throughput was best when the CPU was around 70%. 我们发现,当CPU约为70%时,我们的总体吞吐量是最好的。 Our solution was to spin up additional threads, with a delay between each one, until the desired load was reached. 我们的解决方案是增加每个线程之间的延迟,直到达到所需的负载为止。 If CPU went above 80%, we reduced the number of running threads (signaled a thread to finish its current work and then terminate). 如果CPU的速度超过80%,我们将减少正在运行的线程数(用信号通知一个线程以完成其当前工作,然后终止)。 If CPU dropped below 60%, we fired up a new thread. 如果CPU降到60%以下,我们将启动一个新线程。

You could apply similar logic to create artificial load that you can tune to a target CPU utilization. 您可以应用类似的逻辑来创建可以调整为目标CPU利用率的人工负载。 Your threads would need to balance CPU and non-CPU (IO, or just sleep) in order to allow for fine tuning. 您的线程需要平衡CPU和非CPU(IO或只是睡眠),以便进行微调。 If your threads are very CPU intensive, each will consume 1 CPU core to about 100%. 如果您的线程占用大量CPU,则每个线程将消耗1个CPU内核,约占100%。

从Windows 8开始,您可以使用Windows Jobs使OS Scheduler 限制CPU使用率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM