Results 1 to 3 of 3

Thread: Better code for this ? (profiling advice)

  1. #1
    Join Date
    Aug 2005
    Location
    Paris / France
    Posts
    12

    Default Better code for this ? (profiling advice)

    Hi!

    while developing some ldap authentication routine, I wanted to measure the time taken by the authentication call. I thought going AOP would be rather handy and non-obstrusive on the code I want to profile.

    I came up with the following stuff - could anyone with more Spring AOP experience review that ? Any feedback welcome.

    the advice:

    Code:
    public class ProfilingAdvice : IMethodInterceptor
    {
    	private static readonly ILog log = LogManager.GetLogger(typeof(ProfilingAdvice));
    
    	public object Invoke(IMethodInvocation invocation)
    	{
    		DateTime startTime = DateTime.Now;
    		try
    		{
    			return invocation.Proceed();
    		}
    		finally
    		{
    			log.Info(string.Format("{0}.{1} call took {2}",invocation.Method.DeclaringType.Name,invocation.Method.Name,DateTime.Now-startTime));
    		}
    	}
    }
    the helper to return a proxified instance:

    Code:
    private static object BuildProfilingProxy(object originalObject)
    {
    	ProxyFactory factory = new ProxyFactory(originalObject);
    	factory.AddAdvice(new ProfilingAdvice());
    	return factory.GetProxy();
    }
    and sample code usage

    Code:
    [Test, Explicit]
    public void BenchmarkLDAPCall()
    {
    	BasicConfigurator.Configure();
    
    	ILDAPAuthenticationProvider provider = new DirectoryServicesAuthenticationProvider();
    
    	provider = (ILDAPAuthenticationProvider)BuildProfilingProxy(provider);
    
    	Assert.IsTrue(provider.Authenticate("myldaphost", "cn=mylogin,o=mycompany");
    }
    cheers

    Thibaut Barrère

  2. #2
    Join Date
    Oct 2005
    Location
    Toulouse, France
    Posts
    1,409

    Default

    Bonjour Thibaut,

    The AOP part seems good.
    But if you need more precision, u can use ticks :

    Code:
    long start = DateTime.Now.Ticks;
    // Code
    long end = DateTime.Now.Ticks;
    If you are working on .NET 2.0 framework, you should use the new System.Diagnostics.StopWatch class :

    Code:
    Stopwatch sw = Stopwatch.StartNew();
    try
    {
       return invocation.Proceed();
    }
    finally
    {
       sw.Stop();
       // sw.ElapsedMilliseconds, sw.ElapsedTicks, etc...
    }

    - Bruno
    My english is as poor as my taylor is rich

  3. #3
    Join Date
    Aug 2005
    Location
    Paris / France
    Posts
    12

    Default

    Hi Bruno

    thanks for the review, and good point about the accuracy (well in my case the server takes 10+ seconds to reply for the moment so I'm accurate enough)

    cheers

Similar Threads

  1. Introduction Advice for windows.forms throws exception
    By GrainOfSalt in forum AOP (Aspect Oriented Programming)
    Replies: 3
    Last Post: 04-25-2006, 09:57 AM
  2. Replies: 0
    Last Post: 04-04-2006, 02:44 PM
  3. Getting a "mixed" objet in a Before Advice impleme
    By cylt in forum AOP (Aspect Oriented Programming)
    Replies: 4
    Last Post: 12-30-2005, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •