Alik Levin's

Clarity, Technology, and Solving Problems | PracticeThis.com 

January, 2008

  • Alik Levin's

    Profiling JavaScript With Ajax View Tool: Spot Poor Performance Client Script In No Time

    • 3 Comments

    Ever wondered why your application unreasonably slow? You have it all - most powerful hardware, your database is tuned, SQL queries are optimized, network is barely utilized, and .Net code is super efficient. So why on earth response time is so slow?

    The answer might lie in recently developing area – client script. I’ve witnessed few over-AJAX’ed applications with poor performance and far from the best user experience. Quite the contrary what AJAX and other client script flavors are for…

    In this post I will show how Ajax View – JavaScript profiler project from Microsoft Research – helped me to spot client side script bottlenecks in no time and make the customer happy.

    • Ajax View – the tool. Ajax View is available for download here. The tool's goal:

    "The goal of the Ajax View project is to improve developer's visibility into and control over their web applications' behaviors on end-user's desktops."

    Issue Recommendation
    1200 lines of inline JavaScript Make JavaScript and CSS External
    Using XmlHttp object with its async property set to true while performing time consuming call to the server Do not re-invent the wheel, use ready to-go, proven AJAX Libraries like ASP.NET AJAX
    Manipulating large XML islands inefficiently Do not use large XML documents - neither client side nor server side. Use lazy approach of downloading the data on demand.

    My related posts

  • Alik Levin's

    Chain Of Responsibility Design Pattern – Focus On Security, Performance, And Operations

    • 1 Comments

    The pattern is also called Intercepting Filter, Pipeline, AOP, and may be few more… I am confused by the name for this design pattern.

    “Life is really simple, but we insist on making it complicated.” - Confucius

    No matter how they call it I like the idea of decoupling actions while processing one after another. Here is the definition from data & object factory:

    Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

    Intercepting Filter visual from MSDN’s Discover the Design Patterns You're Already Using in the .NET Framework

    clip_image005

    .Net framework implements Chain Of Responsibility design pattern for many its internal mechanisms. My favorite is HttpModule. I like it so much I decided to build my own pipeline.

    This post summarizes my steps I took to create my own simple implementation for this design pattern. I did not do any research purposely online and wanted to get my hands dirty without bias.

    The goal

    My goal was creating simple code that can be adopted and extended:

    • The code should be very simple
    • The code should provide pipeline infrastructure to invoke aspects/filters
    • The code should allow dynamic invocation of any arbitrary number of aspects
    • The code should include no optimization to stay simple
    • The code should demonstrate practical idea and not be state of the art, production ready to go one.

    What I needed

    • Provide generic mechanism that will execute arbitrary logic – the Pipeline.
    • The logic is encapsulated in decoupled components – Aspects.
    • Aspects can be independently configured without rebuilding the application.

    The design

    clip_image006

    Summary of steps

    • Step #1 – Create base classes and interfaces.
    • Step #2 – Create concrete implementation of specific Pipeline.
    • Step #3 – Modify config file.
    • Step #4 – Test the solution.

    Following section describes each step in details

    • Step #1 – Create base classes and interfaces. I needed two bases classes - BasePipeline and IAspect. BasePipeline is responsible to perform generic actions of:
      • Consulting configuration file.
      • Loading the configured aspects.
      • And invoking them one by one.
      private void LoadPipelineAspects()
      {
          aspects = new List<IAspect>();
      
          Configuration config = 
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);
      
          string  piplineConfig = 
                  config.AppSettings.Settings[pipelineName].Value;
      
          string[] aspectNames = piplineConfig.Split(',');
      
          for (int i = 0; i < aspectNames.Length; i++)
          {
      
              Type t = Type.GetType(aspectNames[i]);
      
              IAspect aspect=(IAspect)Activator.CreateInstance(t);
      
              aspects.Add(aspect);
      
          }
      }
      IAspect interface serves as a contract between BasePipline and aspect's concrete implementation. Take a look at the design diagram. When the client invokes HandleRequest method on concrete Pipeline implementation it invokes underneath generic implementation of the method of its base - BasePipeline:
      public virtual bool HandleRequest(object request) 
      {
      
          foreach (IAspect item in aspects)
          {
              item.ProcessRequest(request);
          }
          return true;
      }
    • Step #2 – Create concrete implementations. Concrete implementations of both types SimplePipeline and SimpleAspect are nothing fancy. SimplePipeline calls into its base but reserves the right to add any additions without interfering with any other concrete implementations:
      public override bool HandleRequest(object request)
      {
          base.HandleRequest(request);
      
          return true;
      } 
      To indicate what aspect is running I simply spit out aspect's name for aspect's ProcessRequest. That is the place where the whole logic should be performed - DB access, Web Services calls, request modifications etc :
      public bool ProcessRequest(object request)
      {
          Console.WriteLine("processing from SimpleAspect1");
          return true;
      }
    • Step #3 – Modify config file. Config file - app.config or web.config in my case - holds the information about what aspects should be called for specific pipeline:
      <appSettings>
       <add key =
      "MyPipeline.SimplePipeline" value ="MyPipeline.SimpleAspect1,MyPipeline.SimpleAspect2"/> </appSettings>

      The key is the type of the pipeline and the value is comma separated types of the aspects. When the concrete pipeline is instantiated by the client it already knows it type which serves to consult the config file and identify what aspects to load using reflection. This is how BasePipeline constructor looks like:

      public BasePipeline()
      {
          pipelineName = this.ToString();
          
          LoadPipelineAspects();
      
      }

      See Step #1 for LoadPipelineAspects() function implementation where reflection magic happens to dynamically load the aspects.

    • Step #4 – Test the solution. To test the solution follow these steps:
      • Create Class library project and reference to the library where Pipeline base classes reside. Implement your own Pipeline while inheriting from BasePipeline.
      • Implement few aspects that implement IAspect interface.
      • Create simple Windows Console application, add reference to the libraries and add simple code similar to this to Main function:
        MyPipeline.SimplePipeline sp = 
        new MyPipeline.SimplePipeline(); sp.HandleRequest(null);
      • In the config file add modifications as described in Step #3.

      • Run the application.

    Done.

    image

    Any time one needs to change the logic, add/remove aspects - it is just a matter of tweaking config file - that is it.

    I've just copied and pasted existing aspects inside the configuration file and run the application again, here is what I get:

    <add key ="MyPipeline.SimplePipeline"
         value ="MyPipeline.SimpleAspect1,
                 MyPipeline.SimpleAspect2,
                 MyPipeline.SimpleAspect1,
                 MyPipeline.SimpleAspect2,
                 MyPipeline.SimpleAspect1,
                 MyPipeline.SimpleAspect2
    "/>

    image

    I think it is cool.

    Focus on Security

    From security perspective there are few things to keep in mind. When invoking assemblies with reflection there is immediate risk of luring attacks that result in spoofed assemblies. For more information how to get protected see my related post below - .Net Assembly Spoof Attack.

    Focus on Performance

    Reflection is considered as slow operation. Everything is relative. In case where reflection is used in performance critical application you should consider performance optimization for reflection. One such optimization is constructor caching as described in More Provider Goodieness

    Focus on Operations

    From operations perspective it seems ideal case. I witnessed few times the situation where the change was not an option since it required whole rebuild of the application - something that operations team was not happy with and put veto on it.

    The trick is constantly asking yourself "What I am optimizing? Security, Performance, or Operations?"

    My related posts

    Download the sample from my SkyDrive:

    Have fun.

  • Alik Levin's

    Create Your Own Guidance Explorer Items Inside Outlook 2007

    • 1 Comments

    Want to create your own nuggets of wisdom? Want it to look and feel like patterns&practices nuggets of wisdom look and feel? Want to reuse it, mix and match with existing ones? It is easy and fast with Outlook 2007.

    I will show how I extend my knowledge base with a snap using Outlook 2007’s Quick Parts feature and predefined item templates that come with patterns&practices Guidance Explorer [GE].

    I must thank Dor Rotman who pointed me to Quick Parts feature.

    Summary of steps

    • Step #1 – Create Quick Parts templates
    • Step #2 – Compose new items based on the templates
    • Step #3 – Test your work

    Following section describes each step in details

    • Step #1 – Create Quick Parts templates. Start GE – refer to Consume patterns&practices Guidance Explorer Via RSS Using Outlook 2007 for details. Right click on “My Library” node in the treevew on the left and choose “Question and Answer” template: clip_image002[4]

      Highlight entire text in the editor and copy it into clipboard [ctrl + C]. Switch to Outlook 2007. Click on any folder in the treeview. Press Ctrl + Shift + S to bring up new “Post in This Folder” and paste the text in the clipboard. Add attributes part to the end – you can copy and paste it from existing GE items

      clip_image002[8]

      While in the post, highlight the entire text [Ctrl + A] and click on Insert tab, then click on “Quick Parts” ribbon to expand it. Click “Save Selection to Quick Part Gallery” found in the bottom. Give it a name “Question & Answer”, create new category “GE” and hit OK to save it as a Quick Part template:

      clip_image006

      Close the post – do not save it.

      Create few other templates following the procedure.

    • Step #2 – Compose new items based on the templates. Switch to Outlook 2007. Click on “Guidance Explorer Library” folder found in Favorites Folder [I assume you followed instructions in Consume patterns&practices Guidance Explorer Via RSS Using Outlook 2007]. Press Ctrl + Shift + S to bring new “Post in This Folder”. In the Subject line type “How To Call Police”. Click on Insert tab and then click on “Quick Parts” ribbon to expand it. Locate “Question & Answer” quick part you just created in Step #1 and click it. The content of the part fills in into the body of the post. Modify it to your needs, make sure to update the category found under Attributes:

      clip_image002[10]

      Press Ctrl + Enter to post it to the folder.

    • Step #3 – Test your work. Click on “Security” folder found in Favorite Folders, you should see the newly created item there:

      clip_image002[12]

    You’ve just created new how-to item that is part of your own GE library managed inside Outlook 2007

Page 2 of 4 (10 items) 1234