Making PHP websites run faster --- how to optimize PHP

Source: Internet
Author: User
Tags server array
Article Title: Making PHP websites run faster-how to optimize PHP. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
PHP is a programming language that runs very quickly, but it is worth optimizing PHP itself than just optimizing code.
  
This article will explain why optimizing PHP is more appropriate than optimizing code based on some actual results, and why you need to understand how to locate the bottleneck and fix it based on the performance of other subsystems related to PHP on your server. At the same time, we also talked about how to optimize your PHP code to make it faster.
  
   High Performance
When talking about good performance, it often doesn't just mean how fast your PHP code is executed. Performance is a balance between quantifiable evaluation and speed. Code execution with less resources may be slower than the Code with rows in the cache, and the same group (executed in the cache) code can be concurrently executed on a Web server.
In the following example, A. php is regarded as A runners who can run as fast as possible, while B. php is A marathon who can run almost at the same slow speed forever. Under light loads, A. php can be sufficiently fast, but when the traffic increases, B. php's performance will only decrease A little while A. php will collapse.
  
Let's use a fact to verify the more profound essential meaning of this statement. Suppose we need to read a K file and generate an HTML Summary of the file. We wrote two scripts to do the same thing: hare. php will read the entire file to the memory at a time, and then execute it in place step by step; and tortoise. php reads only one row of a file at a time and never exceeds the content capacity. As a result, Tortoise. php is much slower because multiple reads and writes require more system responses.
Each time the program runs, hare. php requires 0.04 seconds of CPU execution time and 10 MB of memory, while tortoise. php requires 0.06 seconds of CPU execution time and 5 MB of memory. The server has a total memory capacity of 99% MB and of its CPU is idle. We also assume that executing such a simple event does not generate memory fragments.
When 10 programs run concurrently, the memory overflow (10 × 10 = 100) occurs in hare. php ). At the same time, tortoise. php will still have 50 MB of free memory available! Concurrent execution of 11 programs will enable hare. php is completely "broken" because it starts to use virtual memory-the execution speed may be lower than half of its normal speed, And now each individual program process needs 0.08 seconds of CPU execution time. During this period, tortoise. php still runs at its regular CPU execution time-0.06 seconds!
In the following table, PHP scripts that run faster are separated by bold:
Bytes ------------------------------------------------------------------------------------------
| Connections | CPU execution time per HTTP Request (in seconds) | CPU execution time per 10 HTTP requests (in seconds) | CPU execution time per 11 HTTP requests (in seconds) |
Bytes ------------------------------------------------------------------------------------------
| Hare. php | 0.04 | 0.04 | 0.88 (memory overflow) |
Bytes ------------------------------------------------------------------------------------------
| Tortoise. php | 0.06 | 0.60 | 0.66 |
Bytes ------------------------------------------------------------------------------------------
As you can see in the above example, to achieve better performance is not just to write and execute faster PHP programs. High-performance PHP requires a good understanding and understanding of underlying hardware knowledge, operating systems, software support, such as Web servers and databases.
  
   Bottleneck
The above two examples show the bottleneck of (performance) decline. Hare. php is always faster than tortoise. php when it has an infinite amount of memory. However, it seems too simple to think that the memory is the bottleneck of PHP's overall performance-actually far more than this:
  
(A) Network
  
Your network may be the biggest bottleneck. If you have 10 Mbps of bandwidth, you can only get 1 Mbps of transmission speed. If each PHP page is 30 k, only 33 pages are transferred per second, which will make your network bandwidth saturated. More factors that cause bottlenecks include frequent access to low-speed DNS, or limited storage for network devices.
  
(B) CPU
  
If you monitor your CPU load, sending a pure static HTML page will not increase the CPU load-as we mentioned above, the bottleneck lies in the network. Of course, for complex dynamic pages generated by PHP, your CPU speed will naturally become one of the constraints. A server or a server array with multiple CPUs can reduce the impact on the CPU.
  
(C) shared storage
  
Shared storage mainly plays a role in communication processes, or is used to store public resources (such as cache data or code between multiple CPUs ). If the shared storage is not allocated enough, the bottleneck also occurs.
  
(D) File System
  
The access speed to the hard disk is at least 50 to 100 times slower than the direct read memory. Using High-speed cache will improve the situation. However, the low memory space will apply to the reduction of File Buffer Memory (usually known as virtual memory) space, leading to a reduction in overall performance. The virtual memory is prone to a large number of file fragments, resulting in a reduction in the access speed to the hard disk. Using a large number of link tags on Unix systems also reduces the access speed to hard disks. The default hard disk access settings in the default Linux installation mode are also not optimistic, because they are for compatibility rather than speed. If necessary, use the hdparm command to change the Linux hard disk configuration.
  
(E) Process Management
  
Creating a new process on some operating systems (such as Windows) is very slow. This means that CGI applications related to process creation on these operating systems will be much slower. In this case, running PHP in multi-process mode will improve. (Note: the old version of PHP does not support the multi-process mode)
Avoid too many invalid processes on your server. For example, if your server is a pure Web service, you need to disable (set to not install) X-Windows window system. In Windows, Microsoft quick search (part of Office) and third-party screensaver programs should be disabled (100% CPU usage is usually required ).
Some programs you can consider to delete include some less commonly used network protocols, mail services, virus scanning, (mouse, infrared port) drivers and so on. On Unix systems, if you access your server through SSH, consider removing the following:
Telnetd, inetd, atd, ftpd, lpd, sambad Module
Sendmail module for receiving emails
NFS ing table
Xfs, fvwm, xinit, X
You can also disable different startup programs by modifying the startup script (the path is usually in/etc/init * or/etc/rc */init.
You can also check whether the scheduled task processing is reasonable and whether the task can be executed during idle time on the server.
  
(F) connect to other servers
If your server needs services provided by other servers, this may cause bottlenecks for other servers. The most common is that a database server that executes complex queries very slowly serves many other servers.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.