Third Party Integration - Using PostSharp with Loupe - Best Practices
In This Topic
The Loupe Agent for PostSharp is a powerful tool with many uses in development, testing and in production.
- In development Loupe aspects can easily be applied to individual methods or whole assemblies to quickly pinpoint the source of an elusive bug or characterize the nature of a performance bottleneck.
- In testing Loupe can track the details that simplify reproduction of failed tests or verify that critical subsystems are meeting performance requirements.
- In production Loupe will efficiently record critical application performance and feature usage metrics that allow you to better understand your users and how they interact with your software.
Best Practices during Development
- Apply GException everywhere to record every exception thrown, even if it is ultimately caught.
This draws attention to poor coding practices that use exceptions for flow of control. More importantly, it ensures that you won't waste days tracking down a mystery bug resulting from a silent swallowed exception.
- Use GTrace to track detailed control flow leading up to a non-obvious error.
Consider using the static Enable property on each Loupe aspect to enable/disable logging at run-time. For example, you might start your application with GTrace disabled during application initialization. Then enable GTrace right before executing the reproduction steps leading to the error.
- Apply GFeature to critical or complex methods
to understand how often it's being called and to understand the relationships between input arguments and execution time. For example, GFeature could be applied to a database engine wrapper to instantly track the duration of each database request and correlate that with query arguments.
- Apply GField to class variables to monitor critical state changes.
For example, GField could be used to track when and why objects are being marked dirty.
Develop an Understanding of your Performance
- Apply GTimer broadly to get a sense of what parts of your application are consuming time.
But, be aware that tracking entry and exit of every method and property in an assembly can dramatically reduce application performance. Start with a short application run and study the output in Loupe Desktop to identify which methods are worth monitoring and which aren't. Use multicast filtering to narrow the focus and achieve the right balance of detail and performance.
Best Practices during Testing
- Add critical performance metrics for your application during requirements analysis and design.
Common examples include application initialization, file load/save operations, database operations, network operations, page load time, and query response time. Define methods that encapsulate the bulk of work associated with each of these operations and use GTimer or GFeature to monitor it. GFeature provides more detailed information at slightly higher performance cost.
- Consider applying GTrace fairly broadly to substantive methods
You don't want to instrument trivial methods such as simple property setters/getters.
Have GTrace disabled by default and build a mechanism into your application to enable GTrace when desired to selectively record detailed logging related to documenting bugs found during testing.
You may want to disable gathering object details for parameters to improve performance if you have any objects that override ToString (set LogParameterDetails = false)
Best Practices in Production
Know what people are doing with your application
- Use GFeature to track feature usage by applying it to a layer in your application.
Pick a layer where the overhead of recording the feature information is trivial compared to the work done in the feature.
Finding Troublesome Defects
- Make sure you have GException applied everywhere.
Many rare problems start as an unexpected exception - it may be of a different type than you expect or in a place you never had an opportunity to verify your exception handling.
- Use GField to track changes to critical state information such the number of concurrent worker threads being used.
Finding Performance Problems
If your application performance is not what you were expecting, don't guess where the time is going.
- Apply GTimer to suspected slow areas and key checkpoints in the application to narrow down where time is being used.
- Use GTimer instead of GFeature for slightly faster performance in cases where you’re interested in tracking method execution but do not require details on parameters and return value for each call.
- Make sure you age GFeature set up for key features to track the performance relative to the specific input values to identify scenarios where performance changes substantially based on data.
See Also
Developer's Guide
Third Party Integration