Occasionally when testing something I want to generate 100% CPU load on a Windows computer. There are several utilities out there to do this, but that implies you have the utility on hand and are comfortable running it on the server. A colleague of mine (thanks Mark S.) showed me this nifty trick of using calc.exe to generate 100% CPU. The best thing about this is that every standard Windows OS installation has calc.exe.
This post provides two methods of generating 100% CPU load, the original calc.exe method, and a simple one line PowerShell command to do the same thing from the command-line (locally or on a remote server with PS v1 and psexec). Note that there may be a better method with PowerShell, I simply scripted the same operation calc was performing.
On dual-CPU/core computers this uses 100% of one CPU/core. To use more than that, calc or the PowerShell command can be run more than once, and Windows by default will run the new process on another less-busy CPU/core.
One practical application of this is to load-test a VMware VI3 cluster, generating 100% CPU on one or more VMs to see how ESX and DRS/VC handles the load. I have also used this in the past when testing multi-threaded applications and processor affinity to see how Windows allocates a processor.
calc.exe
Use calc to calculate the factorial of a number - the product of all integers from 1 up to and including the number specified, eg 5! = 1x2x3x4x5
- Run calc.exe and switch to scientific mode
- Type a large number (eg. 12345678901234567890), press the 'n!' button.
- Calc will ask to confirm after warning this will take a very long time
- 100% CPU utilisation will now occur (essentially forever)
$result = 1; foreach ($number in 1..2147483647) {$result = $result * $number};
Depending on how fast the CPU is, this could finish, so a loop to run the command above 2 billion times:
foreach ($loopnumber in 1..2147483647) {$result=1;foreach ($number in 1..2147483647) {$result = $result * $number};$result}
If you want to see how long the command takes to run:
Measure-Command {$result = 1; foreach ($number in 1..2147483647) {$result = $result * $number}}
Using the command-line then provides the ability to run the command remotely. To use psexec to remotely execute powershell v1 factorial to generate 100% CPU:
psexec \\%computername% /s cmd /c "echo. | powershell $result = 1; foreach ($number in 1..2147483647) {$result = $result * $number}"
Wayne's World of IT (WWoIT), Copyright 2009 Wayne Martin.
8 comments:
Hi Wayne, David B (SSA) here. Just thought I'd let you know that it looks like M$ have put a little trap in calc for W2k8 (R2 at least) that stops you doing this. PS option still works a treat though.
This seems to work somewhat, but for some reason won't push either core of my CPU beyond 50%. I'm guessing there is some sort of load limiter which is restricting how much CPU powershell can use?
Hi John - regarding the 50% CPU. When I ran I found the powershell process is not Multi-threaded hence sticks to 1 vCPU (or CORE). If you saw 50% cpu then you'd probably see 100% CPU on 1 core and ~0% CPU on the other hence average of 50%.
Just though I'd share since I had same experience.
I got around the 'this doesn't peg my processor' by running the same command several times in different windows. Each instance increased CPU utilization by 3.4%.
Just in case anybody is interested in getting all CPU cores busy:
# specify the number of cores
ForEach ($cores in 1..4){
start-job -ScriptBlock{
$result = 1;
foreach ($loopnumber in 1..2147483647)
{
$result=1;
foreach ($number in 1..2147483647)
{
$result = $result * $number
}
$result
}
}
}
# toggle a breakpoint here to easyly stop jobs by pressing F5
Stop-Job *
interesting
best regards
Thomas from computer 22
Building on previous script, can utilize WMI to get the number of Cores
$NumberOfLogicalProcessors = Get-WmiObject win32_processor | Select-Object -ExpandProperty NumberOfLogicalProcessors
ForEach ($core in 1..$NumberOfLogicalProcessors){
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
Post a Comment