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

Performance diagnostics: Adds static timer and caches handler name #1977

Merged
merged 8 commits into from
Nov 3, 2020
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
21 changes: 14 additions & 7 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosDiagnosticScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,44 @@ namespace Microsoft.Azure.Cosmos.Diagnostics
/// </summary>
internal sealed class CosmosDiagnosticScope : CosmosDiagnosticsInternal, IDisposable
{
private readonly Stopwatch ElapsedTimeStopWatch;
private static readonly Stopwatch SingletonTimer = Stopwatch.StartNew();
private readonly TimeSpan startTimeSpan = CosmosDiagnosticScope.SingletonTimer.Elapsed;
private TimeSpan? elapsedTimeSpan = null;

private bool isDisposed = false;

public CosmosDiagnosticScope(
string name)
{
this.Id = name;
this.ElapsedTimeStopWatch = Stopwatch.StartNew();
}

public string Id { get; }

public bool TryGetElapsedTime(out TimeSpan elapsedTime)
{
if (!this.isDisposed)
if (!this.isDisposed || !this.elapsedTimeSpan.HasValue)
{
return false;
}

elapsedTime = this.ElapsedTimeStopWatch.Elapsed;
elapsedTime = this.elapsedTimeSpan.Value;
return true;
}

internal TimeSpan GetElapsedTime()
{
return this.ElapsedTimeStopWatch.Elapsed;
if (this.elapsedTimeSpan.HasValue)
{
return this.elapsedTimeSpan.Value;
}

return CosmosDiagnosticScope.SingletonTimer.Elapsed - this.startTimeSpan;
}

internal bool IsComplete()
{
return !this.ElapsedTimeStopWatch.IsRunning;
return this.elapsedTimeSpan.HasValue;
}

public void Dispose()
Expand All @@ -54,7 +61,7 @@ public void Dispose()
return;
}

this.ElapsedTimeStopWatch.Stop();
this.elapsedTimeSpan = CosmosDiagnosticScope.SingletonTimer.Elapsed - this.startTimeSpan;
this.isDisposed = true;
}

Expand Down
18 changes: 10 additions & 8 deletions Microsoft.Azure.Cosmos/src/Diagnostics/RequestHandlerScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ namespace Microsoft.Azure.Cosmos.Diagnostics

internal sealed class RequestHandlerScope : CosmosDiagnosticsInternal, IDisposable
{
private readonly Stopwatch ElapsedTimeStopWatch;
private static readonly Stopwatch SingletonTimer = Stopwatch.StartNew();
j82w marked this conversation as resolved.
Show resolved Hide resolved
private readonly TimeSpan startTimeSpan = RequestHandlerScope.SingletonTimer.Elapsed;
private TimeSpan? elapsedTimeSpan = null;

private bool isDisposed = false;

public RequestHandlerScope(RequestHandler handler)
Expand All @@ -19,31 +22,30 @@ public RequestHandlerScope(RequestHandler handler)
throw new ArgumentNullException(nameof(handler));
}

this.Id = handler.GetType().FullName;
this.ElapsedTimeStopWatch = Stopwatch.StartNew();
this.Id = handler.FullHandlerName;
}

public string Id { get; }

public bool TryGetTotalElapsedTime(out TimeSpan elapsedTime)
{
if (!this.isDisposed)
if (!this.isDisposed || !this.elapsedTimeSpan.HasValue)
{
return false;
}

elapsedTime = this.ElapsedTimeStopWatch.Elapsed;
elapsedTime = this.elapsedTimeSpan.Value;
return true;
}

internal TimeSpan GetCurrentElapsedTime()
{
return this.ElapsedTimeStopWatch.Elapsed;
return RequestHandlerScope.SingletonTimer.Elapsed - this.startTimeSpan;
}

internal bool IsComplete()
{
return !this.ElapsedTimeStopWatch.IsRunning;
return this.elapsedTimeSpan.HasValue;
}

public void Dispose()
Expand All @@ -53,7 +55,7 @@ public void Dispose()
return;
}

this.ElapsedTimeStopWatch.Stop();
this.elapsedTimeSpan = RequestHandlerScope.SingletonTimer.Elapsed - this.startTimeSpan;
this.isDisposed = true;
}

Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Azure.Cosmos/src/Handler/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ namespace Microsoft.Azure.Cosmos
/// </remarks>
public abstract class RequestHandler
{
internal readonly string FullHandlerName;

/// <summary>
/// Defines a next handler to be called in the chain.
/// </summary>
public RequestHandler InnerHandler { get; set; }

/// <summary>
/// The default constructor for the RequestHandler
/// </summary>
public RequestHandler()
{
this.FullHandlerName = this.GetType().FullName;
}

/// <summary>
/// Processes the current <see cref="RequestMessage"/> in the current handler and sends the current <see cref="RequestMessage"/> to the next handler in the chain.
/// </summary>
Expand Down
29 changes: 5 additions & 24 deletions Microsoft.Azure.Cosmos/src/Handler/RouterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,18 @@ public RouterHandler(
RequestHandler documentFeedHandler,
RequestHandler pointOperationHandler)
{
if (documentFeedHandler == null)
{
throw new ArgumentNullException(nameof(documentFeedHandler));
}

if (pointOperationHandler == null)
{
throw new ArgumentNullException(nameof(pointOperationHandler));
}

this.documentFeedHandler = documentFeedHandler;
this.pointOperationHandler = pointOperationHandler;
this.documentFeedHandler = documentFeedHandler ?? throw new ArgumentNullException(nameof(documentFeedHandler));
this.pointOperationHandler = pointOperationHandler ?? throw new ArgumentNullException(nameof(pointOperationHandler));
}

public override Task<ResponseMessage> SendAsync(
public override async Task<ResponseMessage> SendAsync(
RequestMessage request,
CancellationToken cancellationToken)
{
RequestHandler targetHandler = null;
if (request.IsPartitionKeyRangeHandlerRequired)
{
targetHandler = this.documentFeedHandler;
}
else
{
targetHandler = this.pointOperationHandler;
}

RequestHandler targetHandler = request.IsPartitionKeyRangeHandlerRequired ? this.documentFeedHandler : this.pointOperationHandler;
using (request.DiagnosticsContext.CreateRequestHandlerScopeScope(targetHandler))
{
return targetHandler.SendAsync(request, cancellationToken);
return await targetHandler.SendAsync(request, cancellationToken);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4829,6 +4829,11 @@
],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ResponseMessage] SendAsync(Microsoft.Azure.Cosmos.RequestMessage, System.Threading.CancellationToken);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void .ctor()": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "[Void .ctor(), Void .ctor()]"
},
"Void set_InnerHandler(Microsoft.Azure.Cosmos.RequestHandler)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
Expand Down