Skip to content

Instantly share code, notes, and snippets.

@pradeepn
Created April 26, 2020 19:31
Show Gist options
  • Save pradeepn/27f6fe5112994c6040ba8438c61c09dc to your computer and use it in GitHub Desktop.
Save pradeepn/27f6fe5112994c6040ba8438c61c09dc to your computer and use it in GitHub Desktop.
Gist to load CPU by 80%
///https://stackoverflow.com/questions/2514544/simulate-steady-cpu-load-and-spikes
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment