I'm trying to make a CPU load generator with c#.
If I set a certain amount of CPU usage like 20%, then it makes CPU to
run at a certain percentage.
See code below.
Arne
==================================
using System;
using System.Diagnostics;
using System.Threading;
namespace E
{
public class Program
{
private const double LOAD = 0.25;
public static void Main(string[] args)
{
DateTime startwall = DateTime.Now;
double startcpu =
Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
int t = 100;
while(true)
{
double x = 2;
for(int i = 0; i < 1000000; i++)
{
x = Math.Pow(Math.Sqrt(x), 2);
}
DateTime currwall = DateTime.Now;
double deltawall = (currwall -
startwall).TotalMilliseconds;
double currcpu =
Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
double deltacpu = currcpu - startcpu;
double usage = deltacpu / deltawall;
t = (int)(t * usage/LOAD + 0.5);
Thread.Sleep(t);
startwall = currwall;
startcpu = currcpu;
}
}
}
}