Generate CPU and Memory load

We have a CloudWatch dashboard to show us CPU and Memory statistics for the deployed EC2 instance. In order to showcase the dashboards, lets add a synthetic load to the machine. We have 2 PowerShell scripts that have already been deployed to the instance to facilitate this.

cpu_stress.ps1

This script will start multiple threads (one per CPU in the machine) to keep the processor busy doing a simple math computation. We set the thread priority to “Lowest” so it should still allow system processes to continue.

View cpu_stress.ps1 Code

mem_stress.ps1

This script will create an ever expanding array in RAM to attempt to consume as much as possible. We do reserve 512Mb of ram for the OS to continue to operate.

View mem_stress.ps1 Code

Generate Load

  1. Open a new tab for the AWS console with this link: https://console.aws.amazon.com/ec2/v2/home?r#Instances:instanceState=running;tag:Name=WindowsMachineDeploy

  2. You should see the EC2 instance we have deployed.

    • Troubleshooting: If you do not see the instance, and you changed the CloudFormation stack name when deploying, then delete the Name: WindowsMachineDeploy filter and search for the instance with the same name as you used for your stack GenerateLoad1
  3. Click the checkbox next to the machine, and then click “Connect” GenerateLoad2

  4. Select “Session Manager” and then click Connect. This will open a new tab with a PowerShell console for the instance. GenerateLoad3 GenerateLoad4

  5. Type C:\mem_stress.ps1 at the console and it will start to consume memory resources GenerateLoad5 GenerateLoad6

  6. Go back to the previous broswer tab that has the EC2 console connect screen and click Connect again. This will open another PowerShell console. GenerateLoad4

  7. Type C:\cpu_stress.ps1 at the console and it will start to consume CPU resources GenerateLoad7

  8. Go back to your browser tab that contains the CloudWatch Dashboard. You should see the CPU and Memory graphs change within 10-15 seconds.

    • Processor % User Time goes up as the test script consumes CPU
    • Memory Available goes down, as the script consumes all of it except for a small reserve GenerateLoad8
  9. As time goes on, it will continue to update the graph. In order to remove the load, go back to each of the console windows and simply press any key. This will cause the script to reclaim all resources it has consumed.

  10. Go back to your browser tab that contains the CloudWatch Dashboard to watch as the CPU load goes down and the amount of free RAM increases. GenerateLoad9