Home php教程 php手册 学习PHP精粹,编写高效PHP代码之性能

学习PHP精粹,编写高效PHP代码之性能

Jun 13, 2016 am 09:34 AM
php

一、采用基准测试,检测代码的性能

基准测试涉及Web应用程序时,通常指“压力测试”,即在你的代码中尽可能多地加载流量,然后衡量它的执行能力。

推荐两种基准测试的工具:ApacheBench(ab)和JMeter。

要进行压力测试,我们需要两个东西:同时在线的用户和大量的请求。使用这些工具后,很多同时运行的应用程序线程便代表了用户。因此我们只需记住:并发线程=并发用户。

1、ApacheBench超级简单,通常包含了Apache安装,或是作为Apache开发包的一部分——一个被称为简单ab的二进制文件。要使用ab,只需指定请求的总数(-n),以及并发线程的数量(-c),然后让它开始工作。

例如:我们在这里使用-n 1000 -c 20生成20个并发线程执行1000个请求。

$ ab -n <span>1000</span> -c <span>20</span> http:<span>//</span><span>example.org/</span>
Copy after login

ab使用参考:http://httpd.apache.org/docs/2.0/programs/ab.html

2、JMeter是另一个具备GUI的Apache项目,而且具备更多功能。若要使用JMeter,你需要创建一个测试计划,添加线程组、添加采样器、指定JMeter的配置、添加Cookie处理器这样的其他选项、增加监听器处理结果。

JMeter网站:http://jmeter.apache.org/

二、利用缓存提高代码性能

1、对于Apache服务器,利用apc实现代码缓存。

从PECL(PHP Extension Community Library,PHP扩展共享类库)中获取APC进行编译,然后安装该扩展。

$ pecl install apc
Copy after login

在这之后,根据设置,需要编辑php.ini文件并添加它:

extension = apc.so
Copy after login

重新启动Apache,然后就可以了。

apc使用参考:http://www.php.net/manual/en/book.apc.php

2、对于Windows/IIS服务器,利用微软的WinCache实现代码缓存。

WinCache网站:http://www.iis.net/downloads/microsoft/wincache-extension

3、利用memcached实现会话数据缓存,memcached是基于内存的、群集友好的键/值对存储。如果你启用memcached扩展,就可以自动使用memcached代替磁盘存储回话。

memcached网站:http://memcached.org/

memcached使用参考:http://www.php.net/manual/zh/book.memcached.php

安装memcached:

$ pecl install memcache # Install ext/<span>memcache
$ memcached </span>-d -m <span>128</span> # Start memcached
Copy after login

设置php.ini:

session.save_handler = <span>'</span><span>memcache</span><span>'</span><span>
session.save_path </span>= <span>'</span><span>tcp://localhost:11211</span><span>'</span>
Copy after login

 

三、进行程序概要分析,寻找问题在哪里?

程序概要分析(profiling)是采用精确的时间或内存检测代码运行每个动作的行为。通过分析,找到问题的位置,然后进行优化。

我们有两个常用的profiling工具:

1、由Derick Rethans编写的可靠的Xdebug工具,并由KCachegrind或QCachegrind审核结果。

Xdebug网站:http://xdebug.org/

KCachegrind网站:http://sourceforge.jp/projects/freshmeat_kcachegrind/releases/

QCachegrind网站:http://sourceforge.jp/projects/freshmeat_kcachegrind/releases/

2、新开发的XHProf工具,是来自Facebook的一个应用,由Paul Reinheimer编写XHGui Web前段部分。

XHProf网站:http://pecl.php.net/package/xhprof

XHGui网站:https://github.com/perftools/xhgui

 

总结:

首先我们要解决性能下降这个最大的难题,这样便可获得整体性能的更好提升。如果一个SQL查询花费10秒,而你将它的执行速度提高了50%,这样你为自己节省了5秒;然而,如果执行一个PHP函数花费5秒,你同样将它的执行速度提高了50%,你实际上却只节省了半秒钟。在某些时候,你将受到硬件性能的绝对限制,以我们的经验你更有可能受到磁盘或网络I/O的限制,而不是CPU或RAM的限制。这时你需要开始在多台计算机上缩放应用程序。

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles