Skip to content

Commit

Permalink
Switch to using SharedStopwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jul 26, 2023
1 parent b5b3e16 commit 8627125
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 155 deletions.
80 changes: 27 additions & 53 deletions src/EFCore.Relational/Storage/RelationalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj

connection.Open();

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

var interceptionResult = logger?.CommandNonQueryExecuting(
connection,
command,
Expand All @@ -110,7 +108,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
connection.ConnectionId,
nonQueryResult,
startTime,
Stopwatch.GetElapsedTime(startTimestamp),
stopwatch.Elapsed,
parameterObject.CommandSource)
?? nonQueryResult;
}
Expand All @@ -121,8 +119,6 @@ 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 @@ -133,7 +129,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
commandId,
connection.ConnectionId,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -147,7 +143,7 @@ public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObj
connection.ConnectionId,
exception,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource);
}

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

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

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

var interceptionResult = logger == null
? default
: await logger.CommandNonQueryExecutingAsync(
Expand Down Expand Up @@ -221,7 +215,7 @@ public virtual async Task<int> ExecuteNonQueryAsync(
connection.ConnectionId,
result,
startTime,
Stopwatch.GetElapsedTime(startTimestamp),
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -236,8 +230,6 @@ 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 @@ -250,7 +242,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -266,7 +258,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -302,14 +294,12 @@ await logger.CommandErrorAsync(

connection.Open();

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

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

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

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

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

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

var interceptionResult = logger == null
? default
: await logger.CommandScalarExecutingAsync(
Expand Down Expand Up @@ -443,7 +429,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
result,
startTime,
Stopwatch.GetElapsedTime(startTimestamp),
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken).ConfigureAwait(false);
}
Expand All @@ -457,8 +443,6 @@ 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 @@ -471,7 +455,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -487,7 +471,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -530,14 +514,12 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
var readerOpen = false;
DbDataReader reader;

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

var interceptionResult = logger!.CommandReaderExecuting(
connection,
command,
Expand All @@ -559,7 +541,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
connection.ConnectionId,
reader,
startTime,
Stopwatch.GetElapsedTime(startTimestamp),
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -569,8 +551,6 @@ 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 @@ -581,7 +561,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
commandId,
connection.ConnectionId,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource);
}
else
Expand All @@ -595,7 +575,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
connection.ConnectionId,
exception,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource);
}

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

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

var interceptionResult = await logger!.CommandReaderExecutingAsync(
connection,
command,
Expand All @@ -693,7 +671,7 @@ public virtual async Task<RelationalDataReader> ExecuteReaderAsync(
connection.ConnectionId,
reader,
startTime,
Stopwatch.GetElapsedTime(startTimestamp),
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -705,8 +683,6 @@ 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 @@ -719,7 +695,7 @@ await logger.CommandCanceledAsync(
commandId,
connection.ConnectionId,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand All @@ -735,7 +711,7 @@ await logger.CommandErrorAsync(
connection.ConnectionId,
exception,
startTime,
elapsedTime,
stopwatch.Elapsed,
parameterObject.CommandSource,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -799,13 +775,11 @@ public virtual DbCommand CreateDbCommand(

DbCommand command;

long startTimestamp = 0;
var stopwatch = SharedStopwatch.StartNew();

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

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

Expand Down
Loading

0 comments on commit 8627125

Please sign in to comment.