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:
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:
Get the number of CPU cores
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.
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
- Open your AWS console
- Select the Services drop down menu
- Find and click on CloudWatch
- Select Alarms
- Click the “Create Alarm” button
- Click the “Select Metric” button
- Click the “EC2” link
- Click “Per-instance Metrics” link
- Find and select the “CPU Utilization” row for the desired instance.
- Click the “Select Metric” button. This will then take you to the alarm configuration menus.
- 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:
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.