Table of Contents
前言
系统环境
安装与配置
1.mongodb
2.tideways
安装
配置
3.xhgui
安装(假设在/home/wwwroot/目录下执行如下命令)
4.应用配置
最终成功配置并采集到数据的界面
Home Backend Development PHP Tutorial How to build a performance testing environment for php7

How to build a performance testing environment for php7

Oct 24, 2017 am 09:11 AM
php php7 environment

前言

我之前使用的是xhprof+xhgui分析线上环境的性能,然而PHP版本升级到PHP 7之后,xhprof已经不可用,于是改用tideways+xhgui,这实际上也是PHP7下开源方案的唯一选择,有兴趣的可看下参考资料,有详细说明。

本文主要根据参考资料[1]配置,因此会有大量重复的地方,我主要其基础上根据实际生产环境的要求多添加了以下额外配置:

  • mongodb只绑定到本地

  • xhgui开启HTTP Basic认证

  • xhguimongodb中只保留最近14天的数据

系统环境

CentOS 7.3 + nginx + mysql + php71

本文假设你的lnmp环境已经可以正常使用,并且是通过源码安装PHP,现在只是需要添加性能测试的功能。如果你不熟悉lnmp环境的配置,推荐使用https://lnmp.org/提供的一键安装包,本文的配置路径均基于该包的默认配置。

安装与配置

分成以下几个部分:

  1. mongodb

  2. tideways

  3. xhgui

  4. 应用配置

1.mongodb

安装

#yum install mongodb-server mongodb -y
#pecl install mongodb
Copy after login

启动mongodb服务

#mongod --bind_ip 127.0.0.1
Copy after login

2.tideways

安装

git clone https://github.com/tideways/php-profiler-extension.git
cd php-profiler-extension
phpize
./configure --with-php-config=`which php-config` 
make
sudo make install
Copy after login

配置

编辑php.ini文件,添加:

extension=tideways.so
tideways.auto_prepend_library=0
Copy after login

重启php-fpm,执行以下命令看到tideways的输出表示有生效:

#php -m | grep tide
tideways
Copy after login

3.xhgui

xhgui也是一个网站,最终需要通过web访问。官方版本是英文版,已经不更新了,有很多BUG,这里推荐使用中文版:https://github.com/maxincai/xhgui

安装(假设在/home/wwwroot/目录下执行如下命令)

$ git clone https://github.com/maxincai/xhgui.git
$ cd xhgui
$ php install.php
Copy after login

配置

1.给数据库添加索引,非必须,但是强烈推荐:

$ mongo
 > use xhprof
 > db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
 > db.results.ensureIndex( { 'profile.main().wt' : -1 } )
 > db.results.ensureIndex( { 'profile.main().mu' : -1 } )
 > db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
 > db.results.ensureIndex( { 'meta.url' : 1 } )
Copy after login

2.nginx配置(xhgui本身没有安全机制,它捕捉的数据中有敏感数据,因此开放到外网后必须开启HTTP Basic认证

创建/usr/local/nginx/conf/vhost/xhgui.conf文件,内容如下:

server
{
    listen 8888; # 根据实际情况改成自己的端口
    server_name 127.0.0.1; #根据实际情况改成自己的域名
    index index.html index.htm index.php;
    root  /home/wwwroot/xhgui/webroot/;

    location ~ \.php
    {
        auth_basic "xhgui needs authentication"; # 开启HTTP Basic认证
        auth_basic_user_file htpasswd;  # 密码文件
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }

    location ~ .*\.(js|css)?$ {
        expires      30d;
    }

    access_log  /home/wwwlogs/xhgui.access.log;
    error_log  /home/wwwlogs/xhgui.error.log;
}
Copy after login

开启HTTP Basic认证需要生成密码文件htpasswd。假设生成一个tester用户,密码为123456,则执行以下命令:

printf “tester:$(openssl passwd -crypt 123456)\n" >> /usr/local/nginx/conf/htpasswd
Copy after login

生成后记得检查下文件内容,格式内容应该类似如下:

$cat /usr/local/nginx/conf/htpasswd 
tester:1qe8kAN82iOyo
Copy after login

完成配置重启,在浏览器中进入http://127.0.0.1:8888,应该能看到界面了,只是此时还没有数据。

3.进一步优化配置

xhgui 默认是按1%采集的,可是如果是排查问题时还是希望能够100%采集会比较方便。进入xhgui源码目录,修改config/config.default.php文件,平时仍然按1%的采样率采样,防止数据增长过快,当想调试时,就在URL中添加debug=1的参数即可。

config/config.default.php中,找到profiler.enable这里,按如下修改:

'profiler.enable' => function() {
    // url 中包含debug=1则百分百捕获
    if(!empty($_GET['debug'])){
        return true;
    } else {
        // 1%采样
        return rand(1, 100) === 42;
    }
},
Copy after login

如果不删除采集的数据,很快就会发现mongo数据库变得很大。因此推荐配置下mongo数据库,只保留最近14天的数据。

#mongo
> use xhprof
> db.results.ensureIndex( { "meta.request_ts" : 1 }, { expireAfterSeconds : 3600*24*14 } )
Copy after login

如果想手动全部删除,则执行如下命令:

$ mongo
$ use xhprof;
$ db.dropDatabase();
Copy after login

4.应用配置

让应用实现采集,需要修改对应的nginx配置文件,添加:

fastcgi_param TIDEWAYS_SAMPLERATE “100"; #是否采样取决于xhgui的随机数配置和这里的采样率配置,两者必须同时满足,这里简单设置成100,由xhgui去控制
fastcgi_param PHP_VALUE "auto_prepend_file=/home/wwwroot/xhgui/external/header.php";
Copy after login

完整的nginx示例配置文件如下:

server
{
    listen 80; #根据实际情况修改
    server_name test.dev; #根据实际情况修改
    index index.html index.htm index.php;
    root  /home/wwwroot/test/web/;

    location ~ \.php
    {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index /index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param TIDEWAYS_SAMPLERATE "100”; # 此处为重点
        fastcgi_param PHP_VALUE "auto_prepend_file=/home/wwwroot/xhgui/external/header.php”; # 此处为重点
        include fastcgi_params;
    }

    try_files $uri $uri/ @rewrite;
    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }

    location ~ .*\.(js|css)?$ {
        expires      30d;
    }

    access_log  /home/wwwlogs/test.access.log;
    error_log  /home/wwwlogs/test.error.log;
}
Copy after login

最终成功配置并采集到数据的界面

How to build a performance testing environment for php7


The above is the detailed content of How to build a performance testing environment for php7. For more information, please follow other related articles on the PHP Chinese website!

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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles