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.

Stress

For this lab, the EC2 instance will install a utility called stress. This tool is designed to subject your system to a configurable measure of CPU, memory, I/O and disk stress. This command we will use:

sudo stress --cpu 8 --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1
  • –cpu
    • This will spawn 8 CPU workers spinning on a square root task (sqrt(x))
  • –vm-bytes
    • This will use 90% of the available memory from /proc/meminfo
  • –vm-keep
    • This will re-dirty memory instead of freeing and reallocating.
  • -m 1
    • This will spawn 1 worker spinning on malloc()/free()

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=LinuxMachineDeploy
  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: LinuxMachineDeploy 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 Linux bash shell to run commands on the EC2 instance. GenerateLoad3 GenerateLoad4 GenerateLoad5
  5. Type sudo stress --cpu 8 --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 0.9;}' < /proc/meminfo)k --vm-keep -m 1 This will start to consume all of the available memory as well as all CPU’s within the instance GenerateLoad6
  6. Go back to your browser tab that contains the CloudWatch Dashboard. You should see the CPU and Memory graphs change within 10-15 seconds.
    • cpu_usage_user goes up as the test script consumes CPU
    • mem_used goes up as the script consumes all of it except for a small reserve and should level off right below mem_total GenerateLoad7
  7. As time goes on, it will continue to update the graph. In order to remove the load, press CTRL-C to stop the stress script.
  8. Go back to your browser tab that contains the CloudWatch Dashboard to watch as the CPU load goes down and the amount of free memory increases. GenerateLoad8