C # - calculate download / upload times using network bandwidth

As a feature in an application I am developing, I need to show the total estimated time remaining before the file is uploaded / downloaded to / from the server.

how to get the speed of upload / download to the server from the client machine.

I think if I can get the speed then I can calculate the time by ->

for example --- for a 200MB file = 200 (1024 kb) = 204800 kb and divide it by 204800 Mb / speed Kb / s = "x" seconds

+2


source to share


6 answers


Try something like this:



int chunkSize = 1024;
int sent = 0
int total = reader.Length;
DateTime started = DateTime.Now;
while (reader.Position < reader.Length)
{
    byte[] buffer = new byte[
        Math.Min(chunkSize, reader.Length - reader.Position)];
    readBytes = reader.Read(buffer, 0, buffer.Length);

    // send data packet

    sent += readBytes;
    TimeSpan elapsedTime = DateTime.Now - started;
    TimeSpan estimatedTime = 
        TimeSpan.FromSeconds(
            (total - sent) / 
            ((double)sent  / elapsedTime.TotalSeconds));
}

      

+2


source


Upload / download speed is not a static property of the server, it depends on your particular connection and can also change over time. Most of the apps I've seen do the assessment in a short amount of time. This means they start uploading / downloading and measuring the amount of data, say 10 seconds. It is then taken as the current baud rate and used to calculate the remaining time (eg 2500 kB / 10 s → 250 kbps). The time window is moved and recalculated continuously so that the accuracy of the calculations corresponds to the current speed.



While this is a fairly straightforward approach, it will be useful in most cases.

+11


source


This is only for tangent, but my guess is that if you try to calculate the total time remaining, you will probably also show it as a kind of progress bar. If so, you should read this article by Chris Harrison on Perceptual Differences. Here's the conclusion straight from his article (emphasis mine).

The behavior of the different lanes appears to have a significant impact on the user's perception of the length of the process. By minimizing negative behavior and incorporating positive behavior, progress bars can be efficiently created and associated processes run faster . In addition, if the elements of a multi-step operation can be rearranged, it may be possible to reorder the steps in a more pleasing and seemingly faster sequence.

http://www.chrisharrison.net/projects/progressbars/ProgBarHarrison.pdf

+3


source


I don't know why you need this, but I would go the simplest way and ask the user what type of connection they have. The file size then divides it by speed and then by 8 to get the number of seconds.

Period - you don't need computing power to calculate velocities. Microsoft uses a feature on its website that calculates the default speed for most connections based on the file size you can get when you download the file or manually enter it.

Again, perhaps you have other needs and need to calculate the load on the fly ...

0


source


The following code calculates the remaining time per minute.

    long totalRecieved = 0;
    DateTime lastProgressChange = DateTime.Now;
    Stack<int> timeSatck = new Stack<int>(5);
    Stack<long> byteSatck = new Stack<long>(5);

    using (WebClient c = new WebClient())
    {
        c.DownloadProgressChanged += delegate(object s, DownloadProgressChangedEventArgs args)
        {
            long bytes;

            if (totalRecieved == 0)
            {
                totalRecieved = args.BytesReceived;
                bytes = args.BytesReceived;
            }
            else
            {
                bytes = args.BytesReceived - totalRecieved;
            }

            timeSatck.Push(DateTime.Now.Subtract(lastProgressChange).Seconds);
            byteSatck.Push(bytes);

            double r = timeSatck.Average() * ((args.TotalBytesToReceive - args.BytesReceived) / byteSatck.Average());
            this.textBox1.Text = (r / 60).ToString();

            totalRecieved = args.BytesReceived;
            lastProgressChange = DateTime.Now;
        };

        c.DownloadFileAsync(new Uri("http://www.visualsvn.com/files/VisualSVN-1.7.6.msi"), @"C:\SVN.msi");
    }

      

0


source


I think I have an estimated time to load.

double timeToDownload = ((((totalFileSize/1024)-((fileStream.Length)/1024)) / Math.Round(currentSpeed, 2))/60);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { 
Math.Round(currentSpeed, 2), Math.Round(timeToDownload,2) });

      

Where

private void UpdateProgress(double currentSpeed, double timeToDownload)
    {

        lblTimeUpdate.Text = string.Empty;
        lblTimeUpdate.Text = " At Speed of " + currentSpeed + " it takes " + timeToDownload +"   minute to complete download";
    }

      

and the current speed is calculated as

TimeSpan dElapsed = DateTime.Now - dStart;
if (dElapsed.Seconds > 0) {currentSpeed = (fileStream.Length / 1024) / dElapsed.Seconds;
                                }

      

0


source







All Articles