본문 바로가기

IT이야기

[20061108] apache ab를 이용한 벤치마크

2006년 11월 08일 작성

1. 기본정보

apache 에는 ab 라고 하는 벤치마크 툴이 포함되어 있다. ab를 이용하면 특정 서버 또는 웹 프로그램의 성능을 테스트해볼 수 있다. 보통 ab는 다음과 같은 위치에 설치된다.

/usr/local/apache/bin/ab

ab 실행과 관련된 명령과 옵션은 다음과 같다.

Usage: ./ab [options] [http://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make
    -t timelimit    Seconds to max. wait for responses
    -p postfile     File containg data to POST
    -T content-type Content-type header for POSTing
    -v verbosity    How much troubleshooting info to print
    -w              Print out results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234' (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: zop'
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -h              Display usage information (this message)

ab에서 주로 사용하는 옵션은 -t, -c, -n 이다.

ab -t 10 -c 10 url

이 옵션은 지정된 URL에 대해서 동시 클라이언트© 10개로 10초간 지속해서 최대 가능한 request를 보내고 응답을 받는다. 시간이 정해져 있기 때문에, 결과로 나온 회수로 비교가 가능하다.

ab -n 100 -c 10 url

이 옵션은 지정된 URL에 대해서 동시 클라이언트© 10개로 최대 request(n)을 보내고 응답을 받는다. request가 정해져 있기 때문에, 결과로 나온 값시간으로 비교가 가능하다.
주로 이 두 가지를 방법을 이용해서 사용하는데, 첫번째 방법을 개인적으로 좋아한다.

ab를 실행하면 다음과 같은 화면이 출력된다.

/usr/local/apache/bin/ab -t 1 -c 1 http://localhost/
This is ApacheBench, Version 1.3d <$Revision: 1.73 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Finished 328 requests
Server Software:        Apache/1.3.36
Server Hostname:        localhost
Server Port:            80

Document Path:          /
Document Length:        1456 bytes

Concurrency Level:      1
Time taken for tests:   1.000 seconds
Complete requests:      328
Failed requests:        0
Broken pipe errors:     0
Total transferred:      584633 bytes
HTML transferred:       479024 bytes
Requests per second:    328.00 [#/sec] (mean)
Time per request:       3.05 [ms] (mean)
Time per request:       3.05 [ms] (mean, across all concurrent requests)
Transfer rate:          584.63 [Kbytes/sec] received

Connnection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0     0    0.0      0     0
Processing:     2     2    0.1      2     5
Waiting:        2     2    0.1      2     5
Total:          2     2    0.1      2     5
WARING: The median and mean for the processing time are not within a normal deviation
        These results are propably not that reliable.
WARING: The median and mean for the total time are not within a normal deviation
        These results are propably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      3
  95%      3
  98%      3
  99%      3
 100%      5 (last request)

대략적으로 이해하리라고 보고, 궁금한 것은 질문하세요.

 

2. ab 활용하기

ab 결과 화면에 내용과 줄이 많기 때문에 손쉽게 비교하기가 쉽지 않다. 다양한 조건을 넣어가면서 테스트를 하면 그 내용을 정리하기가 쉽지 않다. 특히 벤치마크 테스트에서는 동시에 접속하는 클라이언트 숫자에 따라 결과가 많이 달라진다. 조건을 다양하게 바꾸면서 테스트하면 그 출력 결과를 분석하고 정리하기가 쉽지 않다. 그래서 클라이언트 접속 숫자를 바꾸면서 동일한 조건으로 테스트하고 그 결과를 출력하는 스크립트를 만들어 사용한다.

#!/bin/sh

if [ $# -lt 5 ]; then
  echo "abstress.sh <url> <time> <start> <max> <inc>"
  echo ""
  exit 1
fi

url=$1
tt=$2
start=$3
max=$4
inc=$5

num=$start

while true
do
  /usr/local/apache/bin/ab -t $tt -c $num $url 2>/dev/null > $$

  tps=`cat $$ | grep Requests\ per\ second: | awk '{print $4;exit;}'`
  echo "$num $tps"
  num=`expr $num + $inc`
  if [ $num -gt $max ];then
    exit;
  fi
  sleep 10
done
abstress.sh <url> <time> <start> <max> <inc>
./abstress.sh http://localhost/ 5 10 20 5

스크립트를 실행하면 다음과 같은 화면이 출력된다.

100 326.01
150 323.32
200 304.53

동시 접속을 100, 150, 200 으로 바꿔 가면서 테스트한 결과이다.

 

3. ab 로 벤치마크 계획하기

벤치마크를 하기 위해서는 우선 비교 가능한 시스템이 있어야 한다. 각 시스템은 하드웨어 사양은 다른 것이 좋으며, 어플리케이션 설치는 동일한 조건과 옵션을 가지는 것이 좋다. 벤치마크를 수행하는 도중에는 외부로부터 간섭이 발생하지 않게끔 해야 한다.
벤치마크를 하려면 비교할 웹 프로그램을 설정해야 한다. 가장 이상적인 상태와 가장 문제가 있는 상태까지 각 구간을 나누어서 테스트를 한다.

웹에서 벤치마크를 하려면 제일 먼저 웹 서버가 보통 가지는 능력을 테스트한다. 가장 좋은 방법은 정적인 HTML을 다양하게 테스트해본다.
그 다음으로 php를 사용한다면 echo 만 있는 php를 테스트한다. 이후에는 include 가 어느 정도 있는 경우와 심하게 있는 경우로 나누어서 내부적인 캐싱과 파일 include 능력을 테스트한다.
php에서는 소스 라인수가 특정 규모를 넘어가면 속도가 느려지는 현상이 있는데, 이에 대해서 의미없는 소스라인을 100, 500, 1000, 5000, 10000 정도 나누어서 테스트한다.
DB 와 연결하는 테스트도 중요하다. 만약 postgres를 사용한다면 postgres와 pgpool 모두를 테스트해본다. 각자 단지 connect, close만 하는 경우를 테스트한다. 그리고 select, insert, update, stored procedure 를 이용하는 경우를 나누어서 테스트한다.
이 정도 테스트를 하고 나면 이제 실제 웹 프로그램을 호출하는 테스트를 한다. 웹 프로그램은 보통 메인 페이지와 속도가 느리다고 판단되는 페이지를 몇개 선정해 본다. 각 페이지에서 SQL 문만 빼내서 별도의 웹 프로그램으로 만든 후 각 비교하는 것도 좋다.

분석은 엑셀이 매우 좋다. 파일 업로드가 되면 엑셀로 나온 결과 값을 올려보겠다.