Apache Bench是Apache服务应用服务器自带的性能测试软件。这篇文章介绍一下如何使用Alpine基础镜像将Apache Bench的压测能力进行容器化,并结合具体的示例来演示此镜像从构建到使用的整体过程。
Alpine镜像的特点就是小,而Apache Bench本身就是apache2-utils包中的一个工具,所以只需要在Alpine中执行apk add apache2-utils即可完成Apache Bench的镜像化。
liumiaocn:ab liumiao$ ls
Dockerfile
liumiaocn:ab liumiao$ cat Dockerfile
FROM alpine:3.10.2
RUN apk update \
&& apk add apache2-utils \
&& rm -rf /var/cache/apk/*
liumiaocn:ab liumiao$
liumiaocn:ab liumiao$ docker build -t apachebench:2.3 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine:3.10.2
3.10.2: Pulling from library/alpine
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:3.10.2
---> 961769676411
Step 2/2 : RUN apk update && apk add apache2-utils && rm -rf /var/cache/apk/*
---> Running in 3b0991f27efa
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
v3.10.2-80-g68e4e4a13a [http://dl-cdn.alpinelinux.org/alpine/v3.10/main]
v3.10.2-83-g64319a6606 [http://dl-cdn.alpinelinux.org/alpine/v3.10/community]
OK: 10336 distinct packages available
(1/5) Installing libuuid (2.33.2-r0)
(2/5) Installing apr (1.6.5-r0)
(3/5) Installing expat (2.2.8-r0)
(4/5) Installing apr-util (1.6.1-r6)
(5/5) Installing apache2-utils (2.4.41-r0)
Executing busybox-1.30.1-r2.trigger
OK: 6 MiB in 19 packages
Removing intermediate container 3b0991f27efa
---> 2dc7eca87521
Successfully built 2dc7eca87521
Successfully tagged apachebench:2.3
liumiaocn:ab liumiao$
liumiaocn:ab liumiao$ docker images |grep alpine |grep 3.10.2
alpine 3.10.2 961769676411 6 weeks ago 5.58MB
liumiaocn:ab liumiao$
而生成的Apache Bench的镜像大小为6.41M
liumiaocn:ab liumiao$ docker images |grep apachebench
apachebench 2.3 2dc7eca87521 2 minutes ago 6.41MB
liumiaocn:ab liumiao$
在本地机器的8088端口使用Docker启动一个Nginx应用(使用其他方式也可),示例如下所示:
liumiaocn:~ liumiao$ docker images |grep nginx |grep latest
nginx latest e445ab08b2be 2 months ago 126MB
liumiaocn:~ liumiao$ docker run -p 8088:80 -d --name=nginx-test nginx:latest
a80fb1a4fc20627891a6bd7394fd79ae9aefb7dc8cf72c12967bc2673a815308
liumiaocn:~ liumiao$
使用curl命令或者直接使用浏览器确认nginx已正常运行
liumiaocn:~ liumiao$ curl http://localhost:8088/
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
liumiaocn:~ liumiao$
liumiaocn:ab liumiao$ docker run -it --rm apachebench:2.3 ab -V
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
liumiaocn:ab liumiao$
压测内容:对http://192.168.31.242:8088/并发启动100个进程,平均10次,共计执行1000次
执行命令如下所示:
执行命令:ab -n 1000 -c 100 http://192.168.31.242:8088/
执行参数说明:
执行日志如下所示:
liumiaocn:ab liumiao$ docker run -it --rm apachebench:2.3 ab -n 1000 -c 100 http://192.168.31.242:8088/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.31.242 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx/1.17.2
Server Hostname: 192.168.31.242
Server Port: 8088
Document Path: /
Document Length: 612 bytes
Concurrency Level: 100
Time taken for tests: 0.791 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 845000 bytes
HTML transferred: 612000 bytes
Requests per second: 1264.94 [#/sec] (mean)
Time per request: 79.055 [ms] (mean)
Time per request: 0.791 [ms] (mean, across all concurrent requests)
Transfer rate: 1043.82 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 12 2.7 11 25
Processing: 10 38 6.4 38 55
Waiting: 2 28 5.7 28 52
Total: 11 49 7.5 49 70
Percentage of the requests served within a certain time (ms)
50% 49
66% 52
75% 55
80% 56
90% 59
95% 61
98% 65
99% 66
100% 70 (longest request)
liumiaocn:ab liumiao$
结果的说明与介绍可参看:https://blog.csdn.net/liumiaocn/article/details/101924143