Coding Gun

ทำ Load Test ด้วย Apache Bench(ab)

Apache Bench(ab) คือเครื่องมือสำหรับทำ Load Test และ Stress Test สามารถเรียกใช้งานผ่านทาง command line ได้ง่ายๆ เหมาะกับการทำ Load Test ที่ต้องการดูผลลัพธ์เร็วๆ ไม่ต้องกำหนด configuration ที่ยุ่งยาก

สำหรับผู้ใช้ MacOS เรามี Apache Bench อยุ่ในเครื่องอยู่แล้วสามารถใช้งานได้เลยไม่ต้องติดตั้งอะไรเพิ่มเติม

การติดตั้ง Apache Bench บน Linux

ถ้าเราต้องการติดตั้ง Apache Bench บน Linux เราสามารถติดต้ังด้วย apt-get ได้เลย

  1. เริ่มจากการ update ข้อมูลของ package ก่อน
    $ apt-get update
    
  2. ติดตั้ง Apache Bench
    $ apt-get install apache2-utils
    
  3. ทดสอบ Apache bench โดยการดึงเอา Version ของ Apache Bench ออกมาแสดง โดยใช้ -V (ต้องเป็น V ใหญ่ด้วยนะ)
    $ ab -V
    
    ผลลัพธ์จะออกมาแบบนี้
    This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    

เริ่มต้นทำ Load Test

เราสามารถทำ Load Test ได้ด้วยการใส่ parameters ต่างๆเหล่านี้เข้่าไปใน command

  1. -n จำนวน Requests ที่จะส่งไป

    $ ab -n 1000 http://scanme.nmap.org/
    

    ในตัวอย่างนี้จะเป็นการส่ง requests ไปยังปลายทางจำนวน 1000 requests

  2. -c จำนวน Requests ที่ส่งไปพร้อมกัน(Concurrency)

    $ ab -n 10000 -c 1000 http://scanme.nmap.org/
    

    ถ้าเรากำหนด -c เป็น 1000 หมายถึง apache bench จะส่ง reqeusts ไปพร้อมๆกันทีละ 1000 requests ทั้งหมด 10 รอบ(เพราะเราต้องส่งทั้งหมด 10000 requests)

  3. -s ถ้า Response ไม่ตอบกลับมาในเวลานี้ถือว่า Timeout(หน่วยเป็น วินาที)

    $ ab -s 5 http://scanme.nmap.org/
    

    ถ้า Server ไม่ตอบกลับมาภายใน 5 วินาที จะถือว่าเป็น Timeout ค่า default จะเป็น 30 วินาที

  4. -H ในกรณีที่เราต้องการเปลี่ยน Header หรือเพิ่ม Header เข้าไปในแต่ละ Request ให้ใช้ parameter นี้

  5. -m ระบุ Http Method ที่ต้องการส่งไป

ข้อควรระวัง URL ที่เป็นปลายทางต้องอยู่ในรูปแบบที่ถูกต้อง เราต้องใส่ให้ครบทั้ง

  1. protocols เป็น http หรือ https
  2. host คือที่อยู่ของเป้าหมายที่ต้องการทดสอบจะเป็น IP address หรือ Domain name ก็ได้
  3. path จะต้องระบุ path ด้วยถ้าเกิดเราต้องการส่ง request เข้าไปที่ root ให้ใส่ / ต่อท้ายด้วย

วิเคราะห์ผลลัพธ์

หลังจากที่เรารู้จักวิธีใช้งาน Apache Bench กันแล้ว เราลองมาดูว่าผลลัพธ์ที่ได้มาบอกอะไรเราได้บ้าง

เริ่มต้นจากการ run command นี้ก่อน

$ ab -n 100 http://scanme.nmap.org/

เราจะได้ output ออกมาหน้าตาแบบนี้

This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking scanme.nmap.org (be patient).....done


Server Software:        Apache/2.4.7
Server Hostname:        scanme.nmap.org
Server Port:            80

Document Path:          /
Document Length:        6974 bytes

Concurrency Level:      10
Time taken for tests:   2.925 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      713100 bytes
HTML transferred:       697400 bytes
Requests per second:    34.19 [#/sec] (mean)
Time per request:       292.487 [ms] (mean)
Time per request:       29.249 [ms] (mean, across all concurrent requests)
Transfer rate:          238.09 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        3    8   7.5      5      32
Processing:   209  238  64.2    215     429
Waiting:      209  236  63.6    213     428
Total:        213  245  64.8    221     447

Percentage of the requests served within a certain time (ms)
  50%    221
  66%    223
  75%    229
  80%    244
  90%    434
  95%    434
  98%    436
  99%    447
 100%    447 (longest request)

สิ่งเราสามารถสรุปได้จาก report ที่ออกมามีดังนี้

  1. Document Length บอกว่า Response ที่ตอบกลับมามีขนาด 6974 bytes

  2. Time taken for tests เวลาที่ใช้ในการทดสอบทั้งหมด 2.925 seconds

  3. Complete requests ส่ง requests ไปทั้งหมด 100 requests มี response ตอบกลับมา 100 requests เลย

  4. Failed requests ในกรณีนี้ไม่มี request ไหนที่ไม่ตอบกลับมา

  5. Requests per second เราสามารถส่ง requests ได้ 34.19 requests ต่อ 1 วินาที ลองปรับ parameter -c ดูค่าที่ได้จะต่างออกไป

  6. Time per request เป็นเวลาที่ใช้ต่อ 1 request ในกรณีนี้เราได้ 292.487 มิลลิวินาที

  7. ส่วนตารางด้านล่างจะบอก Percentile ของ requests โดยเราจะนำเวลาที่ใช้มาเรียงลำดับแล้วดูว่าเวลาที่เราใช้ในแต่ละ request นั้นเป็นเท่าไหร่ เช่น เราจะเห็นว่า request ส่งไปแล้วช้าที่สุดใช้เวลา 447 มิลลิวินาที

    Percentage of the requests served within a certain time (ms)
    50%    221
    66%    223
    75%    229
    80%    244
    90%    434
    95%    434
    98%    436
    99%    447
    100%    447 (longest request)
    

    ส่วนใหญ่เราจะใช้ percentile ที่ 90 หรือ 95 เพื่อบอกประสิทธิภาพของ Server(เพราะ Response Time ที่ช้าจะมีจำนวนน้อย แต่จะมีค่าสูงมากจนทำให้ค่า average นั้นเปลี่ยนไป เราจึงใช้ percentile แทน average) เช่นในกรณีนี้เราอาจบอกได้ว่า Response time ของ Server นี้จะอยู่ที่ 434 มิลลิวินาที

    อ่านบทความอื่นๆต่อได้ที่

Phanupong Permpimol
Follow me

Software Engineer ที่เชื่อในเรื่องของ Process เพราะเมื่อ Process ดี Product ก็จะดีตาม ปัจจุบันเป็นอาจารย์และที่ปรึกษาด้านการออกแบบและพัฒนา Software และ Web Security