Some thoughts on the high performance of the website

Source: Internet
Author: User
Tags flock

Recently has been doing a new online business projects, in all the pre-design, medium-term writing and later on-line completion, open to the user when the use of the site to find that the speed is not as fast as you think! So after a lot of research and commissioning, finally the speed achieved our expectations.

Here are some of the information I've collected over the one months and some of my own thoughts, and I'll do some more recording.

A. From the browser to the server to initiate the request to open:

(1). Problems that have just started:

At the beginning of this slow problem, I was thinking that it should be compressed JS or CSS Some code, or add some memory, add a bit of cache, or even think of it should add more

Wide, anyway is all can mention high performance all should go to do again, finally is cram, did not catch the most fundamental core problem is what, cause the problem solves not so easy.

Ideas to be considered:

In fact, the first step from the browser to the server to request this entire process to analyze bottlenecks, the first need to understand the next HTTP request process:

According to the HTTP protocol, any HTTP request is processed as follows: (PS: This paragraph is reproduced)

1. The browser makes a request to the server. At this point the browser generally appears in the lower-left corner: Requesting ...

2, the server calls the application after the request (Php,java, etc., determined by the suffix name) execution. When the server receives the request, it will return a header message to the browser, which will be displayed at the bottom left corner of the browser: connected

To the server (the server receives the request)

PS: If the speed is fast, the bottom left corner of the browser is a flash of information. If the network is delayed, the information will always appear in the lower-left corner.

3, Php,java and other code execution completed

4, after the execution of the code, the server will send an HTTP header response browser ( that is, tell the browser you start to receive data, ready ).

This stage of the browser is not received this header information, the lower left corner will show : waiting for the server response ...

5, the browser to the server sent to the response header, began to receive data, the browser is receiving data status,

We will see that the lower left corner of the browser shows: Transferring data ..... That's the state.

6, the transfer of data end, the completion of an HTTP processing, the entire process of HTTP processing end.

Browser Summary The bottom left corner of the state is: the request is connected to the server "waiting for the response" Server has responded to the "transmitting data" began rendering data

In fact, we can analyze from these six stages, in the end is that block is relatively slow, generally speaking:

The first stage: is requesting, this stage if slow, may consider whether the bandwidth is the reason, is not the server room is not domestic cause, such as China to the United States, optical cable is through the Atlantic submarine cable into

Lines of communication

The second phase: already connected to the server, indicating that the server (Apache or Nginx) has started to call the relevant PHP or Java module for processing, there should be no performance impact!

      The third stage : Wait for the response, at this time if the speed is too slow, it should be paid attention to, may be their own program written on the problem, need to benchmark the corresponding code block, the most simple method is

Use the Microtime_float function to

To test (PS: The basic usage of this function is in the PHP manual), at the beginning of the code, put a time, the end of the time to put a period, and then two time subtraction is the time spent, specific

The code is as follows:

1 $begin _time= Microtime_float ();//execution start record timestamp, accurate to milliseconds2  3 /*the code to execute in the middle*/4  5End_time =microtime_float ();6  7 $exec _time=$end _time-$begin _time;//Execution Time Calculation8  9@save_stat ($exec _time);//write execution time to a text fileTen   One   A /*Here are the two defined functions*/ - functionSave_stat ($time) - {   theStatic $call _count= 1;//How many times does the statistic call -$call _limit= 10;//How many times limit attempts when encountering concurrency problems -if(!$time)return ; -$exec _stat_file= "./exec_stat_file.txt";$fp=fopen($exec _stat_file, ' AB '); +if(Flock($fp,lock_ex)) -     { +$s= ' Access: '.Date("Y-m-d h:i:s"). ', Execute time: '.$time.‘ s,request_url:http://'.$_server[' Http_host '].$_server[' Request_uri ']. "| |".Php_eol; Afwrite($fp,$s); at Flock($fp,lock_un); -}Else{ -Usleep(1000); -if($call _count<$call _limit) -         { -$call _count++; inSave_stat ($time); -         } to     } +       -@fcolse ($fp); the//Var_dump ($fp); *       $ }Panax Notoginseng   - functionmicrotime_float () the { +List($usec,$sec) =Explode(" ",Microtime()); Areturn((float)$usec+ (float)$sec); the}
View Code

Note: This time contains the database of SQL execution time, if it is more than 2 seconds, I think it is a bit slow, it is necessary to carefully analyze the code here, is not supposed to loop the use of the loop, is not the algorithm

Not used very accurately?

This is a procedural aspect, if this is not a problem, we are looking at the database is not the SQL write a problem, put each SQL into the database to execute once, statistics on the time spent, is not necessary to

One-step optimization, how to optimize? This piece is

A big topic, this piece of record later, here is just a chat about the thinking of things.

PS: When we do the test, we need to test a few times, and finally find an average, perhaps due to some non-human factors, the test results may not be so accurate, so it is necessary!

In fact, this stage is the most prone to problems, because this piece involved in programming problems, database design problems, these are our location to the problem, we need to refine a direction!

Four, five stages: that if the previous three is not a problem, to this stage, the description is transmitting data, if the execution is slow, consider whether the bandwidth is the reason, should be added bandwidth such a problem!

The sixth stage: after the browser gets the data, start rendering the data, this phase of execution slow, consider whether the front end with too many plug-ins, resulting. (PS: When we go online this project, all the previous steps

It's not a problem, it's just a matter of time

, rendering data became particularly slow, and finally found that we used too many third-party plug-ins, which is also time-consuming cost.

      

The above six stages, just a rough overall analysis, we are doing debugging, I think it is necessary to have this approximate path, in accordance with this way to slowly chase down, always find the bottleneck of the problem where

In

I just started tuning, think of really a lot of, but did not grasp the bottleneck of the problem where, just to optimize a bit of light itching place, resulting in speed has not come up.

When we are optimizing, what I think needs to be done is:

      find the site's current performance bottlenecks where, to solve the bottleneck, other aspects of the subtle impact of speed, Is also very small. If you don't find the bottleneck, To solve the subtle, not the main, contradictions. but because the bottleneck is not solved, Whole

speed is not fast .

      

In fact, throughout the process, the program, just a small part of the optimization, in the database optimization, is really to improve the overall speed, so the next record of the database level optimization!

      

Some thoughts on the high performance of the website

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.