JustGeek.dev Tech, simplified.

Stress Command Examples

Comprehensive Guide to Stress Utility: Performance Testing and Load Generation

Overview

The Stress utility is a powerful workload generator tool designed to simulate high-load conditions on Linux servers. It allows system administrators and developers to test system performance, stability, and resource management under various stress scenarios.

Installation

Centos/RHEL

# Enable EPEL repository (for Amazon Linux)
$ sudo yum install amazon-linux-extras install epel -y

# Install Stress
$ sudo yum install stress -y

Ubuntu/Debian

$ sudo apt-get update
$ sudo apt-get install stress -y

Basic Stress Command Examples

1. CPU Stress

Generate load on multiple CPU cores:

# Spawn 4 workers that perform square root calculations
$ stress -c 4

# Spawn 8 CPU workers
$ stress -c 8

# Run CPU workers in background
$ stress -c 8 &

2. Memory Stress

Create memory allocation stress:

# Allocate 1GB of memory with 2 workers
$ stress -m 2 --vm-bytes 1G

# Allocate 2GB of memory with 4 workers
$ stress -m 4 --vm-bytes 2G

# Perform memory writes
$ stress -m 4 --vm-bytes 2G --vm-method write

3. I/O Stress

Generate I/O load:

# 3 workers performing disk I/O
$ stress -i 3

# Combine CPU and I/O stress
$ stress -c 4 -i 2

4. Comprehensive System Stress

Create multi-dimensional load:

# 4 CPU workers, 2 memory workers, 1 I/O worker
$ stress -c 4 -m 2 -i 1

# Long-running stress test (30 minutes)
$ stress -c 4 -m 2 -i 1 --timeout 1800

5. Watching System Load

Monitor system load during stress test:

# Before stress test
$ uptime
 04:12:18 up 182 days, load average: 3.27, 0.94, 0.36

# During stress test
$ uptime
 04:13:59 up 182 days, load average: 8.66, 3.45, 1.32

Advanced Options

Timeout Control

Limit stress test duration:

# Run stress for 15 minutes (900 seconds)
$ stress -c 4 --timeout 900

Verbose Mode

Get detailed information:

# Verbose output
$ stress -v -c 4

Best Practices

  1. Start Small: Begin with fewer workers and gradually increase load.
  2. Monitor System: Use top, htop, or uptime to track resource usage.
  3. Controlled Environment: Test during maintenance windows.
  4. Backup Critical Data: Always have backups before stress testing.

Potential Risks

  • Excessive stress can temporarily freeze or crash systems
  • Not recommended on production servers without careful planning
  • May impact other running services

Troubleshooting

  • If stress fails, ensure you have sufficient system resources
  • Check kernel support for stress-ng features
  • Verify package installation

Alternatives

  • stress-ng: More advanced stress testing utility
  • sysbench: Scriptable database and system performance benchmark
  • dd: Simple I/O performance testing

Conclusion

Stress utility provides a flexible way to simulate system load, helping administrators and developers understand system behavior under pressure.