Feb. 19, 2011, 11:57 a.m.
posted by routh
Using Trace to Measure Time
Our string concatenation exercise introduces a quick and dirty way to measure the time it takes to complete an operation in the execution of your page. When trace is on for the page or your application, every item in the trace output is marked with two times, once from the start of execution, and once from the previous item.
Because we've talked a lot about application layering and breaking out your "heavy lifting" into its own application layer, you might be writing code outside of the context of a page, and therefore outside of the normal ASP.NET runtime. To get around this, remember that you can access most of the same objects by accessing HttpContext.Current. This provides a reference, cleverly enough, to the current instance of the HttpContext in which your code is running. You can access Cache, TRace, and the usual suspects this way.
If you find your page is not responsive when you're testing, try surrounding an action in TRace.Warn() (or trace.Write()) methods to output messages to the page trace, as we did in Listing 15.2. Typical slow points might include database access, calls to Web services, or generation of dynamic images.