Created
April 26, 2020 19:31
-
-
Save pradeepn/27f6fe5112994c6040ba8438c61c09dc to your computer and use it in GitHub Desktop.
Gist to load CPU by 80%
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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