Kyle
Kyle

Reputation: 17687

wcf webservice timing methods?

I recently found this article: http://coderjournal.com/2010/10/timing-the-execution-time-of-your-mvc-actions/

Which shows a nice little attribute that you can add to your Controller classes, which creates a stopwatch and sticks an X-Runtime header into the result.

I've been trying to add something similar to my WCF Rest service, but have been unable to figure it out so far. Any suggestions on how I could go about implementing something similar on my webservice?

Upvotes: 1

Views: 216

Answers (1)

devshorts
devshorts

Reputation: 8872

This is a perfect place for aspects. Check out post sharp.

You can then create an aspect kind of like:

/// <summary>
/// Adds execution timing and automatic logging to the tagged method
/// </summary>
[Serializable]
public class MethodTimeAttribute : OnMethodBoundaryAspect
{
    private String MethodName { get; set; }
    private DateTime StartTime { get; set; }

    /// <summary>
    /// Method executed at build time. Initializes the aspect instance. After the execution
    /// of <see cref="CompileTimeInitialize"/>, the aspect is serialized as a managed 
    /// resource inside the transformed assembly, and deserialized at runtime.
    /// </summary>
    /// <param name="method">Method to which the current aspect instance 
    /// has been applied.</param>
    /// <param name="aspectInfo">Unused.</param>
    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        MethodName = method.DeclaringType.FullName  "."  method.Name;
    }


    public override void OnEntry(MethodExecutionArgs args)
    {
        StartTime = DateTime.Now;
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Log.Debug(this, "Method {0} took {1} to execute", MethodName, DateTime.Now - StartTime);
    }
}

And all you have to do is tag your methods like this:

[MethodTime]
private void RunFor(TimeSpan runTime){
    // do stuff 
}

Advanced post sharp features require a license but simple ones like method boundary aspects are free.

While this doesn't test any network boundaries for your wcf service it will test your service implementation time.

If you really wanted to you could create some custom behaviors and inject that into your WCF chain to test timing, but the likelihood of bottlenecks at that phase is pretty unlikely. What's more likely is you are sending a lot of data so things are slow, or things in your implementation are slow, all of which can be tested server side or client side independently.

Upvotes: 1

Related Questions