使用Apache Bench和Vegeta工具对LiteSpeed/Nginx/Apache进行性能比较

首先

本文是富士通云技术2021年圣诞月历的第23篇文章。

你好。我是@s_ave,在富士通云技术有限公司担任商务职位。
昨天的文章是@ksw分享的关于使用Gopacket进行抓包和生成包的内容。对于包的内容,我只具有关于信息处理技术师考试以前学到的一些模糊的知识,所以我想参考这篇文章来试一试。

今天的文章是关于LiteSpeed、Nginx和Apache性能比较的。最近我要创建一个新的服务,从工程师那里听说要使用LiteSpeed作为Web服务器。以前我一直认为在Linux上使用的Web服务器应该是Apache或者Nginx,而LiteSpeed对我来说是个新听说的名字,所以我决定试着安装并进行简单的性能比较。

我們將在一台Linux伺服器上安裝LiteSpeed、Nginx和Apache,並在不同的端口上同時啟動它們,以此進行相同條件下的性能比較。

LiteSpeed 是什么?

根据调查,据说它被称为仅次于Apache、Nginx和IIS的第四种网络服务器。它的特点是与Apache的兼容性很高,性能也优于Apache和Nginx。

尽管初版发布自2003年以来相当古老,但根据W3Techs的WEB服务器份额比较,从去年开始使用量急剧增长,今年似乎超过了IIS的份额。对于新建WEB服务器的选择来说,它似乎很流行。

web_share2.png

我记得大约10年前,我经常接触服务器的时候,提到Linux的WEB服务器,就只有Apache一种选择。我记得当时Nginx才开始成为话题,但现在Apache的市场份额已经降至约30%左右。没注意之间,市场份额已经发生了巨大的变化。

准备WEB服务器

安装和启动LiteSpeed

我們使用的操作系統是CentOS 8.3。由於CentOS的倉庫中沒有LiteSpeed,所以需要進行安裝。我們將從LiteSpeed所依賴的套件中進行安裝。操作步驟如下。

# yum -y install epel-release
# rpm -Uvh http://rpms.litespeedtech.com/centos/litespeed-repo-1.2-1.el8.noarch.rpm
# yum -y install openlitespeed

安装完成后,使用Service命令启动,并进行进程确认。

# service lsws start
# ps -ef|grep http
root        4674       1  0 18:11 ?        00:00:00 openlitespeed (lshttpd - main)
nobody      4676    4674  0 18:11 ?        00:00:00 openlitespeed (lshttpd - #01)
nobody      4677    4674  0 18:11 ?        00:00:00 openlitespeed (lshttpd - #02)
root        4850    4814  0 18:13 pts/0    00:00:00 grep --color=auto http

服务器已成功启动。要停止服务器,请使用命令service lsws stop。LiteSpeed在刚安装后默认端口为8088,通常会通过Web控制台更改为80号端口,但本次将保持不变。

安装和启动Nginx

Nginx可以在CentOS的软件仓库中找到,但是它的版本不是最新的,所以我们会从Nginx官方的软件仓库进行安装。但是由于篇幅原因,在这里省略了详细步骤。请参考以下网站等来了解安装方法。

完成安装后,请使用service命令启动并检查进程。

# service nginx start
# ps -ef|grep nginx
root        5033       1  0 18:17 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx       5034    5033  0 18:17 ?        00:00:00 nginx: worker process
nginx       5035    5033  0 18:17 ?        00:00:00 nginx: worker process

服务器已经成功启动。如果要停止服务器,请使用命令”service nginx stop”。Nginx在安装后默认使用80端口,可以直接使用。

安装并启动Apache

由于Apache和CentOS存储库中的版本较旧,因此我们将采用与Nginx相似的步骤。
以下网站提供了有关安装最新版本的详细说明。

安装完毕后,Apache默认使用80端口进行配置,但由于Nginx已经占用了80端口,所以需要按以下步骤将端口更改为8008。

# vi /etc/httpd/conf/httpd.conf
~

# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 8008

~

安装完成后,使用service命令来启动,并进行进程确认。

# service httpd start
# ps -ef|grep http
root        8925       1  0 18:48 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      8926    8925  0 18:48 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      8927    8925  0 18:48 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      8928    8925  0 18:48 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      8929    8925  0 18:48 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

服务器已经成功启动。当需要停止服务器时,请输入命令 service httpd stop。

在文档根目录下放置虚拟文件。

为了在后续的基准测试中验证不同文件大小的性能,我们将在每个WEB服务器的文档根目录中使用以下命令放置创建的虚拟文件。容量从上到下分别为1KB、1MB和10MB。

# dd bs=1K count=1 if=/dev/zero of=1KB.html
# dd bs=1M count=1 if=/dev/zero of=1MB.html
# dd bs=10M count=1 if=/dev/zero of=10MB.html

默认文档根目录的位置如下所示。

WEBサーバーパスLiteSpeed/usr/local/lsws/Example/html/Nginx/usr/share/nginx/html/Apache/var/www/html/

用这个,WEB服务器已经准备完成了。

准备基准测试工具。

选择基准测试工具。

在网上搜索了一些资料之后,发现Apache自带的Apache Bench工具似乎不需要安装,很方便地使用起来。

另外,我在寻找其他选择时发现了以下文章。

我决定尝试一下Vegeta这个工具,它在星级评价方面表现出色,与Apache Bench一样容易使用,而且可以提供与Apache Bench不同的报告视角。

因此,我想通过同时使用Apache Bench和Vegeta来进行本次性能比较。

执行Apache Bench测试

Apache安装完成后,可以立即使用,无需事先准备。使用方法是,通过n选项指定总请求数,通过c选项指定并发执行数。例如,当执行以下命令时,将会有10个用户同时连接,每个用户发送30个请求。

# ab -n 300 -c 10 http://kakarot:8088/1KB.html
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 kakarot (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Finished 300 requests


Server Software:        LiteSpeed
Server Hostname:        kakarot
Server Port:            8088

Document Path:          /1KB.html
Document Length:        1024 bytes

Concurrency Level:      10
Time taken for tests:   0.024 seconds
Complete requests:      300
Failed requests:        0
Total transferred:      379200 bytes
HTML transferred:       307200 bytes
Requests per second:    12600.81 [#/sec] (mean)
Time per request:       0.794 [ms] (mean)
Time per request:       0.079 [ms] (mean, across all concurrent requests)
Transfer rate:          15554.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     0    0   0.1      0       1
Waiting:        0    0   0.1      0       1
Total:          0    1   0.1      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)

以下是用于检查Web服务器性能的主要项目:

項目概要実行テスト時の値Requests per second1秒間あたりのリクエスト処理可能件数12600.81/secTime per request1リクエスト当たりの処理時間0.794ms

维吉特的执行测试

這裡是從安裝程序開始。由於Vegeta是用Go語言編寫的,所以首先要安裝Go。

# wget https://storage.googleapis.com/golang/go1.17.5.linux-amd64.tar.gz
# tar -C /usr/local/ -xzf go1.17.5.linux-amd64.tar.gz
# export PATH=$PATH:/usr/local/go/bin

接下来安装Vegeta。

# wget https://github.com/tsenart/vegeta/releases/download/v12.8.4/vegeta_12.8.4_linux_amd64.tar.gz
# tar -C /usr/local/vegeta -zxvf vegeta_12.8.4_linux_amd64.tar.gz
# chmod +x vegeta
# export PATH=$PATH:/usr/local/vegeta

使用方法是使用”vegeta attack”命令执行请求。可以使用rate选项指定每秒请求数量,并使用duration选项指定attack持续多少秒。在下面的示例中,发送了1000个请求×3秒=3000个请求。命令的执行结果将以二进制数据的形式返回。该二进制数据可以使用”vegeta report”命令连接并输出。

# echo "GET http://kakarot:8088/1KB.html" | vegeta attack -rate=1000 -duration=3s | vegeta report
Requests      [total, rate, throughput]         3000, 1000.34, 1000.29
Duration      [total, attack, wait]             2.999s, 2.999s, 139.008µs
Latencies     [min, mean, 50, 90, 95, 99, max]  104.564µs, 141.779µs, 134.95µs, 157.983µs, 170.424µs, 343.462µs, 1.257ms
Bytes In      [total, mean]                     3072000, 1024.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:3000  
Error Set:

在Vegeta的报告中,可以确认的性能指标只有按百分位数划分的延迟,而与Apache Bench不同的是增加了请求成功率这一项。在每秒请求数约为100或1000时,成功率几乎达到100%;但是在施加数千个以上请求的负载时,会出现请求失败的情况。

Apache Bench和vegeta的特点比较

让我们比较一下Apache Bench和vegeta的报告项目。

レポート項目Apache Benchvegetaリクエスト処理時間〇〇パーセンタイル別リクエスト処理時間〇〇秒間処理リクエスト数〇×リクエスト成功率×〇

我认为要比较Web服务器的基本性能,可以通过Apache Bench的请求每秒处理数指标(Requests per second)来了解。另一方面,通过使用vegeta在指定的每秒大量请求下观察成功率(Success),可以确认其在高负载情况下的耐受能力。在下一部分,我想结合这些内容实际进行性能比较。

性能测试结果

在Apache Bench进行的性能测试结果

请求数为100,请求时的同时连接为10并且是固定的。

ファイル容量Requests per second(LiteSpeed)Requests per second(nginx)Requests per second(Apache)1KB12586.5311689.987035.321MB2891.262860.902771.3110MB362.12372.71334.20

根据这份报告,轻量级文件对于LiteSpeed来说,它似乎能够处理更多的请求。但随着文件容量的增大,差异似乎变得更小。尤其是10MB的文件,Nginx的表现往往更优。我们进行了几次测试,结果是无论文件容量大小如何,Apache表现出好的数值的情况很少见。

在Vegeta上的性能测试结果。

由于在执行大容量文件时频繁出现错误,因此我们将文件容量固定为1KB。同时,持续时间固定为3秒。

秒間リクエスト数Success(LiteSpeed)Success(Nginx)Success(Apache)10,000100%100%63.59%15,00034.15%100%18.44%20,00033.13%37.88%24.52%

据说Nginx在承受高负荷方面是最优秀的。

总结

在评判中发现LiteSpeed的性能比Apache更高,但与Nginx相比则因条件而异,很难一概而论优劣,这是我们得出的结论。然而,这只是在有限条件下的基准测试,仅供参考。

明天(12/24)的富士通云技术2021年圣诞节日历活动,将由@u-koji的文章推送。敬请期待!

广告
将在 10 秒后关闭
bannerAds