Skip to content

Commit

Permalink
Start measuring Ping latency closer to sending request and remove Sto…
Browse files Browse the repository at this point in the history
…pwatch instance
  • Loading branch information
eduardobr committed May 23, 2021
1 parent f2a7671 commit e4328e4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/StackExchange.Redis/ResultProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes

public sealed class TimingProcessor : ResultProcessor<TimeSpan>
{
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;

public static TimerMessage CreateMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value = default(RedisValue))
{
return new TimerMessage(db, flags, command, value);
Expand All @@ -346,9 +348,9 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
TimeSpan duration;
if (message is TimerMessage timingMessage)
{
var watch = timingMessage.Watch;
watch.Stop();
duration = watch.Elapsed;
var timestampDelta = Stopwatch.GetTimestamp() - timingMessage.StartedWritingTimestamp;
var ticks = (long)(TimestampToTicks * timestampDelta);
duration = new TimeSpan(ticks);
}
else
{
Expand All @@ -361,17 +363,17 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes

internal sealed class TimerMessage : Message
{
public readonly Stopwatch Watch;
public long StartedWritingTimestamp;
private readonly RedisValue value;
public TimerMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value)
: base(db, flags, command)
{
Watch = Stopwatch.StartNew();
this.value = value;
}

protected override void WriteImpl(PhysicalConnection physical)
{
StartedWritingTimestamp = Stopwatch.GetTimestamp();
if (value.IsNull)
{
physical.WriteHeader(command, 0);
Expand Down

0 comments on commit e4328e4

Please sign in to comment.