Skip to content

Commit

Permalink
[Perf] Call Run() once before starting recording (#24263)
Browse files Browse the repository at this point in the history
- Avoids capturing one-time setup like authorization requests
  • Loading branch information
mikeharder authored Sep 28, 2021
1 parent bfd3109 commit 18015f5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
14 changes: 4 additions & 10 deletions common/Perf/Azure.Sample.Perf/HttpClientGetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

namespace Azure.Sample.Perf
{
public class HttpClientGetTest : PerfTest<HttpClientGetTest.HttpClientGetOptions>
public class HttpClientGetTest : PerfTest<UriOptions>
{
private static HttpClient _httpClient;

public HttpClientGetTest(HttpClientGetOptions options) : base(options)
public HttpClientGetTest(UriOptions options) : base(options)
{
}

Expand All @@ -35,18 +35,12 @@ public override Task GlobalSetupAsync()

public override void Run(CancellationToken cancellationToken)
{
_httpClient.GetStringAsync(Options.Url).Wait();
_httpClient.GetStringAsync(Options.Uri).Wait();
}

public override async Task RunAsync(CancellationToken cancellationToken)
{
await _httpClient.GetStringAsync(Options.Url);
}

public class HttpClientGetOptions : PerfOptions
{
[Option('u', "url", Required = true)]
public string Url { get; set; }
await _httpClient.GetStringAsync(Options.Uri);
}
}
}
77 changes: 77 additions & 0 deletions common/Perf/Azure.Sample.Perf/HttpPipelineGetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Test.Perf;
using CommandLine;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Sample.Perf
{
public class HttpPipelineGetTest : PerfTest<HttpPipelineGetTest.HttpPipelineGetOptions>
{
private HttpPipeline _httpPipeline;
private bool _firstRun = true;

public HttpPipelineGetTest(HttpPipelineGetOptions options) : base(options)
{
_httpPipeline = HttpPipelineBuilder.Build(ConfigureClientOptions(new HttpPipelineGetClientOptions()));
}

public override void Run(CancellationToken cancellationToken)
{
Response response;

if (_firstRun)
{
for (var i=0; i < Options.FirstRunExtraRequests; i++)
{
response = _httpPipeline.SendRequest(CreateRequest(), cancellationToken);
response.ContentStream.CopyTo(Stream.Null);
}
_firstRun = false;
}

response = _httpPipeline.SendRequest(CreateRequest(), cancellationToken);
response.ContentStream.CopyTo(Stream.Null);
}

public override async Task RunAsync(CancellationToken cancellationToken)
{
Response response;

if (_firstRun)
{
for (var i=0; i < Options.FirstRunExtraRequests; i++)
{
response = await _httpPipeline.SendRequestAsync(CreateRequest(), cancellationToken);
await response.ContentStream.CopyToAsync(Stream.Null);
}
_firstRun = false;
}

response = await _httpPipeline.SendRequestAsync(CreateRequest(), cancellationToken);
await response.ContentStream.CopyToAsync(Stream.Null);
}

// Request object should not be re-used across distinct requests
private Request CreateRequest()
{
var request = _httpPipeline.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri.Reset(Options.Uri);
return request;
}

private class HttpPipelineGetClientOptions : ClientOptions { }

public class HttpPipelineGetOptions : UriOptions {
[Option("first-run-extra-requests", Default = 0, HelpText = "Extra requests to send on first run. " +
"Simulates SDKs which require extra requests (like authentication) on first API call.")]
public int FirstRunExtraRequests { get; set; }
}
}
}
15 changes: 15 additions & 0 deletions common/Perf/Azure.Sample.Perf/UriOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Test.Perf;
using CommandLine;
using System;

namespace Azure.Sample.Perf
{
public class UriOptions : PerfOptions
{
[Option('u', "uri", Required = true)]
public Uri Uri { get; set; }
}
}
10 changes: 10 additions & 0 deletions common/Perf/Azure.Test.Perf/PerfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public virtual Task SetupAsync()

public async Task RecordAndStartPlayback()
{
// Make one call to Run() before starting recording, to avoid capturing one-time setup like authorization requests.
if (Options.Sync)
{
Run(CancellationToken.None);
}
else
{
await RunAsync(CancellationToken.None);
}

await StartRecording();

_testProxyPolicy.RecordingId = _recordingId;
Expand Down

0 comments on commit 18015f5

Please sign in to comment.