Skip to content

Commit

Permalink
Stop instantiating Stopwatch
Browse files Browse the repository at this point in the history
Closes #26295
  • Loading branch information
roji committed Jul 26, 2023
1 parent a1cd4f4 commit b5b3e16
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 96 deletions.
81 changes: 53 additions & 28 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,11 +80,13 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj

connection.Open();

long startTimestamp = 0;

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();
startTimestamp = Stopwatch.GetTimestamp();

var interceptionResult = logger?.CommandNonQueryExecuting(
connection,
Expand All @@ -109,7 +110,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
connection.ConnectionId,
nonQueryResult,
startTime,
_stopwatch.Elapsed,
Stopwatch.GetElapsedTime(startTimestamp),
parameterObject.CommandSource)
?? nonQueryResult;
}
Expand All @@ -120,6 +121,8 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
}
catch (Exception exception)
{
var elapsedTime = startTimestamp > 0 ? Stopwatch.GetElapsedTime(startTimestamp) : TimeSpan.Zero;

if (Dependencies.ExceptionDetector.IsCancellation(exception))
{
logger?.CommandCanceled(
Expand All @@ -130,7 +133,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource);
}
else
Expand All @@ -144,7 +147,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource);
}

Expand Down Expand Up @@ -183,11 +186,13 @@ public virtual async Task<int> ExecuteNonQueryAsync(

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

long startTimestamp = 0;

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();
startTimestamp = Stopwatch.GetTimestamp();

var interceptionResult = logger == null
? default
Expand Down Expand Up @@ -216,7 +221,7 @@ public virtual async Task<int> ExecuteNonQueryAsync(
connection.ConnectionId,
result,
startTime,
_stopwatch.Elapsed,
Stopwatch.GetElapsedTime(startTimestamp),
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -231,6 +236,8 @@ public virtual async Task<int> ExecuteNonQueryAsync(
}
catch (Exception exception)
{
var elapsedTime = startTimestamp > 0 ? Stopwatch.GetElapsedTime(startTimestamp) : TimeSpan.Zero;

if (logger != null)
{
if (Dependencies.ExceptionDetector.IsCancellation(exception, cancellationToken))
Expand All @@ -243,7 +250,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -259,7 +266,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -295,11 +302,13 @@ await logger.CommandErrorAsync(

connection.Open();

long startTimestamp = 0;

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();
startTimestamp = Stopwatch.GetTimestamp();

var interceptionResult = logger?.CommandScalarExecuting(
connection,
Expand All @@ -323,7 +332,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
result,
startTime,
_stopwatch.Elapsed,
Stopwatch.GetElapsedTime(startTimestamp),
parameterObject.CommandSource)
?? result;
}
Expand All @@ -334,6 +343,8 @@ await logger.CommandErrorAsync(
}
catch (Exception exception)
{
var elapsedTime = startTimestamp > 0 ? Stopwatch.GetElapsedTime(startTimestamp) : TimeSpan.Zero;

if (Dependencies.ExceptionDetector.IsCancellation(exception))
{
logger?.CommandCanceled(
Expand All @@ -344,7 +355,7 @@ await logger.CommandErrorAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource);
}
else
Expand All @@ -358,7 +369,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource);
}

Expand Down Expand Up @@ -397,11 +408,13 @@ await logger.CommandErrorAsync(

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

long startTimestamp = 0;

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();
startTimestamp = Stopwatch.GetTimestamp();

var interceptionResult = logger == null
? default
Expand Down Expand Up @@ -430,7 +443,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
result,
startTime,
_stopwatch.Elapsed,
Stopwatch.GetElapsedTime(startTimestamp),
parameterObject.CommandSource,
cancellationToken).ConfigureAwait(false);
}
Expand All @@ -444,6 +457,8 @@ await logger.CommandErrorAsync(
}
catch (Exception exception)
{
var elapsedTime = startTimestamp > 0 ? Stopwatch.GetElapsedTime(startTimestamp) : TimeSpan.Zero;

if (logger != null)
{
if (Dependencies.ExceptionDetector.IsCancellation(exception, cancellationToken))
Expand All @@ -456,7 +471,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -472,7 +487,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -515,11 +530,13 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
var readerOpen = false;
DbDataReader reader;

long startTimestamp = 0;

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();
startTimestamp = Stopwatch.GetTimestamp();

var interceptionResult = logger!.CommandReaderExecuting(
connection,
Expand All @@ -542,7 +559,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
connection.ConnectionId,
reader,
startTime,
_stopwatch.Elapsed,
Stopwatch.GetElapsedTime(startTimestamp),
parameterObject.CommandSource);
}
else
Expand All @@ -552,6 +569,8 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
}
catch (Exception exception)
{
var elapsedTime = startTimestamp > 0 ? Stopwatch.GetElapsedTime(startTimestamp) : TimeSpan.Zero;

if (Dependencies.ExceptionDetector.IsCancellation(exception))
{
logger?.CommandCanceled(
Expand All @@ -562,7 +581,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource);
}
else
Expand All @@ -576,7 +595,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource);
}

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

long startTimestamp = 0;

try
{
if (shouldLogCommandExecute)
{
_stopwatch.Restart();
startTimestamp = Stopwatch.GetTimestamp();

var interceptionResult = await logger!.CommandReaderExecutingAsync(
connection,
Expand All @@ -672,7 +693,7 @@ public virtual async Task<RelationalDataReader> ExecuteReaderAsync(
connection.ConnectionId,
reader,
startTime,
_stopwatch.Elapsed,
Stopwatch.GetElapsedTime(startTimestamp),
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -684,6 +705,8 @@ public virtual async Task<RelationalDataReader> ExecuteReaderAsync(
}
catch (Exception exception)
{
var elapsedTime = startTimestamp > 0 ? Stopwatch.GetElapsedTime(startTimestamp) : TimeSpan.Zero;

if (logger != null)
{
if (Dependencies.ExceptionDetector.IsCancellation(exception, cancellationToken))
Expand All @@ -696,7 +719,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -712,7 +735,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
_stopwatch.Elapsed,
elapsedTime,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -776,10 +799,12 @@ public virtual DbCommand CreateDbCommand(

DbCommand command;

long startTimestamp = 0;

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

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

Expand Down
Loading

0 comments on commit b5b3e16

Please sign in to comment.