Profiling
You can use the logging infrastructure to perform simple profiling.
Query Patterns
You can review the SQL logs to determine what queries LightSpeed is issuing and to look for inefficient patterns such as N+1 lazy loads. When looking for performance bottlenecks, it can be wise to identify a piece of code you want to profile, and turn logging off after executing that code – otherwise you may end up with a very large SQL trace, making it to track down whether you have traced a single inefficient run of code or a large number of efficient runs!
Timing Queries
When LightSpeed logs a SQL statement or batch, it also prints out the time taken to execute that statement. You can also access the time taken directly using the CommandLog.TimeTaken property.
Using a Custom Logger for Profiling
In some cases you may be able to use a custom logger to search for patterns or problems by drilling into the CommandLog object. For example, to detect poorly-performing queries which might be candidates for tuning at the database level or refactoring into stored procedures, you could create a custom logger which logs only queries that take a long time to run. Or to detect N+1 problems you could create a logger which logs SQL statements on a per unit of work basis, and highlights where a unit of work has executed more statements than a threshold.
A custom logger which uses CommandLog properties to detect slow queries |
public class AlertingLogger : ILogger |
Using Profilers with the Interception API
LightSpeed 5 introduces a dedicated API for profiling internal performance. This exposes LightSpeed events to external profiling tools, such as the ASP.NET MVC Mini-Profiler, allowing them to intercept and modify the events. The API is available in the Mindscape.LightSpeed.Profiling namespace, which contains the Interceptor class. To connect a profiler to LightSpeed, you need to provide an implementation of this, such as in the figure below.
An interceptor that sends LightSpeed events to ASP.NET MVC Mini-Profiler |
public class MiniProfilerInterceptor : Interceptor |
You can then attach the profiler through the web.config using the new interceptorClass attribute. Alternatively, this can be done by setting the LightSpeedContext.Interceptor property.
In web.config |
<lightSpeedContexts> |