Code for calculating the download speed of web pages implemented by asp.net

Source: Internet
Author: User

Copy codeThe Code is as follows:
Private void getSpeed ()
{
// Link Start Time
DateTime stime = DateTime. Now;
// File
String url = "http://xxx.com/images/test.jpg ";
WebRequest myRequest = WebRequest. Create (url );
// Link successful
WebResponse myReponse = myRequest. GetResponse ();
// Obtain the object size in bytes
Int ii = int. Parse (myReponse. ContentLength. ToString ());
// Obtain the 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) // ################ read the file cyclically and display 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 successful .. start to download .. m = "+ m +" | "+ a1.ToString () +"/"+ a2.ToString () +" KB "; // startbyte +"/"+ ii. toString ();
}
// Link end time
DateTime etime = DateTime. Now;
TimeSpan ts = etime-stime;
// Total time consumption
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 (I/1024/SpeedTime );
}
Else
{
Kbps = 10000;
}
// Display the image length for the current traffic
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 + "image length $" + ShowPer + "Time consumed $" + SpeedTime + "file size $" + ii );
Response. End ();
}

Finally, some netizens gave another idea of implementation:

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

The idea is still the same, that is, using webRequest to get the stream file only gets the response time, but the output time is not allowed (my own level is limited, but I think it can be implemented ). Now I just changed the method to get the output time. In baidu and google, I can only find the time for Asynchronously obtaining text content output. However, it seems a little difficult to have images.

However, the final result is still somewhat rewarding. Haha...

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.