Contents

Max out CPU on Linux

There are times when you may have a need to test server performance when investigating an issue or doing a predictive analysis. I learned an easy way to do this on Linux using the built-in ‘yes’ command.

From the man page for the ‘yes’ command:

1
2
NAME
    yes - output a string repeatedly until killed

This does exactly as it says and will consume the CPU unless it is controlled or killed. Here’s the full process:

  1. Get the number of CPU cores

    cat /proc/cpuinfo

  2. Use the ‘Yes’ command to run a continuous command in the background. Repeat it for the number of CPUs on the server.

    yes > /dev/null &

    The CPU(s) should be at 100% in just a couple of seconds.

  3. Enter ‘killall yes’ to end it

In the world of AWS, this is useful for testing CloudWatch CPU alarms. For AWS documentation on CloudWatch alarms see Using Amazon CloudWatch Alarms and Creating an Alarm from a Metric on a Graph. I will create a post on creating a CloudWatch alarm at some point, but for now here are the high level steps.

To create an EC2 CPU alarm

  1. Open your AWS console
  2. Select the Services drop down menu
  3. Find and click on CloudWatch
  4. Select Alarms
  5. Click the “Create Alarm” button
  6. Click the “Select Metric” button
  7. Click the “EC2” link
  8. Click “Per-instance Metrics” link
  9. Find and select the “CPU Utilization” row for the desired instance.
  10. Click the “Select Metric” button. This will then take you to the alarm configuration menus.
  11. Performance alarms are very unique, so your configuration will vary from server to server and app to app, but the screen should look something like this now:
CloudWatch EC2 CPU Alarm

From here you can follow the screens to configure your alarm triggers, actions, and notifications. If everything works you should be able to run the ‘yes’ commands shown above and trigger your alarm condition.

Bonus: Disk Space Usage

For disk space alarms, you can use the fallocate or dd commands to fill disk space.

fallocate -l 50G big_file