Wednesday, December 13, 2006

How to use ASP.NET controls inside a XSLT

I wrote an article at CodeProject explaining how to use ASP.NET controls inside a XSLT.

I think that this is a must read to anyone that uses XSLTs in their web applications and even to every ASP.NET developer!

Brief description of the solution

I used a HttpModule to capture all requests made to all ASP.NET pages and made the XSLT/XML transformations before the request arrives at the PageHandlerFactory, in the HttpPipeline, and the ASP.NET page is dynamically created.

I have a demo project showing this solution, but if you want to look only at the HttpModule's code, I've posted the source code of the HttpModule only as well.

Read the article and leave a comment!

Tell me what you think!

Read the article here

Posted by BCoelho2000 at 22:52:07 | Permanent Link | Comments (0) |

Thursday, August 31, 2006

The performance of System.Xml - Insert operations

In this post, I will analyse the ways that you can add nodes to a XmlDocument and wich way is the fastest, using the .NET framework 1.1.

How can we add nodes to a XmlDocument?

There are four ways for you to add nodes to a XmlDocument:

  • XmlDocument.InsertAfter
  • XmlDocument.InsertBefore
  • XmlDocument.Append
  • XmlDocument.PrependChild

So, wich method is the fastest?

To answer that question I've created a test program that uses the content of a xml file (11KB) to create a XmlDocument in memory. Then the user can choose wich method he wants to test. The results are measured in nanoseconds.

How did you make the performance test?

One could easily say that the easiest way to make a performance test is something like this:

DateTime before = DateTime.Now;

// do the test...

DateTime res = before.Subtract(DateTime.Now);

Unfortunately, this code forgets that there are threads running in your system. Our performance test application isn't running all alone and most important, it isn't running all the time.

Hence, a good performance test must take this issues into account.

My performance test uses the following approach:

  1. There are 2 threads. One thread is sleeping for a certaing period of time. I like to call it the TimerThread. While it's sleeping the other thread is invoking the method to test, over and over again, until the first thread wakes up.
  2. The number of times that the method was invoked is registered in a variable.
  3. The TimeThread executes the following code: (timeThatWasSleeping*1000000)/counter. The counter variable holds the number of times that the method to be tested was called.
  4. The time in nanoseconds that the TimerThread calculates is the time that the method takes to finish.

For minimizing the scheduler interference in the tests I've give ThreadPriority.Highest to the thread that is making the test. However if you run the tests multiple times you can get different values because of this.

The results

  • AppendChild - 38ns
  • InsertAfter - 12ns
  • InsertBefore - 13ns
  • PrependChild - 14 ns

Conclusions

The InsertAfter, InsertBefore and PrependChild methods have basicly the same performance results. The AppendChild is a little slower.

One interesting conclusion is the diference between the AppendChild and the PrependChild. The first "adds the specified node to the end of the list of child nodes, of this node." and the second "Adds the specified node to the beginning of the list of child nodes for this node." Hence, it's faster to insert nodes to the begining of the list of child nodes that to the end.

The challenge

This tests were made with a Intel Pentium M processor 1500Mhz and 512 MB of RAM running Windows Xp Pro. I would like to challenge everyone who would like to run this tests and post the here results.

Here's the source code or get directly the exe and the xml file used in the tests here.

Remenber to put the xml file in the same folder that the exe file.

Hope that you can improve your code with this performance test.

Posted by BCoelho2000 at 21:22:51 | Permanent Link | Comments (1) |