I am creating a windows app to monitor my servers. I want to know the upload and download speed to the server to which hosted my web application. I am using FTP to check the speed because both HTTP and FTP uses TCP Connection. Below defines the calculation of the upload speed. Is there any other way to check the upload speed? Heard that upload speed can be found out by monitoring nic card. Waiting for advices to continue.. Is it possible to calculate using (totalDataSent/ TotalTimeTaken) method.
But the speed varies with file size. Pls give your advice
private string UploadSpeedCheck()
{
FileStream rdr;
FtpWebRequest request;
FtpWebResponse response;
Stream requestStream;
try
{
//Upload
DateTime start = new DateTime();
DateTime end = new DateTime();
rdr = new FileStream("C:\\test\\temp2.txt", FileMode.Open);
request = (FtpWebRequest)FtpWebRequest.Create(url);
request.Credentials = new NetworkCredential(serverUserName, serverPassWord);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.KeepAlive = false;//Do not keep alive (stateless mode)
requestStream = request.GetRequestStream();
byte[] inData = new byte[rdr.Length];
int bytesRead = rdr.Read(inData, 0, int.Parse(rdr.Length.ToString()));
start = DateTime.Now;
requestStream.Write(inData, 0, int.Parse(rdr.Length.ToString()));
requestStream.Close();
response = (FtpWebResponse)request.GetResponse();
end = DateTime.Now;
response.Close();
double sizeInKb = (rdr.Length * 8) / 1024;
TimeSpan ts = end.Subtract(start);
double speed = sizeInKb / ts.TotalSeconds;
string unit = "Kbps";
if (speed > 1000)
{
speed = speed / 1024;
unit = "Mbps";
}
return "Upload Speed : " + speed.ToString("0.0") + " " + unit;
}
catch (Exception e)
{
return "Test failed";
}
finally
{
rdr = null;
request = null;
response = null;
requestStream = null;
}
}