[Linux] 리눅스 서버 스트레스 테스트(CPU 과부하 시키기)

2022-04-13


alex-holt-TrRWdVys09o-unsplash.jpg
Photo by Alex Holt on Unsplash

오토스케일링과 같이 서버의 과부하를 높여 테스트를 해야 하는 경우가 있다. 실제 트래픽을 유발해서 테스트를 하는 것이 가장 좋은 방법이 지만, 시간과 자원이 여유치 못할 때 서버의 직접 적인 부하를 주어 테스트하는 방법이 있는데, 오늘은 이와 같은 방법을 알아보자.


- 패키지 설치

# 설치 명령어
yum install -y stress
# 버전 확인 명령어
stress --version

위의 명령어는 순서대로 패키지를 설치하고 해당 패키지의 버전을 확인하는 명령어 이다. 만약에 자신의 운영하는 서버가 EC2이고 단순히 설치가 이루어지지 않을 경우 아래의 명령어를 실행한 후 다시 패키지를 설치해보자.

# 아마존 리눅스 전용 epel 설치 명령어
amazon-linux-extras install epel -y

- 사용명령어

stress --help

위의 help 옵션을 주어 보면 자세히 해당 명령어의 기능에 대한 설명이 나온다.

 

-?, --help         show this help statement
     --version      show version statement
 -v, --verbose      be verbose
 -q, --quiet        be quiet
 -n, --dry-run      show what would have been done
 -t, --timeout N    timeout after N seconds
     --backoff N    wait factor of N microseconds before work starts
 -c, --cpu N        spawn N workers spinning on sqrt()
 -i, --io N         spawn N workers spinning on sync()
 -m, --vm N         spawn N workers spinning on malloc()/free()
     --vm-bytes B   malloc B bytes per vm worker (default is 256MB)
     --vm-stride B  touch a byte every B bytes (default is 4096)
     --vm-hang N    sleep N secs before free (default none, 0 is inf)
     --vm-keep      redirty memory instead of freeing and reallocating
 -d, --hdd N        spawn N workers spinning on write()/unlink()
     --hdd-bytes B  write B bytes per hdd worker (default is 1GB)

Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s

 

명령어 실행 시 위와 같이 자세한 사용법이 나오면 친절하게 예제도 확인할 수 있다. 그중 자주 사용하는 옵션은 cpu와 timeout 이며, 이 두 가지만 사용해도 충분히 스트레스 테스트를 해볼 수 있다.


# 2코어의 cpu를 20초동안 과부화시킨다.
stress --cpu 2 --timeout 20s

# 4코어의 cpu를 60초동안 과부화시킨다.
stress --cpu 4 --timeout 60s

위의 예제를 참고해서 사용하면 된다.


메인 이미지 출처 : Photo by Alex Holt on Unsplash