search
HomeBackend DevelopmentPHP TutorialInstallation and use of XHProf (PHP performance testing artifact)

XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮。XHProf安装和使用方法将在本章讲解。XHProf是PHP的PECL扩展。没有XDeBug那些耗费资源,更加的小巧。 流程:程序开头打点,结尾打点。那么XHProf机会记录在两个点之间的所有代码响应时所耗费的时间、内存、CPU等各项指标,我们也可以知道一次请求调用了多少次MySQL,多少次Memcache,更加直观的指明优化道路。 安装: <precourier new white-space:pre-wrap padding:9.5px margin-top:0px margin-bottom:10px line-height:1.42857 word-break:break-all word-wrap:break-word border:1px solid rgb background-color:rgb>------------下载并编译PHP-XHProf源码------------ wget http://pecl.php.net/get/xhprof-0.9.4.tgz tar -zxvf xhprof-0.9.4.tgz cd xhprof-0.9.4 cd extension phpize ./configure --enable-xhprof make make test sudo make install ------------修改php.ini--------------- sudo vim /etc/php.ini #在php.ini最下方加入以下: extension=xhprof.so xhprof.output_dir="/var/www/xhprof" -----------重启Apache-------------- sudo apache restart Enter the installation package folder you just decompressed, and copy xhprof_lib and xhprof_html to the project directory. Next, create a header file head.php, which is the beginning of the two dots: <precourier new white-space:pre-wrap padding:9.5px margin-top:0px margin-bottom:10px line-height:1.42857 word-break:break-all word-wrap:break-word border:1px solid rgb background-color:rgb>//head.php <?php if(extension_loaded('xhprof')){ //载入下载的XHPROF包中的2个文件夹 include_once 'xhprof_lib/utils/xhprof_lib.php'; include_once 'xhprof_lib/utils/xhprof_runs.php'; xhprof_enable(XHPROF_FLAGS_CPU &#43; XHPROF_FLAGS_MEMORY); } . Then create a bottom file foot.php, which is the last of the two dots: //foot.php <?php if(extension_loaded('xhprof')){ $ns = 'myXhprof'; //关闭profiler $xhprofData = xhprof_disable(); //实例化类 $xhprofRuns = new XHProfRuns_Default(); $runId = $xhprofRuns->save_run($xhprofData, $ns); //前端展示库的URL $url = 'http://localhost/xhprof_html/index.php'; $url .= '?run=%s&source=%s'; //变量替换 $url = sprintf($url, $runId, $ns); //输入URL echo '<a href="'.%24url.'" target="_blank">查看结果</a>'; } . The last step: dotting. Now we create a test file index.php. Test my big Hello World. <precourier new white-space:pre-wrap padding:9.5px margin-top:0px margin-bottom:10px line-height:1.42857 word-break:break-all word-wrap:break-word border:1px solid rgb background-color:rgb>//index.php <?php include_once 'head.php'; echo 'Hello World'; include_once 'foot.php'; You can see that in http://localhost/index.php, the bottom is the "View Results" we wrote in foot.php. Click in and you can see a list of all functions used in this request. , the time, CPU, Memory and other information consumed by each function can be sorted according to the selection by clicking on the first column. Click [View Full Callgraph] to see the flow chart generated by this list. From the entry point to which function, to which function, which function this function calls, how many times this function calls Memcache, etc., it is clear. Reduce MC calls, reduce this, reduce that, can the response speed of requests be faster? Tip: I have 1,000 files. Now I need to use XHProf to check my entire project. Do I need to add include at the beginning and end of each file? Add: auto_prepend_file = /var/www/head.php auto_append_file = /var/www/foot.php in php.ini or add php_value auto_prepend_file = /var/www/head.php php_value auto_append_file = /var/www/foot.php in .htaccess. Error: 1. When clicking [View Full Callgraph] to view the picture, an error is reported: failed to execute cmd: "dot -Tpng". stderr: `sh: dot: command not found`. Reason: Reason: The graphical tool is not installed Solution: //红帽系列 yum install graphviz //Ununtu apt-get install graphviz //OS X brew install graphviz

The above introduces the installation and use of XHProf (PHP performance testing artifact), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 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.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

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

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor