Self Aware Objects – Performance Testing

The code for the self-aware objects uses reflection to check the values of properties, and see if they meet the validation requirements declared by the custom attributes.

If you’ve ever worked with reflection, you know it isn’t fast.  So I ran some performance tests this morning, to compare the validation routines using my SAO code and the exact same validation rules in a normal controller.

There’s good news and bad news.

The bad news is, yes, SAO validation is slower.  It takes about 31 times longer to validate an object – as the code currently is written.

However, the good news is that it’s still fast enough for almost every business application I’ve ever been involved with.

The exact results, to perform the object validation routine ten thousand times, are:

Normal validation: 0.0042 seconds

SAO validation: 0.131421 seconds

I don’t know what your volume of data input is, but I’ve personally only ever worked on one business application with requirements to potentially validate tens of thousands of objects per second.  Even in that situation, you will still get sub-second throughput.

The thing to keep in mind is the big picture.

Will using a method like this cause a noticeable degradation in your application’s performance?  It’s very unlikely.

Will using a method like this decrease your development time, produce more reliable code, and make maintenance easier (due to the clarity from easily-found business logic)?  I think it will.

Here’s the code I used for the performance tests – both the standard code and the SAO code.









SAOAttributeInfo.cs (used to cache SAO custom attribute information in the validator)

Next Step

I have a couple other ideas I want to try out with building self-aware objects.  Before I go much further with these custom attributes, I want to see how the other ideas look.

I’m searching for programming techniques to improve code quality, shorten development time, and make maintenance work easier.

After all, the ultimate goal of programming is to solve business problems.  The faster we can provide a reliable, working solution to the people who hire us, and the less time we spend maintaining those solutions, the happier they’ll be.

One thought on “Self Aware Objects – Performance Testing

  1. I just made a change to the SAObjectValidator.cs Validate() method.

    Instead of having a switch statement that calls a private method to handle the logic for each custom attribute, I moved the validation logic into a Validate() method of each custom attribute classes. Now, SAObjectValidator.Validate() just passes the object and property into the custom attribute’s Validate() method and lets it do its magic.

    That made the code much cleaner, put the validation logic where you’d most expect to find it, and improved the performance by around eight percent.

Leave a Reply

Your email address will not be published. Required fields are marked *