This article introduces my general approach to analyzing performance bottlenecks on websites.

1, browser review elements, shut off the cache Settings, such as to view the request of the time, although the open a website will load a lot of things, but some of them are asynchronous loading, on the front page rendering and load at the same time, other links, namely the browser parallel in sending a request, so the whole some special long loading time, may not affect the user experience. The test uses disable cache, which allows browsers to cache resources more quickly.

Lazyload can be done for pictures and other files, which is not slow for users’ intuitive feelings. Of course, it will be better if CDN is added. CSS, JS these files are best compressed, plus CDN will be faster.





A network request from Jane

2. Find time-consuming web pages or interface requests and check them through charels or Console in Chrome.

3, look at the source code, and then find the SQL to see if it needs to be optimized, I personally will add the time calculation code in the code, to compare which section of code time is more serious. The usual approach to query optimization for SQL is to index or cache data. Mysql > query optimization

4. Multi-threaded programming model. Traditional PHP websites do not have multi-threading. If the modification is made on traditional PHP websites, part of the business can be split out and forwarded to Swoole through Nginx, and the overall query time can be saved by swoole’s asynchronous operation.

5, asynchronous programming model, for time-consuming processing can be put into the task queue, simple can directly use redis queue data type to do the task queue, complex can use MQ framework, such as RocketMQ, ZeromQ. Want to save trouble can directly buy Ali Cloud MQ service.

6. For data with low update frequency, it can be put into Redis to cache and provide query speed. It is faster than taking data directly from the database, depending on the business situation. If you find that some data update frequency is more frequent, you do not need to save the database and save REDis, you can use redis first, and then asynchronously at an interval of time to synchronize to the database. Mysql is mainly used to store data permanently, while Redis is used to store hot data.

7, when doing some PHP performance tests, to obtain the code execution time and memory consumption, check the data, found PHP has a built-in function to achieve this function, the specific implementation code is as follows:


$t1 = microtime(true);

/ /… Execute code…

$t2 = microtime(true);

Echo round($t2-$t1,3).

echo ‘Now memory_get_usage: ‘ . memory_get_usage() . ‘

‘;

8. Load balancing for heavy traffic access is generally divided into 7 and 4 layers. For HTTP requests, you can use nginx upstream to do 7 layer load balancing. For layer 4 can be done with LVS or haproxy, in fact, using nginx tcp_proxy module can also do layer 4 load.

9, big data split, generally will do sub-table sub-library, may be the business code to do direct modification. You can also use the middleware Mycat.


Performance optimization is not something that will be encountered at the beginning of the project. Second, with the online operation of the project, some special situations will be encountered and special analysis will be conducted to find appropriate solutions. Just as there is no silver bullet, performance optimization also needs to find appropriate ways.