使用ab测试Web性能

Web服务程序

ab - Apache HTTP server benchmarking tool

gohttp.go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import (
"net/http"
)

func SayHello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
}

func main() {
http.HandleFunc("/hello", SayHello)
err := http.ListenAndServe(":8001", nil)
if err != nil {
panic(err)
}
}

启动Web服务器程序:

1
$ go run gohttp.go

测试可以访问:

1
2
$ curl http://localhost:8001/hello
Hello

用ab进行压力测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$ ab -n 10000 -c 100 http://localhost:8001/hello
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
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: 8001

Document Path: /hello
Document Length: 5 bytes

Concurrency Level: 100
Time taken for tests: 1.175 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1210000 bytes
HTML transferred: 50000 bytes
Requests per second: 8508.34 [#/sec] (mean)
Time per request: 11.753 [ms] (mean)
Time per request: 0.118 [ms] (mean, across all concurrent requests)
Transfer rate: 1005.38 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 1.3 5 17
Processing: 4 6 1.4 6 20
Waiting: 3 6 1.3 6 17
Total: 8 12 2.0 11 27

Percentage of the requests served within a certain time (ms)
50% 11
66% 12
75% 13
80% 13
90% 14
95% 15
98% 17
99% 22
100% 27 (longest request)