Cloud Wisdom Neeke
One. PHP Application Scenarios
Since its inception in 1994, PHP has evolved from Petty's "Personal Home Page Tools" to "Php:hypertext preprocessor", based on strong scalability and agile iterative features, Basically has become the Internet technology company's necessary language, to promote the development of the Internet to provide a steady flow of powerful power.
PHP-based open source software and development frameworks (excellent, such as Wordpress,zend framework,laravel,yaf,hiphop, etc.) are also evolving, making PHP massively used by companies other than internet companies, And go to the Enterprise Application development Language list.
Two. User Monitoring requirements
We know that PHP's low threshold iterations quickly make a lot of projects, for various reasons become architecturally ambiguous:
1. Insufficient developer level;
2. The project is taken over from the outsourcing team;
3. Historical issues, Humpty;
4. Poor performance, anyway, has become increasingly uncontrolled;
and to discover problems that are not or are difficult to debug for a variety of reasons, such as:
1. Time is too long, I also forgot how to write, to see the code
2. Because the data is not built, the logic can not be reached, can not reproduce
3. Project is not my team development, architecture and code, shy disgusting.
Figure 1:php Execution model of application system
The running phase of PHP can be roughly divided into three phases:
1. Parse
2. Compile
3. Execute
The compile process will produce OP code and class Table,function Table, which is then handed to execute for final execution. Op code is an intermediate code that is executed by the Zend engine call.
It is not difficult to see, in fact, PHP and Java are similar, are generated in the middle code, running on their own "virtual machine", but why PHP performance than Java,. NET, go is so different?
In a word, it is generally agreed that "PHP is interpreted language" is not rigorous, PHP is not not compiled, but every execution is compiled, in addition to the most serious problem is more difficult to achieve parallel operations (note that the "more difficult", using PHP for Parallel computing scheme more than one). Based on the existence of OP code, a large number of cache tool extensions have been created, which can effectively improve the performance of PHP applications, such as Opcache,apc,apcu,xcache and so on.
Where is the bottleneck point of performance for Web sites, interfaces, and applications that are developed with PHP? Those of you who have done performance optimizations for PHP applications know that recursion, looping, resource manipulation, resource release, and so on are common bottlenecks that often result in blocking or locking.
It can be concluded that the consumption of CPU, memory, various I/O, various network bandwidth is the most serious point in the performance bottleneck, we can simply fall into the following: external services (such as third-party APIs), resource reading and writing, code exceptions.
The general approach to dealing with these issues is to use tools such as Xhprof,xdebug or php-trace to identify them and to work with architects or senior engineer experiences, including Singleton, transactional, on-demand loading, short transactions, timely release, etc. (for most companies and developers, The problem with performance is more of a blind guess and scratching. But what are the shortcomings of these methods?
At a glance, it can only be done in the test or production environment, causing the problem and making it clear. The test environment is fine, but if it is a production environment, the post-processing can be remedied, but most of the complaints are due to functional impairment or more serious business losses.
Be able to detect or circumvent possible performance problems in the first time of production, and accurately record known or unknown fault sites. These two points, it becomes the urgent demand of application performance management.
Three. Perspective Treasure PHP Monitoring Implementation principle
Fig. 2 The Hook application signal
PHP running support Zend Engine early in the design process has reserved a rich hook, can effectively interfere with the process of several key steps.
Cloud Wisdom Perspective Bao phpagent Research and development implementation, of course, can not affect the application of the original code, then the simplest and most effective way is to implement a PHP extension, phpagent use the following several hooks:
1. Zend_compile_file & zend_compile_string
Loading the parse file or string itself will result in very large IO, which, if executed too much, will undoubtedly result in memory and CPU consumption. With these two hooks, you can get the file name, the number of rows executed, the memory used, and the CPU usage time.
2. Zend_execute & Zend_execute_internal
Through the use of these two hooks, we can accurately analyze a PHP application in the class call, method calls, method parameters, memory consumption and CPU usage, to analyze, you can accurately draw the application system running process of the method run stack, API call address, SQL statement, Cache Key information, such as keys and cache hits.
3. Zend_throw_exception_hook
With the exception hooks, we can get the abnormal information in the operation of the application system accurately, including the class \ Method location, parameter, exception code and exception message.
4. ZEND_ERROR_CB
Error hooks are more straightforward and can accurately get any warning, code errors or syntax errors that occur during system operation.
Figure 3 Phpagent Registration and application hook process
Fig. 4 application schematic of Zend_execute Hook (pseudo code)
Roughly explains how we use the Zend_execute hook for data acquisition: First obtain the method name, and through the Rule filter engine, determine which is our attention or not attention (class \ Method of black and white list), and then through the Agent_begin macro record method start time, method name, Number of rows, memory start and parameters, after performing the original Zend_execute, and then through the Agent_end macro record the end time, memory stop point.
Through the application of the above hook, we can already get the data of our attention: class \ Method execution order, execution time, memory consumption, interface \db and other resource connection, SQL statement and execution time, etc.
After rigorous stress testing and production environment deployment practices, Phpagent has an impact on the performance of legacy application systems within 5%. After some parameter leveling, the performance impact can be reduced to a lower level, including:
1. Whether to start the exception hook
2. Whether to start the error hook
3. Whether data sampling is enabled
4. Whether to enable stack tracking
5. Whether to use the black and white list (Url\header\cookie\request Params)
6. Whether to enable the UDP send Agent
7. Focus on request time response thresholds
8. Focus on method Time response thresholds
Four. Perspective Bao phpagent Monitoring deployment process
The perspective Bao Phpagent complies with the Smartagent plug-in specification, so everything starts with the deployment of the Smartagent installation.
1, Login Cloud Wisdom Perspective Bao Official website: https://www.toushibao.com/, click on the top right of the page navigation "free trial", correctly fill out the free trial application information will pop up the following dialog box, while the activation of the mail will be automatically sent to your mailbox, follow the process to register an account.
2. After successful registration, login to the perspective treasure, click Configure-Application , download and install the smart Agent in the configuration page. After the installation is successful, the Smart agent automatically obtains the host information based on the system configuration, and after approximately two minutes, you can view the server's CPU, memory, network card, disk, and process performance data in the "host → server" module.
3, if you want to monitor the application run time code, host services and database performance data, you need to further install and configure the various plug-ins provided by the Smart agent, because the smart agent implements an open plug-in structure, for each running time code, The monitoring of the service and the database is implemented by the corresponding plug-in.
3. After the installation is complete, the Smart agent automatically discovers your application components during the loading process, and you can add phpagent manually if you do not automatically monitor the PHP environment. As shown, click "Manage" portal, go to "plugin management", click "Add Service" at the bottom of the page, select Phpagent, then click "Create".
When you are finished, click "on". (The on operation simply initializes the user's information so that the information collected later can be correctly passed back to the user.) )
4. Of course, if the phpagent is already installed, it can be easily upgraded and degraded directly on this management interface.
5. The Setup process script uses the default whereisphp Find in-system PHP for installation .
If you compile and install PHP, assign the Appd_php_path variable value in the script to the PHP bin directory, such as:/usr/local/php-5.5.14/bin.
Executes the phpagent/install.sh start installation command.
6. Restart Web Server
After installing the PHP code monitoring plugin, you need to manually restart the Web Server (APACHE\HTTPD\PHP-FPM and other software)
7. View PHP Application Data
Congratulations! Now that the configuration is complete, you can view the app data in the Apps module if the app has normal access.
Five. Perspective Treasure PHP Monitoring Features
In the functional aspect, the perspective treasure whether in PHP, or other such as Java,. NET and other mainstream language monitoring, including: see the execution of the slowest 10 elements, including the number of elements executed, duration, and the percentage of elapsed time; View the HTTP request parameters, including the response status of the request, the link page, the specific request parameters and the return result; View detailed tree-like information for the code execution stack, Including the calculation time of each method, the total time and the number of calls, you can directly see the slowest method of special identification, and see the total time-consuming ordering of SQL statements, including the total time spent on SQL execution, the number of executions and the specific query statements; Third-party API calls.
is an overview of the resource topology and request response for an application discovered by Phpagent.
A hash chart describes the request response time distribution for a PHP application over a period of time, which can be seen at a glance for problematic requests.
The topology and code run stack for a single request transaction can be analyzed accurately:
The following is an analysis of the operation of MySQL resources for a period of time discovered by Phpagent in an application.
Based on the application of a certain cluster, the perspective treasure can automatically identify and depict the total topology.
Of course, the identified application site topology can be grouped highlighting.