Recording for my webinar with PostSharp

Again, I’d like to thank Igal Tabachnik and SharpCrafters for inviting me to do the webinar, the recording of the session is now available on their Vimeo channel.

Pseudo Real Time Performance Monitoring with AOP and AWS CloudWatch from SharpCrafters on Vimeo.


Performance Monitoring with AOP and Amazon CloudWatch

View more PowerPoint from Yan Cui


Source code is available at

Slides and Source Code for my webinar with PostSharp

Following my recent webinar with SharpCrafters on how to setup pseudo real-time performance monitoring using Aspect Oriented Programming and Amazon CloudWatch, I’d like to say thanks to the guys for having me, it was a great fun Smile

For anyone interested, the source code is available at:

If you want to run the demo console app to generate some data, you need to put your AWS key and secret in the App.config file in the Demo.ConsoleApp project:


Just go to and create an account, you’ll then be given an access key and secret to use.

The slides for the session is also available to download on SlideShare:


PostSharp webinar

I will be doing a webinar with the good folks of PostSharp on the 22nd March to talk about the use of AOP and AWS CloudWatch as a pseudo real-time performance monitoring tool (see high-level overview here).

It’s a free webinar, it runs from 22nd March 3PM – 4PM GMT, the registration link is here.


Hope to see you there Smile

Pseudo RealTime performance monitoring with AOP and AWS CloudWatch

This is something I’ve mentioned in my recent AOP talks, and I think it’s worthy of a wider audience as it can be very useful to anyone who’s obsessed with performance as I am.

At iwi, we take performance very seriously and are always looking to improve the performance of our applications. In order for us to identify the problem areas and focus our efforts on the big wins we first need a way to measure and monitor the individual performance of the different components inside our system, sometimes down to a method level.

Fortunately, with the help of AOP and AWS CloudWatch we’re able to get a pseudo-realtime view on how frequently a method is executed and how much time it takes to execute, down to one minute intervals:


With this information, I can quickly identify methods that are the worst offenders and focus my profiling and optimization efforts around those particular methods/components.

Whilst I cannot disclose any implementation details in this post, it is my hope that it’ll be sufficient to give you an idea of how you might be able to implement a similar mechanism.


A while back I posted about a simple attribute for watching method executing time and logging warning messages when a method takes longer than some pre-defined threshold.

Now, it’s possible and indeed easy to modify this simple attribute to instead keep track of the execution times and bundle them up into average/min/max values for a given minute. You can then publish these minute-by-minute metrics to AWS CloudWatch from each virtual instance and let the CloudWatch service itself handle the task of aggregating all the data-points.

By encapsulating the logic of measuring execution time into an attribute, you can start measuring a particular method by simply applying the attribute to that method. Alternatively, PostSharp supports pointcut and lets you multicast an attribute to many methods at once, and allows you to filter the method target by name as well as visibility level. It is therefore possible for you to start measuring and publishing the execution time of ALL public methods in a class/assembly with only one line of code!


The CloudWatch service should be familiar to anyone who has used AWS EC2 before, it’s a monitoring service primarily for AWS cloud resources (virtual instances, load balancers, etc.) but it also allows you to publish your own data about your application. Even if your application is not being hosted inside AWS EC2, you can still make use of the CloudWatch service as long as you have an AWS account and a valid AWS access key and secret.

Once published, you can visualize your data inside the AWS web console, depending on the type of data you’re publishing there are a number of different ways you can view them – Average, Min, Max, Sum, Count, etc.

Note that AWS only keeps up to two weeks worth of data, so if you want to keep the data for longer you’ll have to query and store the data yourself. For instance, it makes sense to keep a history of hourly averages for the method execution times you’re tracking so that in the future, you can easily see where and when a particular change has impacted the performance of those methods. After all, storage is cheap and even with thousands of data points you’ll only be storing that many rows per hour.

AOP – Enforce initialization with PostSharp

Oh, the Pain!

Now and again we all come across an object which requires initialization before it can be used but with nothing there to stop us from trying to use it before it’s initialized.

In most cases you will get a seemingly unrelated exception (e.g. null reference when the object tries to use some resource which it expects to be set during initialization), which aren’t very helpful at all when you try to debug/identify the problem.

If you’re lucky, the object you’re using might have been designed with this in mind and gives you a meaningful exception when you try to call a method which requires the object to be initialized first, in which case it should be a straightforward to fix your code accordingly. For the guys designing the class though, this usually means they have to litter their code with validation logic and often means duplicated validation code or unnecessary inheritance just to encapsulate the validation logic.

Fortunately, with the help of PostSharp, a simple attribute can take care of this chore for you with ease!

Define high level interface

First off, you need a nice and simple interface like this:

/// <summary>
/// Marks a component which needs to be initialized before it can be used
/// </summary>
public interface IInitializable<T>
    bool IsInitialized { get; }

    void Initialize(T initObject);


The attribute will look something like this (in PostSharp 2):

[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class InitializationRequiredAttribute : MethodInterceptionAspect
    public override bool CompileTimeValidate(MethodBase method)
        // make sure that the attribute is used in a type that implements the 
        // IInitializable interface
        if (!method.DeclaringType.GetInterfaces()
                   .Any(i => i.IsGenericType &&
                             i.GetGenericTypeDefinition() == typeof(IInitializable<>)))
            throw new Exception(string.Format(
                "The [{0}] attribute can only be used in types that implement the [{1}] interface",
                GetType(), typeof(IInitializable<>)));
        return base.CompileTimeValidate(method);

    /// <summary>
    /// Checks the IsInitialized property before allowing the call to proceed
    /// </summary>
    public override void OnInvoke(MethodInterceptionArgs eventArgs)
        var propertyInfo = eventArgs.Instance.GetType().GetProperty("IsInitialized");
        if (propertyInfo != null)
            var propertyValue = propertyInfo.GetValue(eventArgs.Instance, null);
            if (propertyValue != null && propertyValue is bool && (bool) propertyValue)
                // only proceed with the call if the instance has been initialized
        throw new InstanceNotInitializedException(eventArgs.Method.Name);

I have enforced a rule that this attribute can only be used in a class which implements our IInitializable interface, you can relax this rule by removing the overridden CompileTimeValidate method. However, in my experience it’s best to not leave any grey area in your code just in case you run into a situation where you had expected the attribute to do its job only to find out later that something’s gone wrong because you had accidentally/purposely removed the IInitializable interface from your class.


To use the attribute:

public class MyClass : IInitializable<string>
    public bool IsInitialized { get; private set; }
    public void Initialize(string initializationData)
        IsInitialized = true;

    public void DoSomething() { }