I executed a file transfer rate calculator to display kB / sec for the download process taking place in my application, however with the following code it seems that I get “spikes” in my KB / s readings right after the file starts with upload.
This is part of the code of my stream, this is a stream of files in 1024 pieces to the server using httpWebRequest:
using (Stream httpWebRequestStream = httpWebRequest.GetRequestStream()) { if (request.DataStream != null) { byte[] buffer = new byte[1024]; int bytesRead = 0; Debug.WriteLine("File Start"); var duration = new Stopwatch(); duration.Start(); while (true) { bytesRead = request.DataStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; httpWebRequestStream.Write(buffer, 0, bytesRead); totalBytes += bytesRead; double bytesPerSecond = 0; if (duration.Elapsed.TotalSeconds > 0) bytesPerSecond = (totalBytes / duration.Elapsed.TotalSeconds); Debug.WriteLine(((long)bytesPerSecond).FormatAsFileSize()); } duration.Stop(); Debug.WriteLine("File End"); request.DataStream.Close(); } }
Now the log of the output of the download process and the related readings kB / sec looks like this: (You will notice that a new file starts and ends with "File Start" and "File End")
File Start 5.19 MB 7.89 MB 9.35 MB 11.12 MB 12.2 MB 13.13 MB 13.84 MB 14.42 MB 41.97 kB 37.44 kB 41.17 kB 37.68 kB 40.81 kB 40.21 kB 33.8 kB 34.68 kB 33.34 kB 35.3 kB 33.92 kB 35.7 kB 34.36 kB 35.99 kB 34.7 kB 34.85 kB File End File Start 11.32 MB 14.7 MB 15.98 MB 17.82 MB 18.02 MB 18.88 MB 18.93 MB 19.44 MB 40.76 kB 36.53 kB 40.17 kB 36.99 kB 40.07 kB 37.27 kB 39.92 kB 37.44 kB 39.77 kB 36.49 kB 34.81 kB 36.63 kB 35.15 kB 36.82 kB 35.51 kB 37.04 kB 35.71 kB 37.13 kB 34.66 kB 33.6 kB 34.8 kB 33.96 kB 35.09 kB 34.1 kB 35.17 kB 34.34 kB 35.35 kB 34.28 kB File End
My problem, as you will notice, the “explosion” that I am talking about starts at the beginning of each new file, reaching the MB peak, and then aligns correctly. is it ok to boot so that it pops up? The download speed will usually not be higher than 40 fps, so it may not be right.
This is a real problem, when I take the average over the last 5-10 seconds to display on the screen, it really throws things, producing a result of about ~ 3 MB / s!
Any ideas if I approach this issue in the best way? and what should I do ?: S
Graham
Also: Why can't I do " bytesPerSecond = (bytesRead / duration.Elapsed.TotalSeconds) " and move the duration. Start and duration. End the while loop and get accurate results? I would have thought it would be more accurate? Each speed reads as 900 bytes / s, 800 bytes / s, etc.