`
webcode
  • 浏览: 6150692 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Data Binding: A godsend or the devil in disguise?

阅读更多

当.NET给我们带来无尽便利的时候,有谁会去探究它的性能。这篇文章让你尽量不要使用数据绑定。
来自:http://www.codeproject.com/aspnet/SitePerf.asp

When .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 RecordSets in your ASP to build your HTML. This is so easy!

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 didn’t 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, so in extreme situations where ASP.NET controls just won’t perform, then 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 average % 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.

分享到:
评论

相关推荐

    Android Data Binding

    implementation 'androidx.databinding:viewbinding:4.0.1' ``` 3. **生成绑定类**:编译时,Data Binding会自动生成一个绑定类,用于在Java代码中访问和操作布局中的元素。 4. **数据绑定表达式**:例如,`@{...

    Data Binding with Windows Forms 2.0

    Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...

    Data Structures and Algorithms in Java (2nd Edition)

    In the second edition, the program is rewritten to improve operation and clarify the algorithms, the example programs are revised to work with the latest version of the Java JDK, and questions and ...

    WPF Data Binding with LINQ to SQL

    This is the final part of a three-part series on using LINQ to SQL: Part 1: Mapping Tables to Objects Part 2: Adding/Updating/Deleting Data Part 3: WPF Data Binding with LINQ to SQL These tutorials ...

    Data Binding with Windows Forms 2.0 Programming

    This book is all about the what and the why of binding to data sources in a Windows Forms application built using Visual Studio 2005. The book goes into great detail in explaining the rationale behind...

    Android代码-数据绑定验证工具,快速帮你验证表单数据绑定状况

    The Data Binding Validator makes it easy and quick to validate fields in forms using data binding framework. Download Step 1: Add it in your root build.gradle at the end of repositories: allprojects {...

    Android Data Binding 代码实战 demo

    **Android Data Binding 框架详解与实战演示** 在Android应用开发中,数据绑定是一种将UI组件和数据源紧密关联的技术,它可以帮助开发者减少在Activity或Fragment中的样板代码,提高代码可读性和维护性。本实战项目...

    Data-Binding:Android数据绑定演示示例

    在"Data-Binding: Android数据绑定演示示例"中,我们主要会探讨以下几个关键知识点: 1. **数据绑定库的引入**:在项目中使用数据绑定,首先需要在`build.gradle`模块文件中添加依赖。例如: ```groovy android {...

    Android Data Binding实战-入门篇

    Android Data Binding是Google推出的一种强大的数据绑定库,它旨在简化Android应用中的UI逻辑,通过将数据绑定到XML布局文件中,使代码更加清晰、可读性更强,同时也减少了Activity或Fragment中的样板代码。...

    Android Data Binding 在 library module 中遇到错误及解决办法

    在使用Android Data Binding时,有时会遇到在library module中出现错误的情况。这通常是由于Data Binding在编译过程中生成的binding类与library module的特定规则不兼容导致的。本文将深入探讨这个问题并提供解决...

    Analog Circuit Design: High-speed Clock and Data Recovery

    Each part discusses a specific to-date topic on new and valuable design ideas in the area of analog circuit design. Each part is presented by six experts in that field and state of the art ...

    Navigation-ViewBinding:一些在活动和片段中使用ViewBinding处理片段和

    为了进一步简化代码,可以创建一个泛型函数,用于在任何Fragment或Activity中自动初始化ViewBinding: ```kotlin inline fun &lt;reified T : ViewDataBinding&gt; AppCompatActivity.bindLayout(inflater: ...

    data-binding:javascript 数据绑定

    nanoplex 数据绑定 #expressions 将变量、函数和对象绑定到属性和内部元素 用法 attr="{{var}}" {{var}} {{func(var)}} {{object.path.string}} 例子 {{function(var, var1)}} {{object.string}} ...

    AdventureWorks Data Binding sample

    本文将深入探讨“AdventureWorks Data Binding sample”,这是微软提供的一款用于演示VB.NET数据绑定特性的实例。 首先,让我们了解数据绑定的基本概念。数据绑定允许UI控件(如文本框、列表视图等)直接与数据源...

    Flex Data Binding详解

    Flex Data Binding是Adobe Flex框架中的核心特性之一,它允许开发者创建数据驱动的应用程序,通过将UI组件的属性与数据模型的属性直接关联,实现实时的数据同步。在Flex中,数据绑定确保当数据源发生变化时,相关的...

    ADF的data binding

    4. **Binding Context**:Binding Context是整个应用程序的上下文,它包含所有Binding Containers和Data Controls的信息。Binding Context提供了一个全局视图,使得在多个界面间共享数据和资源成为可能。 一个典型...

    C# 7.0 in a Nutshell: The Definitive Reference -epub

    When you have questions about C# 7.0 or the .NET CLR and its core Framework assemblies, this bestselling guide has the answers you need. Since its debut in 2000, C# has become a language of unusual ...

    DeepDTA_Deep Drug-Target Binding Affinity Prediction2018.pdf

    The increase in the affinity data available in DT knowledge-bases allows the use of advanced learning techniques such as deep learning architectures in the prediction of binding affinities. In this ...

Global site tag (gtag.js) - Google Analytics