0

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;
        }

    }
  • Please be more specific. What did you expect to happen? Why did you expect that to happen? What happened instead? Also: `1)` The length of streams and files is always given in bytes, not bits, so you don't need to multiply by 8. `2)` Why do you open `fileToUpload` but check the length of `filepath`? `3)` Why convert `Length` to string, just to parse it back to `int`? Casting would be better. `4)` In general, longer files tend to use the network more efficiently than shorter ones, so it _is_ possible to see different transmit speeds depending on length. – Peter Duniho Oct 30 '14 at 23:26
  • Hi Peter. I want to know is it possible to calculate the upload speed to a remote machine through this calculation. – Ajay Unnikrishnan Oct 31 '14 at 08:13
  • 2
    That depends on your definition of "upload speed", but sure...it is possible to perform that simple computation, dividing bytes sent by the time it took, and get some representation of throughput _for that particular observation_. That may or may not provide more generally useful information. – Peter Duniho Oct 31 '14 at 08:16
  • Is there any way to find time taken for upload? – Ajay Unnikrishnan Oct 31 '14 at 08:44
  • Sorry, I don't understand the question. The code you posted seems to already do this. The best way is to use the `System.Diagnostics.Stopwatch` class. But the code you posted uses `DateTime` values, subtracting the start time from the end time. That will work fine most of the time, especially if you change to use `DateTime.UtcNow` instead of `DateTime.Now`. – Peter Duniho Oct 31 '14 at 17:14
  • Is there any way to get the time taken from the FTP response? It will be helpful in the case of download test also – Ajay Unnikrishnan Nov 01 '14 at 06:18
  • 1
    I don't know that you'll be able to get the timing at such fine granularity. One of the problems with using higher level abstractions like WebRequest/Response is that a lot of the I/O activity is hidden from you. You can time the overall transaction, but it doesn't seem to me that the object gives you sufficient detail of the actual transaction elements to break the time down into upload versus response. You'd have to handle the I/O yourself at the `Socket` level, I think. – Peter Duniho Nov 01 '14 at 06:27

0 Answers0