Search code examples
performancesocketstransferrate

calculate sending file speed/sec in less than a second (without using thread.sleep)


This is a file transfer (Server-Client tcp sockets)

The code below shows the transfer rate per second (kb/s) every one second.

I want to show the the speed (rate/s) every time I send the data to the client. How do I calculate the speed every time (without usings thread.sleep(1000))?

private void timeElasped()
    {
        int rate = 0;
        int prevSent = 0;
        while (fileTransfer.busy)
        {
            rate = fileTransfer.Sent - prevSent ;
            prevSum = fileTransfer.Sent;
            RateLabel(string.Format("{0}/Sec", CnvrtUnit(rate)));
            if(rate!=0)
                Timeleft = (fileTransfer.fileSize - fileTransfer.sum) / rate;
            TimeSpan t = TimeSpan.FromSeconds(Timeleft);
            timeLeftLabel(FormatRemainingText(rate, t));
            Thread.Sleep(1000);
        }
    }

Solution

  • in form constructor

    Timer timer1 = new Time();
    public Form1()
    {
        InitializeComponent();
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    }
    

    or add it from toolbox and set the previous values

    the sum of sent bytes should be public so our method can get its value every second

    long sentBytes = 0;      //the sent bytes that updated from sending method
    long prevSentBytes = 0;   //which references to the previous sentByte
    double totalSeconds = 0;   //seconds counter to show total time .. it increases everytime the timer1 ticks.
    private void timer1_Tick(object sender, EventArgs e)
    {
        long speed = sentBytes - prevSentBytes ;  //here's the Transfer-Rate or Speed
        prevSentBytes = sentBytes ;
        labelSpeed.Text = CnvrtUnit(speed) + "/S";   //display the speed like (100 kb/s) to a label
        if (speed > 0)                //considering that the speed would be 0 sometimes.. we avoid dividing on 0 exception
        {
            totalSeconds++;     //increasing total-time
            labelTime.Text = TimeToText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed));
            //displaying time-left in label
            labelTotalTime.Text = TimeToText(TimeSpan.FromSeconds(totalSeconds));
            //displaying total-time in label
        }
    }
    
    private string TimeToText(TimeSpan t)
    {
        return string.Format("{2:D2}:{1:D2}:{0:D2}", t.Seconds, t.Minutes, t.Hours);
    }
    
    private string CnvrtUnit(long source)
    {
        const int byteConversion = 1024;
        double bytes = Convert.ToDouble(source);
    
        if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
        {
            return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
        }
        else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
        {
            return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
        }
        else if (bytes >= byteConversion) //KB Range
        {
            return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
        }
        else //Bytes
        {
            return string.Concat(bytes, " Bytes");
        }
    }