I wanted to check the upload speed of the system.
void CheckUploadSpeed()
{
using (var wc = new WebClient())
{
IPv4InterfaceStatistics ipis = networkInterface.GetIPv4Statistics();
BytesSentb4Upload = ipis.BytesSent;
FileStream stream = File.OpenRead(string.Format("{0}speedtext.txt", path)); //speedtext.txt is a 5 MB file.
var fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
startTime = Environment.TickCount;
wc.UploadDataAsync(new Uri("http://www.example.com/"), fileBytes);
InternetSpeedResult = "Data upload started. Uploading 5MB file";
wc.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
wc.UploadDataCompleted += wc_UploadDataCompleted;
}
}
Upload Progress Changed
void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
InternetSpeedResult = string.Format("Checking Upload Speed ... ");
double endTime = Environment.TickCount;
double secs = Math.Round(Math.Floor(endTime - startTime) / 1000, 0);
if (secs >= 30)
{
UploadComplete(sender, e);
}
}
This code actually is serving my issue but the problem is that this is not giving the exact results everytime. Since I am counting on Complete BytesSent
in the particular period of time. This numbers automatically varies. If the Speed is very low (less than 512 KBPS) and very high (Greater than 20 MBPS) than its not giving the expected upload rate.
I would not say this as a solution whereas it would be small workaround which might help you to get rely on your upload speed.
Increase the size of the uploaded file to somewhere around 100MB or so. Set the secs >= 60
Now whatever the speed of the network is if it low it would check for a minute and let u know the upload speed. If it is higher. Then also it would let u know either all 100 MB is transferred and you will get the upload speed and if not than you you'll get to know the speed in 60 seconds.
Since working with high speed internet the problem is that the bandwidth is not optimum while the transaction begins. as soon as you start sending or receiving data it will increase the speed thus Changing it to 60 seconds and increasing the file size will give you results in both the cases.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments