C# 模拟稳定的CPU负载和峰值
如何在C#中生成稳定的CPU负载,在特定时间内低于100%?我也希望能够改变负载量后,一段时间。您建议如何在很短的时间内产生使用率峰值?首先,您必须了解CPU使用率始终是特定时间内的平均值。在任何给定的时间,CPU要么工作,要么不工作。CPU从来没有40%工作 然而,我们可以通过让CPU工作0.4秒和睡眠0.6秒来模拟40%的负载,比如说一秒钟。这使得该秒的平均利用率达到40% 将其压缩到小于1秒,比如说100毫秒的数据块应该可以提供更稳定的利用率 以下方法将采用期望利用率的参数,然后将单个CPU/核心利用到该程度:C# 模拟稳定的CPU负载和峰值,c#,cpu-usage,C#,Cpu Usage,如何在C#中生成稳定的CPU负载,在特定时间内低于100%?我也希望能够改变负载量后,一段时间。您建议如何在很短的时间内产生使用率峰值?首先,您必须了解CPU使用率始终是特定时间内的平均值。在任何给定的时间,CPU要么工作,要么不工作。CPU从来没有40%工作 然而,我们可以通过让CPU工作0.4秒和睡眠0.6秒来模拟40%的负载,比如说一秒钟。这使得该秒的平均利用率达到40% 将其压缩到小于1秒,比如说100毫秒的数据块应该可以提供更稳定的利用率 以下方法将采用期望利用率的参数,然后将单个CP
public static void ConsumeCPU(int percentage)
{
if (percentage < 0 || percentage > 100)
throw new ArgumentException("percentage");
Stopwatch watch = new Stopwatch();
watch.Start();
while (true)
{
// Make the loop go on for "percentage" milliseconds then sleep the
// remaining percentage milliseconds. So 40% utilization means work 40ms and sleep 60ms
if (watch.ElapsedMilliseconds > percentage)
{
Thread.Sleep(100 - percentage);
watch.Reset();
watch.Start();
}
}
}
publicstaticvoidconsumercpu(整数百分比)
{
如果(百分比<0 | |百分比>100)
抛出新的ArgumentException(“百分比”);
秒表=新秒表();
watch.Start();
while(true)
{
//使循环持续“百分比”毫秒,然后睡眠
//剩余百分比毫秒。所以40%的利用率意味着工作40毫秒,睡眠60毫秒
如果(watch.elapsedmillesons>百分比)
{
睡眠(100%);
watch.Reset();
watch.Start();
}
}
}
我在这里使用a是因为它比属性更精确,但是你也可以使用它,并使用减法来检查你是否跑得足够长
要记住两件事:
- 在多核系统上,您必须为每个核心生成一个线程。否则,您将只看到一个CPU/内核被使用,从而获得大致“内核百分比/数量”的利用率
- 线程。睡眠不是很准确。它永远不能保证时间精确到毫秒,因此您将看到结果中的一些变化
为了回答您的第二个问题,关于在一段时间后更改利用率,我建议您在一个或多个线程上运行此方法(取决于内核的数量),然后当您想要更改利用率时,您只需停止这些线程并生成具有新百分比值的新线程。这样,您就不必实现线程通信来更改运行线程的百分比。除了Isak响应之外,我在这里介绍了一个多核的简单实现:
public static void CPUKill(object cpuUsage)
{
Parallel.For(0, 1, new Action<int>((int i) =>
{
Stopwatch watch = new Stopwatch();
watch.Start();
while (true)
{
if (watch.ElapsedMilliseconds > (int)cpuUsage)
{
Thread.Sleep(100 - (int)cpuUsage);
watch.Reset();
watch.Start();
}
}
}));
}
static void Main(string[] args)
{
int cpuUsage = 50;
int time = 10000;
List<Thread> threads = new List<Thread>();
for (int i = 0; i < Environment.ProcessorCount; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(CPUKill));
t.Start(cpuUsage);
threads.Add(t);
}
Thread.Sleep(time);
foreach (var t in threads)
{
t.Abort();
}
}
公共静态无效CPUKill(对象cpuUsage)
{
平行。对于(0,1,新动作((int i)=>
{
秒表=新秒表();
watch.Start();
while(true)
{
如果(watch.elapsedmillyses>(int)cpuUsage)
{
线程睡眠(100-(int)cpuUsage);
watch.Reset();
watch.Start();
}
}
}));
}
静态void Main(字符串[]参数)
{
int-cpuUsage=50;
整数时间=10000;
列表线程=新列表();
for(int i=0;i
每次必须按变量设置cpuUsageIncreaseby时。
例如:
1-Cpu%增加>CPUUSAGE增加%,持续一分钟。
2-下降到0%,持续20秒。
3-转到步骤1
private void test()
{
int cpuUsageIncreaseby = 10;
while (true)
{
for (int i = 0; i < 4; i++)
{
//Console.WriteLine("am running ");
//DateTime start = DateTime.Now;
int cpuUsage = cpuUsageIncreaseby;
int time = 60000; // duration for cpu must increase for process...
List<Thread> threads = new List<Thread>();
for (int j = 0; j < Environment.ProcessorCount; j++)
{
Thread t = new Thread(new ParameterizedThreadStart(CPUKill));
t.Start(cpuUsage);
threads.Add(t);
}
Thread.Sleep(time);
foreach (var t in threads)
{
t.Abort();
}
//DateTime end = DateTime.Now;
//TimeSpan span = end.Subtract(start);
//Console.WriteLine("Time Difference (seconds): " + span.Seconds);
//Console.WriteLine("10 sec wait... for another.");
cpuUsageIncreaseby = cpuUsageIncreaseby + 10;
System.Threading.Thread.Sleep(20000);
}
}
}
private void test()
{
int cpuUsageIncreaseby=10;
while(true)
{
对于(int i=0;i<4;i++)
{
//控制台。WriteLine(“正在运行”);
//DateTime start=DateTime.Now;
int cpuUsage=cpuUsage递增单位;
int time=60000;//进程的cpu持续时间必须增加。。。
列表线程=新列表();
对于(int j=0;j
对于统一的压力:伊萨克·萨沃的回答稍加调整
int percentage = 80;
for (int i = 0; i < Environment.ProcessorCount; i++)
{
(new Thread(() =>
{
Stopwatch watch = new Stopwatch();
watch.Start();
while (true)
{
// Make the loop go on for "percentage" milliseconds then sleep the
// remaining percentage milliseconds. So 40% utilization means work 40ms and sleep 60ms
if (watch.ElapsedMilliseconds > percentage)
{
Thread.Sleep(100 - percentage);
watch.Reset();
watch.Start();
}
}
})).Start();
}
int百分比=80;
for(int i=0;i
{
秒表=新秒表();
watch.Start();
while(true)
{
//使循环持续“百分比”毫秒,然后睡眠
//剩余百分比毫秒。所以40%的利用率意味着工作40毫秒,睡眠60毫秒
如果(watch.elapsedmillesons>百分比)
{
睡眠(100%);
watch.Reset();
watch.Start();
}
}
})).Start();
}
我使用了你的函数,它几乎成功了。8个内核中有7个处于所需级别,然后我将“for(int i=0;i