Repository navigation
Logging a query's duration time if Logging on a debug level is enabled. - #3194
Conversation
roji
left a comment
There was a problem hiding this comment.
See comments below and also the general design question in #2282 (comment).
|
|
||
| connector.UserTimeout = CommandTimeout * 1000; | ||
|
|
||
| // If statement logging is enabled, start a StopWatch to log the query's duration time. |
There was a problem hiding this comment.
This can all move into the LogCommand method that's already called above, when debug logging is enabled. This would executing the same condition twice (loglevel check), and wouldn't allocate the Stopwatch on the heap unless needed.
In addition, since Stopwatch is class and so allocated on the heap, we shouldn't be allocating a new one every time a command is executed. We can have a single Stopwatch instance on the connector and reuse it many times.
| if (Log.IsEnabled(NpgsqlLogLevel.Debug)) | ||
| { | ||
| stopWatch.Stop(); | ||
| LogQueryDuration(ms: stopWatch.ElapsedMilliseconds); |
There was a problem hiding this comment.
nit: no need to have ms here as a named parameter
| else | ||
| reader.NextResult(); | ||
|
|
||
| if (Log.IsEnabled(NpgsqlLogLevel.Debug)) |
There was a problem hiding this comment.
This is still an added check to the hot path, which I'm not very happy about - but it's going to be hard to get rid of that. We could put all the code between the start and end in a local function, and call that either with or without the stopwatch. But that's a bit extreme, I will try to measure the perf impact of this on the TechEmpower platform.
There was a problem hiding this comment.
@roji I thought about putting all of this in a local function and call it with or w/o the stopwatch but I thought that it would be an overkill. I am not really an expert on this, but I am curious to see the perf impact this has.
There was a problem hiding this comment.
I'm not convinced it's not overkill either... But in any case we have the outstanding design question which may make this question moot.
…till waiting on the decision regarding regarding stopping the counter when the first command is executed vs stop measuring when the reader is fully consumed
… command starting and the cleanup of the reader
roji
left a comment
There was a problem hiding this comment.
Thanks, looks good! See minor nits below.
|
|
||
| static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlConnector)); | ||
|
|
||
| internal Stopwatch QueryLogStopWatch = new Stopwatch(); |
There was a problem hiding this comment.
nit: make this a read-only property
| Connector.CurrentReader = null; | ||
| if (Log.IsEnabled(NpgsqlLogLevel.Debug)) { | ||
| Connector.QueryLogStopWatch.Stop(); | ||
| Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds} ms", Connector.Id); |
There was a problem hiding this comment.
| Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds} ms", Connector.Id); | |
| Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds}ms", Connector.Id); |
|
@roji Is this good to go now? |
|
Thanks @ch-asimakopoulos! |
Closes #2282