使用 ab 命令对 http 服务进行压测

ab命令全称为:Apache bench 。是Apache自带的压力测试工具。ab命令非常实用,它不仅可以对Apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。可以测试安装Web服务器每秒种处理的HTTP请求。

使用 Go 创建一个简单的 http 接口

package main

import (
"log"
"net/http"
)

func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})

err := http.ListenAndServe(":8888", nil)
if err != nil {
log.Fatalf("ListenAndServe: %v", err)
}
}

使用 ab 命令进行压测

# 并发 1 00 个连接,共 10000 个请求
ab -n 10000 -c 100 http://localhost:8888/hello

结果:

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

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname: localhost
Server Port: 8888

Document Path: /hello
Document Length: 11 bytes

Concurrency Level: 100
Time taken for tests: 0.675 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1280000 bytes
HTML transferred: 110000 bytes
Requests per second: 14817.10 [#/sec] (mean)
Time per request: 6.749 [ms] (mean)
Time per request: 0.067 [ms] (mean, across all concurrent requests)
Transfer rate: 1852.14 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 0.5 3 5
Processing: 1 3 0.5 3 5
Waiting: 0 3 0.5 3 5
Total: 5 7 0.7 7 10

Percentage of the requests served within a certain time (ms)
50% 7
66% 7
75% 7
80% 7
90% 8
95% 8
98% 8
99% 9
100% 10 (longest request)
Published At