Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop instantiating Stopwatch #31348

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 34 additions & 35 deletions src/EFCore.Relational/Storage/RelationalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Microsoft.EntityFrameworkCore.Storage;
public class RelationalCommand : IRelationalCommand
{
private RelationalDataReader? _relationalReader;
private readonly Stopwatch _stopwatch = new();

/// <summary>
/// <para>
Expand Down Expand Up @@ -81,12 +80,12 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj

connection.Open();

var stopwatch = SharedStopwatch.StartNew();

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();

var interceptionResult = logger?.CommandNonQueryExecuting(
connection,
command,
Expand All @@ -109,7 +108,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
connection.ConnectionId,
nonQueryResult,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource)
?? nonQueryResult;
}
Expand All @@ -130,7 +129,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -144,7 +143,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}

Expand Down Expand Up @@ -183,12 +182,12 @@ public virtual async Task<int> ExecuteNonQueryAsync(

await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

var stopwatch = SharedStopwatch.StartNew();

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();

var interceptionResult = logger == null
? default
: await logger.CommandNonQueryExecutingAsync(
Expand Down Expand Up @@ -216,7 +215,7 @@ public virtual async Task<int> ExecuteNonQueryAsync(
connection.ConnectionId,
result,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -243,7 +242,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -259,7 +258,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -295,12 +294,12 @@ await logger.CommandErrorAsync(

connection.Open();

var stopwatch = SharedStopwatch.StartNew();

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();

var interceptionResult = logger?.CommandScalarExecuting(
connection,
command,
Expand All @@ -323,7 +322,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
result,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource)
?? result;
}
Expand All @@ -344,7 +343,7 @@ await logger.CommandErrorAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -358,7 +357,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}

Expand Down Expand Up @@ -397,12 +396,12 @@ await logger.CommandErrorAsync(

await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

var stopwatch = SharedStopwatch.StartNew();

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();

var interceptionResult = logger == null
? default
: await logger.CommandScalarExecutingAsync(
Expand Down Expand Up @@ -430,7 +429,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
result,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken).ConfigureAwait(false);
}
Expand All @@ -456,7 +455,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -472,7 +471,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -515,12 +514,12 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
var readerOpen = false;
DbDataReader reader;

var stopwatch = SharedStopwatch.StartNew();

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();

var interceptionResult = logger!.CommandReaderExecuting(
connection,
command,
Expand All @@ -542,7 +541,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
connection.ConnectionId,
reader,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -562,7 +561,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -576,7 +575,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}

Expand Down Expand Up @@ -643,12 +642,12 @@ public virtual async Task<RelationalDataReader> ExecuteReaderAsync(
var readerOpen = false;
DbDataReader reader;

var stopwatch = SharedStopwatch.StartNew();

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();

var interceptionResult = await logger!.CommandReaderExecutingAsync(
connection,
command,
Expand All @@ -672,7 +671,7 @@ public virtual async Task<RelationalDataReader> ExecuteReaderAsync(
connection.ConnectionId,
reader,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -696,7 +695,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -712,7 +711,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -776,11 +775,11 @@ public virtual DbCommand CreateDbCommand(

DbCommand command;

var stopwatch = SharedStopwatch.StartNew();

var logCommandCreate = logger?.ShouldLogCommandCreate(startTime) == true;
if (logCommandCreate)
{
_stopwatch.Restart();

var interceptionResult = logger!.CommandCreating(
connection, commandMethod, context, commandId, connectionId, startTime,
parameterObject.CommandSource);
Expand All @@ -797,7 +796,7 @@ public virtual DbCommand CreateDbCommand(
commandId,
connectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand Down Expand Up @@ -832,7 +831,7 @@ public virtual DbCommand CreateDbCommand(
commandId,
connectionId,
startTime,
_stopwatch.Elapsed,
stopwatch.Elapsed,
parameterObject.CommandSource);
}

Expand Down
Loading
Loading