Calculating upload speed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Calculating upload speed?

    I'm making a quick little ftp uploader as a test and I'm trying to track upload speed. I'm using a loop to write 2048 bytes at a time to the ftp stream, so my logic was to run the loop a predefined number of times and count how long it took to do that number of loops. I'm running the loop 32 times, so I'm uploading 64 kilobytes, then I calculate how many milliseconds it took to upload that amount. I'm just stuck at what to do next. Let's say, for example, that it takes me 250 milliseconds to upload 64 kilobytes (an unrealistic number, but a simple one), I know then that 1/16th of a megabytes takes me 1/4th of a second to upload. So since 1000/250 = 4 (1000 being the number of milliseconds in a second), I would take that 4 and multiply it by 64, the number of bytes I'm uploading per calculation to get 256 Kbps.

    I dunno if this is right though...becaus e my program is returning values like 900 for the number of milliseconds per loop. Here is the code I'm using to time everything:

    Code:
    Stream strm = ftp.GetRequestStream();
    contentLen = fs.Read(buff, 0, buffLength);
    totalUploaded += contentLen;
    DateTime startTime = DateTime.Now;
    DateTime endTime;
    TimeSpan duration;
    while (contentLen != 0)
    {
        if (count == 32)
        {
            endTime = DateTime.Now;
            count = 0;
            duration = endTime - startTime;
            double inASec = (double)1000 / (double)duration.Milliseconds;
            uploadSpeed = Math.Round((double)64 * inASec, 2);
            startTime = DateTime.Now;
        }
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
        percentDone = Math.Round((totalUploaded / finfo.Length) * 100, 2);
        textBox1.Text = "Uploaded " + totalUploaded + "/" + finfo.Length + " bytes  -  (" + percentDone + "%, " + uploadSpeed + "Kbps)";
        totalUploaded += contentLen;
        progressBar1.PerformStep();
        count++;
        Application.DoEvents();
    }
    So I basically set my start time, run the loop 32 times then set my stop time. Reset my count to 0, take a timespan duration and get the milliseconds, do my calculations, then reset the start time again.

    Is this a good way to do it?
  • harrisunderwork
    New Member
    • Feb 2010
    • 7

    #2
    You need to consider that ISP speed is kilo bits per second and not kilo bytes per second. So what is 64 kilobytes for you is actually 64 kilobits. Now dividing 900 / 8 = 112.5 kilo bytes per second and that is pretty much achievable. :)

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      I want to report the kilobyes per second. I just used thed math above and it ended up working great, I get about 150kbps, but it only updates once every 16 loops, so once every 32 kilobytes. I guess the loop is too fast for one duration, and I can't figure out how to use ticks. I'd like to have it update the speed once per loop, like the percent.

      Comment

      Working...