PHP achieves performance optimization

王林
Release: 2023-04-08 08:12:02
forward
2435 people have browsed it

PHP achieves performance optimization

Problem Background

After developing a page, how to make the page run faster and better is what distinguishes a programmer's technical level and An important indicator of field of view. So during the interview, the interviewer will always ask you a question, how to optimize performance?

What is performance optimization?

From a front-end perspective, performance optimization can be divided into two directions. From the user's perspective, one is that the page loads quickly, and the other is that the page is smooth to use. Therefore, the exploration of performance optimization can be divided into two directions: page loading time and page running efficiency.

(Free learning video tutorial sharing: php video tutorial)

How much time does it take from the browser opening to the completion of page rendering

Browser parsing->Query cache->dns query->Establish link->The server processes the request->The server sends a response->The client receives the page->Parse HTML-> Build the rendering tree->Start displaying content (white screen time)->First screen content is loaded (first screen time)->User interactive (DOMContentLoaded)->Loading completed (load), if we want to load To optimize time, we need to think about and summarize every step here, and avoid piecing together a little bit here and there.

Page load time monitoring

Before optimizing these links, we need to know how much time is spent monitoring these links.

You can get a lot of data related to page loading. The more commonly used ones are

DNS resolution time: domainLookupEnd - domainLookupStart

TCP connection establishment time: connectEnd - connectStart

白Screen time: responseStart - navigationStartdom

Rendering completion time: domContentLoadedEventEnd - navigationStart

Page onload time: loadEventEnd - navigationStart

If you do not use this API, you can use the time returned by server rendering or the time when the SPA route jumps away as the starting point, and domContentLoaded, load and other events as the end point for recording. Or go directly to Google Analytics.

Key points of server part optimization

The back-end part can optimize cache, dns query time, link time, request processing time, response time, etc.

Dns query time can be optimized using httpdns or dns preloading, domain name convergence and other means.

The focus of establishing a connection is long connection and link reuse, keep-alive, long-polling, http-straming, websocket or other protocols you have written yourself. It is better to use http2 directly. In order to optimize the linking process, the front-end also needs to use CDN, sprite images, code merging and other methods for resources.

There are many points that can be optimized in server processing requests. It is worth noting that when the mobile terminal accesses the PC page and needs to jump to the mobile page, use 302 jump on the server side and do not do it on the front end. Jump. There is also the option of enabling hsts, which requires the browser to use https for subsequent visits, reducing unnecessary http jumps to https. It can also prevent SSL stripping attacks and improve security.

When the server sends a response, you can use Transfer-Encoding=chunked to return responses multiple times. For specific operations, check bigpipe. There are also ways to reduce the size of cookies and so on.

Recommended related articles and tutorials: php tutorial

The above is the detailed content of PHP achieves performance optimization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template