search
HomeBackend DevelopmentPHP Tutorial高性能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引用所指向的位置。
现在,打开\Apache2.2\bin。应该可以看到如下图所示文件和目录的集合。

四、使用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
执行上面命令的结果如下图所示:



URLhttp://www.example.com的ab响应上图的数据分为4个部分
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命令响应如下图




www.example.com/的基准测试结果(20秒内10个并发用户)
此模拟的结果指出,当10个并发用户在20秒的时间内请求Web文档时性能会下降。最快速的满足请求花费了464毫秒,而最慢速的请求花费了1890毫秒。


五、ab陷阱
使用ab时有几点警告。
1、再看看上面执行的命令,就会注意到在域名的结尾有一个反斜杠。如果你不请求该域中的特定文档,则这个反斜杠是必需的。
2、ab还可能会由于其传递给Web服务器的用户代理值而被某些Web服务器阻止,因此在这些情况下,可能收不到任何数据。如果要解决这个问题,请使用可用的选项标记之一-H,来提供你的请求中自定义浏览器标头信息。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version