我正在创建一个Windows应用程序来监控我的服务器。我想知道托管我的Web应用程序的服务器的上传和下载速度。我使用FTP来检查速度,因为HTTP和FTP都使用TCP连接。下面定义了上传速度的计算。有没有其他方法来检查上传速度?听说可以通过监控nic卡找到上传速度。等待建议继续.. 是否可以使用(totalDataSent / TotalTimeTaken)方法进行计算。
但速度因文件大小而异。 请给出你的建议
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;
}
}