PHP 网站开发中网站性能分析
PHP 开发中的外围资源性能分析
首先,后端外围资源,是指跟 PHP 运行过程中与语言本身无关的网络与 IO 操作、存储服务、中间件代理、缓存和数据库访问等,在本文中,我们先分析 IO 操作和中间件服务。
为什么外围资源的性能分析,要以以上三者分析为主?我们可以看如下国内专业的性能监控工具 OneAPM 的 PHP Web 应用后台截取下来的总览图,通过这个图可以看到,数据库所花费的时间在总 PHP 响应时间中,占据着 60% 甚至更大的比重,而 Memcached 缓存服务,在这张图里所占的响应时间,几乎看不见。
一、IO 操作
PHP 语言本身尽管有性能的差异,但是从对 PHP 的性能微观分析也可以看出,如果只执行单次操作,实际中这种差别是非常小的,前面的实验中,十万次以上操作,才有百 ms 级的差别,因为 PHP 语言本身操作的是内存,一次内存访问,大约在 50ns 左右。而 IO 操作,则是磁盘访问,一次磁盘访问所费时间在 5ms 以上。仅从这个数量级看是 10 万倍的差距,实际上,根据实验,也有百倍级的差距(顺序访问和随机访问差距巨大,实际中两者同时进行,还会有磁盘缓存等)。
所以对比语言本身,IO 成为瓶颈的可能性更大。首先看一下,IO 操作带来的性能差别。
一个 PHP 脚本,通过 PHP 命令方式运行
当使用如下命令清空磁盘缓存后:
echo 3 | sudo tee /proc/sys/vm/drop_caches代码一模一样,但是运行时间却是正常运行时间的 6 倍。当然这个时间的慢,并不仅仅是由于程序本身的 IO 操作导致,而更大的慢的因素是在 CGI 模式下,PHP 脚本的每一次运行都需要加载所有模块,这个加载,也伴随着大量的 IO 操作。
再做一个实验,完全同样功能的两个页面,一个采用了 MVC 的方式,把头部,尾部拆开成独立的模板(并未使用模板引擎),中间逻辑也使用独立的 Model 类来处理。另一个只 require 了宏定义和数据库操作两个文件。
使用命令ab -c 40 -n 1000 http://xxxxx/0929/zuche/carlist.php 进行压力测试,
在这个页面中,MVC 版本所费时间要多 6-8ms 左右。虽然只是多增加了几个文件包含,但是明显增加了请求延时,如果文件操作本身更加复杂,比如文件上传、检测、转换,则延时会增加一个数量级以上。在实际的生产使用中,也不是说有了文件操作,就一定会产生大的延时,因为就像本例的 require 而言,由于磁盘缓存等的存在,延时的影响已降低很多。
二、中间件代理
在正式使用中间件之前,我们先对比一下,使用数据库与不使用数据库的差别,同样是上面的这个例子,我们把数据结果集,从数据库获取转换成为直接的结果数组设置,为了结构化清楚,采用 MVC 这一版。同时为了更显著对比上一轮测试结果,同时也消除语言本身的一些慢的因素,在本轮实验中,我们采用 PHP7,得到结果是令人吃惊的。 如下图是带有数据库连接和数据读取的版本,PHP 扩展使用的是 mysqli。
由于本页面,只有一次数据库操作,页面结构也比较简单,语言本身的影响因素非常大,PHP7 下速度有两倍以上提升,原来平均响应时长为 37-40ms,现在则为 14ms。
即使如此,不读取数据库时,有 4ms 的差距,尽管数目上不大,但是对于一个总响应时长只有 14ms 的应用,这 4ms 已经很显著了,而这只是一个数据库查询操作。
接下来看一下,当增加一层数据库中间件时,效率又有怎么样的变化呢?由于笔者所使用的中间件,目前并不支持 PHP7,所以我们还在老版 PHP 的基础上来比对。在同样的服务器压力下,使用了中间件的版本慢了一倍以上。如下图所示。
从这个例子可以看出来,原本 PHP 直接连数据库,取得数据的操作,增加了中间件之后,变了先到中间件,中间件再到数据库,返回亦如是,导致了速度的大幅度下降(这里已经剔除了中间件本身占用资源的因素,在原来直连的版本是 37-40ms 左右)。
这里也请读者不要误解,演示中间件使用速度下降的例子,并不是说为了说明中间件不好,在分布式环境下,使用中间件是非常必要的。而是说,程序的外部资源,往往是影响性能的重要因素,尤其是当外部资源的连接和数据获取本身速度达不到理想的结果时。
对于 IO 操作和中间件服务的分析就到这里,下篇将分析数据库给整个应用性能带来的影响。

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











As we all know, optimizing the SEO of a website is a very important part of website operation. The default URLs of dynamic web systems (such as PHP) used by many websites have extensions (.php, .html, etc.), which will affect the SEO effect of the website. In order to improve the optimization effect of the website, a common practice is to change the dynamic URL to a pseudo-static URL to hide the extension name and improve the user experience and search engine ranking of the website. This article will take "pseudo-static hidden php suffix" as the theme, introduce how to achieve this optimization in PHP websites, and

Websites developed with PHP include: 1. Facebook, one of the world's largest social media platforms; 2. Wikipedia, the world's leading online encyclopedia; 3. WordPress, a widely used open source blog and content management system; 4. Twitter, a popular social media platform; 5. Yahoo, a world-renowned portal; 6. Flickr, an online image hosting and sharing platform; 7. LinkedIn, a professional social media platform.

Empire cms template development methods include: 1. Understand the template structure; 2. Modify the template code; 3. Use tags and variables; 4. Create custom functions; 5. Use CSS and JS; 6. Use template modules; 7. Debugging and test.

Websites made with PHP include: 1. Facebook, one of the world's largest social media platforms; 2. Wikipedia, an online encyclopedia edited by users around the world; 3. WordPress, a popular open source blog and content management system; 4. Slack, a widely used team collaboration tool; 5. Magento, a powerful open source e-commerce platform; 6. Etsy, an online marketplace for handicrafts and independent designers.

Building a PHP website involves the following steps: 1. Prepare the environment, install the PHP development environment and create the website root directory. 2. Create the basic file structure, including the homepage index.php and other required files. 3. Write home page content, use HTML and PHP to build page structure and dynamically generate content. 4. Add other pages and create other PHP files to process page content. 5. Connect to the database (optional), use PHP to connect to the database and operate the data. 6. Design styles and interactive effects, and use CSS and JavaScript to enhance the beauty and interactivity of the website. 7. Deploy the website and upload the website files to the server or

How to protect your PHP website using secure coding practices? With the popularity and development of the Internet, more and more websites use PHP as the development language. However, PHP's flexibility and ease of use also make it vulnerable to various security threats. Therefore, when developing a PHP website, it is essential to apply secure coding practices to protect the website from possible attacks. This article will introduce some ways to protect your PHP website using secure coding practices and provide corresponding code examples. Input Validation and Filtering Input Validation and Filtering is the key to protecting PHP websites

How to use encryption algorithm to protect user data of PHP website? With the rapid development of the Internet, user data protection of websites has become more and more important. In PHP development, we can use encryption algorithms to protect the security of user data. This article will introduce some commonly used encryption algorithms and how to use them in PHP websites to encrypt user data. 1. Selection of encryption algorithms For PHP websites, we can choose the following commonly used encryption algorithms to protect the security of user data: 1. Symmetric encryption algorithm: This algorithm uses the same encryption algorithm.

How to improve the access speed of PHP website by compressing files? Abstract: With the development of the Internet, website access speed has become more and more important. In PHP websites, compressing files is a common method to speed up. This article will introduce how to improve the access speed of PHP website by compressing files, and give corresponding code examples. 1. Why can compressing files improve access speed? In the process of browsing the web, the browser needs to download and load various files required by the web page, including HTML, CSS, JavaScript files, etc.
