Alik Levin's

Clarity, Technology, and Solving Problems | PracticeThis.com 

  • Alik Levin's

    Best ASP.NET Performance Winner For Data Binding - Hands Up To Response.Write()

    • 21 Comments

    Alik Levin     To achieve best performance you need to make decisions based on trade-off between coolness, coding productivity, and personal engineering values. I never thought I would be recommending my customer considering using old fashion Response.Write() in his Internet facing ASP.NET web application in order to significantly improve the application’s performance.

    Customer Case Study

    During load/stress testing customer’s ASP.NET web application we identified high CPU utilization (up to 90%). After quick investigation we noticed that %Time in GC performance counter is less than optimal. Our assumption was that the application uses memory allocation techniques that are less than optimal. From GC Performance Counters:

    "First thing you may want to look at is “% Time in GC”... What is a health value for this counter? It’s hard to say. It depends on what your app does. But if you are seeing a really high value (like 50% or more) then it’s a reasonable time to look at what’s going on inside of the managed heap."

    Another resource we used is timeless patterns & practices’ Chapter 15 — Measuring .NET Application Performance:

    .NET CLR Memory\% Time in GC

    "…The most common cause of a high value is making too many allocations, which may be the case if you are allocating on a per-request basis for ASP.NET applications. You need to study the allocation profile for your application if this counter shows a higher value."

    So we headed to looking into the code and this is what we found out.

    Analysis

    During performance code inspection we identified massive usage of collections. The collections were used to transfer the data between the logical layers and then the collections were transferred into datatables to be bindable for DataGrid (yes, it is .Net 1.1 app).

    Eureka! We just spotted 3 performance anti-patterns. Massive memory allocation, massive loops, massive type conversions. I’ve shown it to 4 very respected professionals and everyone was saying the same – current situation is pure performance anti-pattern. Here are few suggestions that came up:

    • Bind collections directly to DataGrid eliminating additional memory allocations and loops.
    • Create Datatable directly from XML skipping collection creation step eliminating additional memory allocations and loops.
    • Use Xslt transformation transforming original Xml into Html table using Xslt elminating memory allocations and loops for both collections and datatables.
    • Use Response.Write() as it’s suggested by patterns & practices:

    "Use the Response.Write method. It is one of the fastest ways to return output back to the browser."

    Case close? Not really...

    Secretly I’ve built Visual Studio 2003 project with these implementations and ran simple stress test using TinyGet utility. The results left us all a bit surprised.

    Converting Collection To Datatable (Current Situation)

    The code:

       1: //create custom collection
       2: MyCollection myCollection = (MyCollection)SampleServices.GenerateCollection(200);
       3:  
       4: //convert collection to datatable
       5: DataTable datatable = SampleServices.ConvertCollectionTableIntoDataTalbe(myCollection);
       6:  
       7: //bind datatalbe to dynamically created datagrid
       8: datagrid.DataSource = datatable;
       9: datagrid.DataBind();
     
    The stress test:
     
    tinyget.exe  -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/UseDataTable.aspx -loop:100 -threads:15
     
    The result:
     
    image

    Bind Collection Directly To Grid

    The code:

       1: MyCollection myCollection = (MyCollection)SampleServices.GenerateCollection(200);
       2:  
       3: //bind datatalbe to dynamically created datagrid
       4: datagrid.DataSource = myCollection;
       5: datagrid.DataBind();

    The stress test:

    tinyget.exe  -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/UseCustomCollection.aspx -loop:100 -threads:15

    The result:

    image

     

    Create Datatable From Xml

    The code:

       1: string xml = SampleServices.GenerateXml(200);
       2:  
       3: StringReader theReader = new StringReader(xml);
       4: DataSet theDataSet = new DataSet();
       5: theDataSet.ReadXml(theReader);
       6:  
       7: datagrid.DataSource = theDataSet.Tables[0].DefaultView;;
       8: datagrid.DataBind();

    The stress test:

    tinyget.exe  -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/LoadXmlIntoDataTable.aspx -loop:100 -threads:15

    The result:

    image 

    Use Xslt Transformation To Create Html Table

    The code:

       1: Xml1.DocumentContent = SampleServices.GenerateXml(200);
       2: Xml1.TransformSource=@"xsl.xml";

    The stress test:

    tinyget.exe  -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/XmlXslTransformation.aspx -loop:100 -threads:15

    The result:

     image

    Use Response.Write()

    The code:

       1: MyCollection myCollection = (MyCollection)SampleServices.GenerateCollection(200);
       2:  
       3: // Put user code to initialize the page here
       4: Response.Write("<table>");
       5:  
       6: foreach(MyModelItem item in  myCollection)
       7: {
       8:  
       9:     Response.Write("<tr>");
      10:     Response.Write("<td>" +  item.Address  + "<td>");
      11:     Response.Write("<td>" +  item.City  + "<td>");
      12:     Response.Write("<td>" +  item.Education+ "<td>");
      13:     Response.Write("<td>" +  item.Family  + "<td>");
      14:     Response.Write("<td>" +  item.Name  + "<td>");
      15:     Response.Write("</tr>");
      16: }
      17: Response.Write("</table>");

    The stress test:

    tinyget.exe  -srv:192.168.50.68 -uri:/dynamiccontrolsloadingrelease/ResponseWrite.aspx -loop:100 -threads:15

    The result:

    image 

    Sample Visual Studio 2003 Project

    Interested in testing it yourself? Grab the source code from my SkyDrive here:

    Conclusion

    After conducting this simple test these are the conclusions I’ve made:

    • “Don't be afraid to challenge the pros, even in their own backyard." - How to Get Things Done - Colin Powell Version
    • Testing IS DA thing. Assumptions are good but nothing speaks louder than facts.
    • Test early - avoid massive rework afterwards. Create POC's (Proof of concept) early in architecture/design stages.
    • Best performance comes on expense of productivity and coolness.

    Related Materials

  • Alik Levin's

    My Pipeline Is My Inbox

    • 13 Comments

    I keep seeing folks with thousands emails inside their Inbox... I hardly can get it. Actually I do not get it. I cannot imagine  actual mail box getting filled with thousands of letters - so why making your Outlook Inbox a trash can?

    As a consultant I spend most of my work time working on my laptop - writing code, writing docs, reviewing stuff, summarizing meetings, etc. Context switching is of high rate but My life Definitely Changed When I understood that Focus Is The Key

    I decided that the simplest way to focus one my action items is get them into one place - my Outlook Inbox. My customers send me emails, my friends send me emails, my manager sends me emails, my colleagues send me emails. So if I have some other action item - I got phone call, I have some idea, etc - then I just do post (ctrl+shift+S) into my Inbox.

    Once in inbox I need to handle all this avalanche of emails. Just like I clean my mail box, I do the same with my Inbox. I set some time - usually in the morning - just after I walked my dog and before kids wake up, but any other time works too. What I do with mails is the following (the ultimate goal is to sort while scanning my Inbox - not to read!):

    • Junk move to delete
    • Tech good stuff move to KB pst. NO READING in depth!
    • Customer/Engagement specific FYI move to Customer/Engagement pst
    • Action item (from customer, my manager, my peer, myself) - block time slot in the calendar to handle it. Just block time slot, put the email in it and forget. When the time comes - I am in context to deep dive into it.
    • Personal move to personal pst

    Takes me minutes to clean the Inbox and block times to handle it afterwards.

    If my Inbox  grows I start to worry since I know there is something I do not have (have not blocked) time to handle it and it can get worse. The worst thing for me is when Inbox gets scroll bar.

    When my Inbox is clean I know everything is planned so I can go ahead and change the plan (or just execute against it) rather skipping from one fire alarm to another.

  • Alik Levin's

    How To Hack WCF - New Technology, Old Hacking Tricks

    • 12 Comments

    First of I'd like to thank Guy for his excellent screencast - very convenient, so thanks.

    Specifically I liked introductory screencast for WCF which can be found here: http://blogs.microsoft.co.il/blogs/bursteg/pages/WCF-Introduction-Demo-_2800_ScreenCast_2900_.aspx

    It is dubbed in Hebrew, but the screens are flipping in so logical way so that one who does not understand Hebrew will be fine - go for it - recommended a lot for WCF newbies like me.

    My interest was to understand the pipeline that the WCF Message goes through before it is put on the transport. The idea was to inject some custom modules (Inspectors) in the pipeline. Why? Is not it clear? To mess around with the message - tamper it in it raw format before it goes down to the transport signed and protected. Why? To show that it DOES NOT matter what communication technology you use - HTTP, Remoting, MSMQ, WCF, RMI, CORBA, DCOM, MQ, <<fill in your own here>> - the basic principle of VALIDATING INPUT ON THE SERVER SIDE is immutable.

    Here I showed it for Web Services App Architecture with Security in mind - Video, Part I (that was easy - Fiddler is of much help here)

    Then remoting came along - same result, here App Architecture with Security in mind - Video, Part II

    Now it is mighty WCF.

    I used excellent demo from Madhu here http://blogs.msdn.com/madhuponduru/archive/2006/07/19/671922.aspx that explained how to build IClientMessageInspector (NOTE - demo that works!)

    So here is the service contract:

    and the implementation:

    and the client side validation:

    here is the client rejects the input:

    and here is the result of server processing for good input:

    after adding the custom message inspector, I am offered to tamper the massage before it is sent to the service and the resulting reply from the service is in red at the bottom:

    Conclusion

    Does that mean that the communication technologies are bad? - NO, it is the way WE use it.

    Here is an basic example for input validation in Web Services Web Service Input Validation - it has link to regex usage that you can use on the server side for input validation.

    Enjoy

Page 1 of 118 (354 items) 12345»