Skip to content

Commit 9b25148

Browse files
committed
Fix Stopwatch elapsed time conversion
Stopwatch ticks are not TimeSpan ticks when Stopwatch.IsHighResolution is true (most systems). TimeSpan.FromTicks(stopwatchTicks) produces incorrect durations. Use Stopwatch.GetElapsedTime() which correctly handles the frequency conversion.
1 parent 2968ea3 commit 9b25148

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

src/FirebirdSql.Data.FirebirdClient/Metrics/FbMetricsStore.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ internal static void CommandStop(long startedAtTicks, FbConnection connection)
7474
{
7575
if (OperationDuration.Enabled && startedAtTicks > 0)
7676
{
77-
var elapsedTicks = Stopwatch.GetTimestamp() - startedAtTicks;
78-
var elapsedSeconds = TimeSpan.FromTicks(elapsedTicks).TotalSeconds;
77+
var elapsed = Stopwatch.GetElapsedTime(startedAtTicks);
7978

80-
OperationDuration.Record(elapsedSeconds, connection.MetricsConnectionAttributes);
79+
OperationDuration.Record(elapsed.TotalSeconds, connection.MetricsConnectionAttributes);
8180
}
8281
}
8382

@@ -87,10 +86,9 @@ internal static void ConnectionOpened(long startedAtTicks, string poolName)
8786
{
8887
if (ConnectionCreateTime.Enabled && startedAtTicks > 0)
8988
{
90-
var elapsedTicks = Stopwatch.GetTimestamp() - startedAtTicks;
91-
var elapsedSeconds = TimeSpan.FromTicks(elapsedTicks).TotalSeconds;
89+
var elapsed = Stopwatch.GetElapsedTime(startedAtTicks);
9290

93-
ConnectionCreateTime.Record(elapsedSeconds, [new(ConnectionPoolNameAttributeName, poolName)]);
91+
ConnectionCreateTime.Record(elapsed.TotalSeconds, [new(ConnectionPoolNameAttributeName, poolName)]);
9492
}
9593
}
9694

0 commit comments

Comments
 (0)