asp.net implementation of the calculation of web page download speed code

  • 2020-05-30 19:50:22
  • OfStack


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 to success  
      WebResponse   myReponse   =   myRequest.GetResponse(); 
      // Gets the size of a file in bytes  
      int   ii   =   int.Parse(myReponse.ContentLength.ToString()); 
      // Obtains the flow  
      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)     //################        Loop read file , And show 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+= " The connection is successful .. Start the download ..m= "+m+ "| "   +   a1.ToString()   +   "/ "   +   a2.ToString()   +   "KB ";//startbyte   +   "/ "   +   ii.ToString(); 
      } 
      // Link end time  
      DateTime   etime=DateTime.Now; 
      TimeSpan   ts   =   etime   -   stime; 
      // The 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; 
      } 
      // Used to display the length of the image to be displayed 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+ " The length of the picture $ "+ShowPer+ " Time consuming $ "+SpeedTime+ " The file size $ "+ii); 
      Response.End(); 
      }

Finally, a netizen gave another idea to achieve 1:

Output a 3M page with 1 page (output bytes through 1 loop), then request the page asynchronously, calculate the difference between the start time and the end time, and then calculate again.

The same idea is to use webRequest to get the stream file and only get the response time, but not the output time (my level is limited, but I think it can be achieved). Now I just changed a method to get the output time. I also only found the output time of asynchronously obtaining text content in baidu and google. But it's a little bit more difficult with pictures.

But the final result got, still have a little achievement feeling. Hehe...


Related articles: