Calculate data rate problem

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.

+4
source share
3 answers

How I do it: Keep all bytes transferred long.

Then every 1 second I check how much was transferred. So I just run the code only to save speed right after the second second. Your while loop will loop for a few seconds on maaaaaaaaaaaaany in a fast network.

Depending on the speed of your network, you may need to check the bytes transferred in a separate stream or function. I prefer to do this with a timer, so I can easily update the user interface

EDIT: From your look at your code, I assume what you are doing wrong is that you do not take into account that one while (true) loop does not take 1 second

EDIT2: Another advantage when only a speed check happens right after the first second is that everything will be much faster. In cases like this update, the user interface may be the slowest thing you do, so if you try to update the interface of each loop, it is most likely your slowest point and will create an unresponsive user interface.

It’s also true that you should evaluate values, so you won’t get minutes of the Microsoft protocol . I normally do this in a Timer function, doing something like this:

 //Global variables long gTotalDownloadedBytes; long gCurrentDownloaded; // Where you add up from the download/upload untill the speedcheck is done. int gTotalDownloadSpeedChecks; //Inside function that does speedcheck gTotalDownloadedBytes += gCurrentDownloaded; gTotalDownloadSpeedChecks++; long AvgDwnSpeed = gTotalDownloadedBytes / gTotalDownloadSpeedChecks; // Assumes 1 speedcheck pr second. 
+3
source

There are many levels of software and hardware between you and the system you are sending, and some of these layers have a certain amount of free space.

When you first start sending, you can pump out the data pretty quickly until you fill these buffers, but in fact they do not reach the end, but quickly! After you fill out the send buffers, you are limited to adding more data to them at the same speed as flushing, so the speed that you see will decrease to the base send speed over the network.

+2
source

That's all, I think I fixed my problem by setting a variable of 5-10 variables to wait one second to take into account the surge, but not the best, but it will allow the Internet to figure it out and let me fix a smooth transition.

It seems that my network traffic is rightly broken, so there is nothing in the code that could otherwise do to stop it.

Please continue to be interested in more answers before I hesitate to accept my own.

0
source

All Articles