简体   繁体   中英

Capture the start and end time of calling the server from the client in .NET application

How can i on the client side pass a parameter that gets the start and end time which is how long it takes to call this call to the server since the server stores some information and it cannot communicate directly to the client but the client can request. I need to get the shortest, longest, and average time and have that information show up in the UI in the background client where i can go in and see how long it takes each user to call the server.

#region Timer Handlers

    private void OnTimer(object sender, ElapsedEventArgs e)
    {
        Ping();
    }

    protected virtual void Ping()
    {
        try
        {
            //Created Date Time to 
            DateTime _Now = DateTime.Now;


            var incomingRequests = GetInboundEcounterInfoForClientCommand.CheckForNewInboundRequests(
                _sessionKey, _userName, _machineName, _openedPatients);
            DateTime NowAgain = DateTime.Now;

            TimeSpan difference = NowAgain - _Now;

            PaceartLoggingService.LogInfo()

            if (incomingRequests != null)
            {
                incomingRequests.ForEach(request => _requests.Enqueue(request));

                if (_requests.Count > 0)
                {
                    InvokeInboundDataAvailible();
                }
            }
        }
        catch (Exception ex) // this should only happen when the server goes down
        {
            PaceartLoggingService.LogError(typeof(GetInboundEcounterInfoForClientCommand), ex);
        }
    }

    private void InvokeInboundDataAvailible()
    {
        if (InboundDataAvailible != null)
        {
            InboundDataAvailible(this, EventArgs.Empty);
        }
    }

    #endregion

I did not completely get your question's point, I assume you want to measure the execution time of a long-time-running method. The method is on server and you call it on Client.
There are two places you can do so.
First approach: you can start a timer, call the method, get the result, stop the counter, and now you have the execution time according to client. This approach is not very much accurate because you are also counting the connection time, I mean if the method takes 10 secs and the connection takes 2 secs, you get 12 seconds.
Second approach: you can use the same timer on server method. You call the server method from client, the server starts the timer, does the long-time-job, stops the timer, and finally passes back the time elapsed as the result to the client. This approach is a little more accurate because connection time will not be counted.

public class Client
{
    public void CallServerMethod_ApproachOne()
    {
        Server s = new Server();
        var sw = System.Diagnostics.Stopwatch.StartNew();
        s.LongLongTimeMethod_ApproachOne();
        sw.Stop();
        MessageBox.Show(sw.ElapsedMilliseconds.ToString());
    }

    public void CallServerMethod_ApproachTwo()
    {

        Server s = new Server();
        var elapsedMilliseconds = s.LongLongTimeMethod_ApproachTwo();
        MessageBox.Show(elapsedMilliseconds.ToString());
    }
}

public class Server
{
    public void LongLongTimeMethod_ApproachOne()
    {
        //this is just a way to waste time, your real work is done here 
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(1, 10)));
        return;
    }

    public long LongLongTimeMethod_ApproachTwo()
    {
        var sw = System.Diagnostics.Stopwatch.StartNew();
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(1, 10)));
        sw.Stop();
        return sw.ElapsedMilliseconds;
    }
}

And for logging the shortest, longest and average time, you can keep the values in a sort of list or array.

Last of all, using DateTime to get the execution time is not an appropriate solution. The best solution is to use Profilers, you can google it. Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM