When .NET brings us endless convenience, anyone will explore its performance. This article allows you to try not to use data binding. From: http: //www.codeproject.com/aspnet/SitePerf.aspWhen .NET was released, many web developers were ecstatic when they saw how easy it was to use server side ASP.NET controls Slap the control on the page,. Set a dataset or arraylist to the databind property and away you go, instant web pages. no more looping through records in Your Asp to build your html. this is so y!
It is easy, but at a cost. I recently worked on a project that used data bound ASP.NET DataList and Repeater controls extensively to build their web pages. But the performance results were very disappointing. Using ACT to run some load tests on these pages, with a small number of concurrent users (5), the page performed reasonably well. But as soon as we increased the number of users to something more realistic, like 25, the page performance went to pot. So we started using PerfMon while the tests were running, and found something pretty interesting. The% time in garbage collection for the page was averaging 30%, with a maximum spike of 45%! Also, the% Processor was pegged at 95% for the entire test run. These last two statistics were big red warning lights, because they told us that not only did our pages run slow, but they were not going to be able to scale out at all. If the site had a high load of users, it would be in Serious trouble.
This was no good and was not acceptable for a release to production, so we started digging into how the data bound controls worked. What we found out was that the data binding process was doing two things that were hurting performance. First, data bound controls use reflection to find the correct property and pull the data from it. Reflection is fairly costly and if you have a repeater control that is pulling 6 properties from an array of 40 objects, the performance hit can really add up.The second thing we noticed was that the number of objects that were being created during the data binding process was pretty high (look in Anakrino at the DataGrid, DataList and Repeater class' CreateControlHierarchy to see how it does its binding). This high number of objects being created was what WAS Kicking The% Time in Garbage Collection So High.
So we had to find a way to create web pages without using data binding. We tried using ASP.NET server controls and manually pushing the data, but this did not really change our statistics very much. Then we got desperate, and really started brainstorming. We tried placing one Literal control on each page and used a StringBuilder in the code behind's PageLoad event to build the HTML structure for the page and then slapping the HTML into the Literal control's text property. This technique performed amazingly well and the% time In Garbage Collection Went Down To Almost Nothing. But The Maintainability of The HTML Would Have Been a Nightmare.
We then decided to try mixing ASP.NET code behind with an ASP style of HTML building. We created and populated all our data objects, as well as put any business logic the page needed in the aspx's code behind PageLoad event. Then in the aspx file, we went back to ASP style HTML building, using old fashioned <% = (C # code)%> to actually inset data from our data objects into the HTML. This technique performed just as good as the string builder technique, but the maintainability of the code was much better.The only problem with this ASP style of HTML rendering is that you are back to writing the HTML to a forward only stream, just like ASP. When using ASP.NET controls, you can update the value of any Control, DURING ANY Stage in The Code. But Web Developers Have Been Doing this Since The Beginning Of ASP.NET Controls Just Won't Perform, The this is a workable option.
Once we had our test page coded this way, I ran ACT on the data bound version and the new ASP style version, and compared the results. During a 15 minute run with 10 users, the ASP style page was able to run iterations as many times as the data bound version. The average requests per second jumped from 72.55 to 152.44 requests per second. The average time to last byte went from 21.79 milliseconds down to an amazing 2.57 milliseconds! But the best statistics came from the% time in garbage collection And Processor%. The averge% Time IN Garbage Collection Went From 30% Down to .79%, and the average processor% Went from 95% Down to 10%! This Meant That Our Asp Style Pages Would Scale Out To a Higher Number of Users with very little trouble.