Performance tips for RIA Service with Silverlight 4
Performance is a vital part for any application possibly more important for web based app.With wizard based approach of RIA service generally we tends to comprise all entities, exposing to the client and also allowing the DomainService to go for everything from the database. Not only this approach takes a toll on security, but also it loads the middle tier unreasonably.
So this post is about few tips using which we can improve the performance. Well mostly we will cover the Pagination, Limiting Query results and Output cache of the results.
As we need a database with records that really exist on real worlds scenario.I am going to stick with Adventure Database example database from Microsoft.As mentioned on my earlier post ,More details regarding the database can be found from here.Also you can download database samples from codeplex with following links AdventureWorksLT 2005 Version,AdventureWorksLT 2008 Version.For Presentation model scenario i am going to use a part of the Data Model which involves Customer and Address .
And we will use the same sample as mentioned in my Presentation Model article.
Before beginning with the performance measures allow me to put some light on basic configurations and coding of the project.In this example the DataBinding is achieved declaratively through DomainDataSource control .The DomainDataSourceControl code is as follows
The next step is bind the the DomainDataSource to the Item Source of DataGrid ,
Performance Snapshot - Before
Using Visual Studio Profiler Wizard with CPU sampling ,the snapshot shows the elapsed time for calling the query and fetching the detail from the database.The straight forward approach to RIA service with Domain DataSource control it retrieves all 800 records at one shot.
Well a detailed tutorial about performance wizard in Visual Studio 2010 can be found here
Performance Tips and Tricks
As the number of records returned by the query always have a performance impact over web so the following tips and trick in combination proper coding standard always makes a difference lets discuss few of them
Pagination with LoadSize and PageSize
The first step of improvements lies with applying paging to the data .Well paging works with out DomainDataSource control but it works better with DomainDataSource control.So lets add a DataPager control to the page and bind the source to Domaindatasource control
the database so we need to some tweaks to the DomainDataSource control
As mentioned in above image ,LoadSize load exactly specified amount of records with each hit to the Tier interaction.For next 30 records it hits the database again.
so here with above snap shot from Tier Interaction you can find out for each record after 30th the DomainDataSource control is sending fresh request to fetch next set.
While working with LoadSize and page Size make sure that the PageSize of Datapager and DomainDataSource control are same , here in this example its defined as 10.Also make sure the page size is less than the LoadSize.
Limit Results of Query Operation
Instead of fetching all the detail , try using QueryAttribute
to limit the results from the query operation .Domain Service at server side supports attribute to limit the result set.For example here in this example each call to domain service returns maximum 30 results.
Well make sure that your loadsize and pagesize as mentioned in above tips for Datapager must be with in this limit .Well it worth mentioning that one of my reader was facing issues ,the famous LoadOperation failed and on digging a bit we found real problem was timeOut .once we set the ResultLimit , the project was on run.
Well this option really does not meant for real time data where changes are very frequent but you can always cache the output of a method call using attributes.
As shown above OutoutCacheLocation specifies the location where the resource need to be cached.
Performance Snapshot - After
Well we talked much and now its time for result , as mentioned above the TierInteraction gives you a complete picture of time taken by each method call .
Comparison of Performance
The last call where you can compare both performance results let you show a complete picture over the improvement.
The Delta here shows a sharp increment in performance of Tier Interaction for GetCustomerWithAddress query method.
May be there are lots of scope available for performance improvement such as exposing entities that need to be exposed to client by using Presentation Model and some few tricks over native WCF service but for the time being hope above will make a difference .Keep me suggesting your ideas and comments.
Source Code and Performance Reports
Download Link - : RIAPerformanceTips.zip