高性能PHP学习-基准测试技术
高性能PHP学习--基准测试技术
一、基准测试工具
基准测试工具提供在各种不同的模拟用户请求发生时有关Web服务器响应的统计信息。它们允许我们模拟任意数量的请求Web服务器上某个特定Web文档的用户,更重要的是,它允许我们模拟任意数量的用户同时访问Web服务器上的文档(并发请求)。
例如,每个工具提供的信息都与下列内容有关:1、响应一个请求所花费的总时间
2、来自服务器的总响应大小
3、Web服务器每秒可以处理的请求总数
二、Apache Benchmark
在这里我向大家介绍一下Apache Benchmark(ab/ab2)工具。
这是一个最著名的基准测试工具之一,它是默认的Apache安装的一部分,能够通过模拟对特定URL的任意数量请求来对Web服务器进行负载测试。ab工具提供以下信息:
1、传输的总数据大小(以字节为单位)
2、Web服务器在模拟流量下每秒可以支持的请求总数
3、完成一个请求所花费的最长时间(以毫秒为单位)
4、完成一个请求所花费的最短时间(以毫秒为单位)
注:在ubuntu操作系统中运行的是ab命令,而笔者使用的是opensuse,此系统下使用的命令是ab2.所以笔者在操作的时候使用的是ab2命令,但是为了方便,写的时候一律写ab命令。
使用ab工具还可以运行很多不同的负载模拟,例如:
1、对Web文档的模拟请求
2、指定时间内的请求
3、打开Keep-Alive时的请求
最重要的是,Apache Benchmark是独立与Apache Web服务器的,从而可以在运行ab的同时使运行此工具的
计算机上的Web服务器处于非活动状态。
三、安装Apache Benchmark
在linux/Unix系统下,只要安装了apache之后就可以使用ab命令了。
在windows下,用户可以打开http://httpd.apache.org/。加载此页后,单击页面左侧的"Download from a mirror"(从镜像下载)链接,找到适合你的系统的相应下载程序包,即Window 32 Binary版本,然后下载。
当程序包下载完后,就可以运行安装向导在系统的任意位置上安装软件。我将Apache安装再默认位置C:\Program Files\Apache Software Foundation,但也可以安装在系统的任意位置。此处所选择的位置就是APACHE_HOME引用所指向的位置。
现在,打开
四、使用ab命令
现在,让我们来使用ab这个工具吧。(基准测试以www.example.com为例)
所有ab命令的组成遵循此结构:
ab [options] [full path to web document]
例:
ab -n 1 http://www.example.com/
n表示再指定的URL上执行的请求数。在这个例子里,ab只请求Web文档一次,但是n的值可以是小于50000的任意数字。默认情况下,n设置为1。
该命令的下一部分是URL部分。对于刚刚执行的命令,URL为http://www.example.com/。如果选择测试此域中的某个文档,如test.php(并不存在),则URL为:http://www.example.com/test.php
执行上面命令的结果如下图所示:
1、服务器信息
包含Web服务器运行的软件、主机名和服务器监听的端口号。
2、脚本信息
包含有关运行模拟的Web文档的信息。Document Path包含请求的文档,而Document Length包含所有HTML、图像、CSS、JS以及响应中任何内容的字节数总和。
3、连接信息
包含信息的主体。它回答了诸如“收到请求响应需要多长时间?”、“返回了多少数据?”之类的信息,最重要的是它回答了“处理文档时Web服务器可以支持多少用户?”。
下表提供了此部分数据的完整列表和描述。
ab响应描述
4、连接指标细目分类最后一个部分包含一个表,其中包含Connect、Processing、Waiting以及Total字段。这些字段告诉我们请求在每个过程状态中所需的时间。我们最感兴趣的是Total字段及其最大、最小值列。
ab选项标记
下面结合上面的标记进行一些测试
1、并发测试
模拟一个并发测试,同时对Web服务器进行10个并发请求,直到进行到100个请求位置。使用c标记时的一项警告是让使用的值小于等于要进行的请求总数n。
例:ab -n 100 -c 10 http://www.example.com
ab命令响应如下图

www.example.com的并发模拟结果
通过模拟的并发请求,我们可以看到Request per second字段,并注意到Web服务器每秒可以支持 个请求(用户)。分析Connection Metrics的Total min和max列,我们注意到再10个并发请求的指定流量负载下,最快的响应为371毫秒,而最慢的请求花了575毫秒。
但是,我们知道流量不会只是持续1、2或3秒,高流量可能会持续几分钟、几小时,甚至是几天。下面让我们运行一个模拟来测试一下。
2、时间测试
t标记可以检查在任何时间内Web服务器执行的情况。
下面我们使用以下命令来模拟10个用户在20秒的时间内同时访问网站:
ab -c 10 -t 20 http://www.example.com/
注:该命令不包含n标记,但默认情况下包含该标记并且使用t选项时ab将其值设置为50000。某些情况下,使用t选项时,最大请求数可能会达到50000,此时模拟结束。
ab命令响应如下图
此模拟的结果指出,当10个并发用户在20秒的时间内请求Web文档时性能会下降。最快速的满足请求花费了464毫秒,而最慢速的请求花费了1890毫秒。
五、ab陷阱
使用ab时有几点警告。
1、再看看上面执行的命令,就会注意到在域名的结尾有一个反斜杠。如果你不请求该域中的特定文档,则这个反斜杠是必需的。
2、ab还可能会由于其传递给Web服务器的用户代理值而被某些Web服务器阻止,因此在这些情况下,可能收不到任何数据。如果要解决这个问题,请使用可用的选项标记之一-H,来提供你的请求中自定义浏览器标头信息。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.
