asp.net implementation of the calculation of Web page download speed Code _ Practical skills

Source: Internet
Author: User
Tags datetime

Copy Code code as follows:

private void GetSpeed ()
{
Link Start time
DateTime stime = DateTime.Now;
File
String url = "Yun_qi_img/test.jpg";
WebRequest myrequest = webrequest.create (URL);
Link successful
WebResponse myreponse = Myrequest.getresponse ();
Gets the size byte unit of the file
int II = Int. Parse (MyReponse.ContentLength.ToString ());
Get stream
Stream MyStream = Myreponse.getresponsestream ();
StreamReader sr = new StreamReader (mystream);
Byte[] Mbyte=new Byte[ii];
int allbyte= (int) MByte. Length;
int startbyte=0;
String test = "";
while (ii> 0)//################ reads the file and displays the progress ....
{
int M=mystream.read (mbyte,startbyte,allbyte);
if (m==0) {break;}
Startbyte+=m;
Allbyte-=m;
int a1= (int) startbyte/1024;
int a2= (int) ii/1024;
test+= "Connection succeeded ... Start the download.   m= "+m+" | " + A1. ToString () + "/" + A2. ToString () + "KB";//startbyte + "/" + II. ToString ();
}
Link End time
DateTime Etime=datetime.now;
TimeSpan ts = etime-stime;
Total time consuming
Double speedtime = (double) ts. TotalSeconds;
Double Kbps = 0;
Double showper = 0;
if (speedtime> 0)
{
Network speed
Kbps = Math.Round (Math.Round (ii*8/1024/speedtime*10.5)/10);
Kbps = Math.Round (ii/1024/speedtime);
}
Else
{
Kbps = 10000;
}
The length of the picture to display for the current flow
Showper = Math.Round (kbps/100);
if (Showper <1)
Showper = 1;
else if (showper> 82)
Showper = 82;
Network speed
String sp = (Math.Round (kbps/8*10)/10). ToString ();
Sr. Close ();
Mystream.close ();
Myreponse.close ();
Response.Write (test);
Response.Write ("Network speed" +sp+ "Picture length $" +showper+ "time-consuming $" +speedtime+ "File size $" +ii);
Response.End ();
}

Finally, a netizen gives another idea to realize:

Use a page to output a 3M page (through a loop output byte), and then asynchronously to request the page, calculate the start time and end time difference, and then to calculate.

The idea is still the same, is to use WebRequest to get the flow file is just to get response time, but not to output time (their level is limited, but I think it can be achieved). Now it's just a different way to get the output time. I am in Baidu and Google is only to find the asynchronous get text content output time can. But there are pictures that seem to be a little difficult.

      But the end result was a bit of a sense of achievement. Oh...

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.