Skip to content

Logging a query's duration time if Logging on a debug level is enabled. - #3194

Merged
roji merged 5 commits into
npgsql:mainfrom
ch-asimakopoulos:logQueryDurationTime_#2282
Oct 8, 2020
Merged

roji merged 5 commits into
npgsql:mainfrom
ch-asimakopoulos:logQueryDurationTime_#2282

Conversation

@ch-asimakopoulos

Copy link
Copy Markdown
Contributor

Closes #2282

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments below and also the general design question in #2282 (comment).

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated

connector.UserTimeout = CommandTimeout * 1000;

// If statement logging is enabled, start a StopWatch to log the query's duration time.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated
if (Log.IsEnabled(NpgsqlLogLevel.Debug))
{
stopWatch.Stop();
LogQueryDuration(ms: stopWatch.ElapsedMilliseconds);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need to have ms here as a named parameter

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated
else
reader.NextResult();

if (Log.IsEnabled(NpgsqlLogLevel.Debug))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@ch-asimakopoulos
ch-asimakopoulos requested a review from roji October 4, 2020 10:02

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! See minor nits below.

Comment thread src/Npgsql/NpgsqlConnector.cs Outdated

static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlConnector));

internal Stopwatch QueryLogStopWatch = new Stopwatch();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: make this a read-only property

Comment thread src/Npgsql/NpgsqlDataReader.cs Outdated
Connector.CurrentReader = null;
if (Log.IsEnabled(NpgsqlLogLevel.Debug)) {
Connector.QueryLogStopWatch.Stop();
Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds} ms", Connector.Id);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds} ms", Connector.Id);
Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds}ms", Connector.Id);

@ch-asimakopoulos
ch-asimakopoulos requested a review from roji October 5, 2020 16:09
@ch-asimakopoulos

Copy link
Copy Markdown
Contributor Author

@roji Is this good to go now?

@roji
roji merged commit 8b88d9d into npgsql:main Oct 8, 2020
@roji

roji commented Oct 8, 2020

Copy link
Copy Markdown
Member

Thanks @ch-asimakopoulos!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Npgsql logging doesn't log queries duration time

2 participants