/* Style Definitions */ table.MsoNormalTable {mso-style-name: ordinary form; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}
Chapter 1VIP website deployed in the IDC computer room, it will feel very slow. What causes this? In order to shorten the response time of the page and improve our user experience, we need to know what users spend their time waiting for.
You can track our login page, as shown in the figure belowWe can analyze it from the above picture,
HTMLThe document only accounts for 20% of the total response time, and the other 80% response time is used to download JS, CSS, images, etc. components. Therefore, the WEB front-end has a lot of room for optimization. We will comprehensively consider the two aspects of WEB’s front-end optimization and back-end optimization to give a WEB performance optimization plan. .
HTTP Requests
There are several common methods to effectively reduce
, Request: 1Merge scripts and style files, for example, you can combine multiple CSS files into one , combine multiple JS files into one. 2
、CSS Sprites Use CSS background Related elements to perform absolute positioning of the background image,Combine multiple images a picture. 2.
Use browser cache When users browse different pages of the website, a lot of content is repeated, such as the same JS, CSS, pictures, etc. If we could suggest or even force browsers to cache these files locally, it would significantly reduce the amount of traffic generated by the page, thereby reducing page load times. According to the server-side responseheader, a file has several different levels of cache status for the browser. 1. The server tells the browser not to cache this file and to update the file on the server every time. 2. The server does not give any instructions to the browser. 3. In the last transmission, the server sent Last-Modified or Etag data to the browser. When browsing again, the browser These data will be submitted to the server to verify whether the local version is the latest. If it is the latest, the server will return the 304 code, telling the browser to use the local version directly, otherwise download the new version. Generally speaking, if there are and only static files, the server will give these data. 4. The server forces the browser to cache files and sets an expiration time. Before the cache expires, the browser will directly use the local cache file without any communication with the server. What we have to do is to try to force the browser to the fourth state, especially for JS, CSS, pictures and other changes that are relatively large Fewer files. Use compression components and Firefox browsers support clientGZIP, before transmission, use GZIP to compress and then transmit it to the client. After the client receives it, it will be decompressed by the browser. Although this slightly takes up some CPU of the server and the client, it will change What comes is higher bandwidth utilization. For plain text, the compression rate is quite impressive. If each user saves 50% of bandwidth, then the rented bandwidth can serve twice as many customers and shorten the data transmission time. images and JS The easiest way to preload images is to JavaScript Instantiate a new Image() object, and then pass in the URL of the image to be loaded as a parameter. function preLoadImg(url) {
var img = new Image();
img.src = url;
}
You can preload JS and pictures on the login page
5.Put the script at the bottom
The problems caused by placing the script at the top,
1,Use script , progressive rendering will be blocked for content located below the script
2,Parallel downloads will be blocked while downloading the script
Putting it at the bottom may cause JS errors. When the script is not loaded, the user triggers the script event.
Consider the situation comprehensively.
6.Place the style file at the top of the page
If the style sheet is not loaded, building the rendering tree is a waste. There may be two situations when the style file is placed at the bottom of the page:
1, White screen
2, Flashing of unstyled content
7.Use external JS and CSS
turn the inline JS and CSS into external JS, CSS. Reduce repeated downloading of inline JS and CSS.
8. Split components into multiple domains
Enter the URL address or click on a link of URL
2. The browser parses the IP address # corresponding to
URLbased on the URL address, combined with DNS ## 3. Send HTTP request
4. Start connecting to the requested server and request related content
5. The browser parses the content returned from the server and displays the page.
The above is basically the basic process of a page from request to implementation. Below we will Analyze this process.
When you enter URL, the browser will know this URLWhat is the corresponding IP? Only by knowing the IP address can the browser prepare to send the request to the specified server. Specifically IP and port number. The browser's DNS parser is responsible for parsing URL into the correct IP address. This parsing process takes time, and during this parsing time period, the browser cannot download anything from the server. Browsers and operating systems provide DNS resolution caching support. After obtaining the IP address, the browser sends a HTTP request to the server. The process is as follows :
1. The browser requests the server to open the connection by sending a TCP packet
2. The server also responds to the client's browser by sending a packet, telling the browser that the connection is open.
3. The browser sends a GET request of HTTP. This request contains a lot of things, such as our common cookie and other headHeader information.
In this way, a request is sent.
After the request is sent, it is the server's business. The server-side program sends the final result to the client.
In fact, the first thing to reach the browser is the documents of html. The so-called documents of html are pure htmlCode does not include any pictures, scripts, CSS, etc. That is, the html structure of the page. Because what is returned at this time is only the html structure of the page. The time it takes for this html document to be sent to the browser is very short, generally accounting for about 10% of the entire response time.
After this, the basic skeleton of the page is in the browser. The next step is the process of the browser parsing the page, which is a step-by-step analysis from top to bottom## The skeleton of #html is gone.
If the img tag is encountered in the html document at this time, the browser will send a HTTP request to this The URL address of the img response is used to obtain the image and then present it. If there are many pictures in the html document, flash, then the browser will request them one by one and then render them. If each picture needs to be requested, then it must be done before The steps mentioned: parse url, open tcp connection, etc. Opening a connection also consumes resources. Just like when we are accessing a database, we try to open as few database connections as possible and use more connections in the connection pool. For the same reason, tcp connections can also be reused. http1.1 proposed the concept of persistent connection (persistent connection), which means that the same HTTP connection can handle multiple requests at the same time, reducing tcpConnection.
When the html skeleton of the page is loaded,the browser begins to parse the tags in the page,from Start analyzing from top to bottom.
The first is the parsing of the head tag. If it is found that there is a JS script to be quoted in head, then The browser begins to request the script at this time, and the parsing process of the entire page stops until the JS request is completed. After that, the page is parsed downwards, such as parsing the body tag. If there is an img tag in body, the browser will request img##. #srcThe corresponding resource, if there are multiple img tags, then the browser will parse them one by one, and the parsing will not wait like JS, Will download concurrently.
The above is the detailed content of Detailed introduction to Web performance optimization solutions. For more information, please follow other related articles on the PHP Chinese website!