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:
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?
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();
}
Is this a good way to do it?
Comment