Skip to content

Commit 11ab9fe

Browse files
authored
Add download perf tests (Azure#17785)
1 parent 6c7e359 commit 11ab9fe

File tree

7 files changed

+209
-0
lines changed

7 files changed

+209
-0
lines changed

sdk/core/Azure.Core/Azure.Core.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.Core.Spatia
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.Core.Spatial.Tests", "..\Microsoft.Azure.Core.Spatial\tests\Microsoft.Azure.Core.Spatial.Tests.csproj", "{36FF59A9-D6C9-4226-A0FE-47C879F3EB94}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Core.Perf", "perf\Azure.Core.Perf.csproj", "{B6D7909F-4DE6-4895-BF39-EF892BA64BA3}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -75,6 +77,10 @@ Global
7577
{36FF59A9-D6C9-4226-A0FE-47C879F3EB94}.Debug|Any CPU.Build.0 = Debug|Any CPU
7678
{36FF59A9-D6C9-4226-A0FE-47C879F3EB94}.Release|Any CPU.ActiveCfg = Release|Any CPU
7779
{36FF59A9-D6C9-4226-A0FE-47C879F3EB94}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{B6D7909F-4DE6-4895-BF39-EF892BA64BA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81+
{B6D7909F-4DE6-4895-BF39-EF892BA64BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
82+
{B6D7909F-4DE6-4895-BF39-EF892BA64BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{B6D7909F-4DE6-4895-BF39-EF892BA64BA3}.Release|Any CPU.Build.0 = Release|Any CPU
7884
EndGlobalSection
7985
GlobalSection(SolutionProperties) = preSolution
8086
HideSolutionNode = FALSE
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="../../Azure.Core/src/Azure.Core.csproj" />
9+
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\..\..\common\Perf\Azure.Test.Perf\Azure.Test.Perf.csproj" />
10+
<ProjectReference Include="$(AzureCoreTestFramework)" />
11+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.IO;
6+
using System.Net.Http;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Azure.Core.Pipeline;
10+
using Azure.Identity;
11+
using Azure.Test.Perf;
12+
13+
namespace Azure.Template.Perf
14+
{
15+
public class DownloadHttpClientTest : PerfTest<DownloadTestOptions>
16+
{
17+
private static HttpClient _client;
18+
private static InProcTestServer _server;
19+
20+
public DownloadHttpClientTest(DownloadTestOptions options) : base(options)
21+
{
22+
_server ??= InProcTestServer.CreateStaticResponse(options.Size);
23+
_client ??= new HttpClient();
24+
}
25+
26+
public override void Run(CancellationToken cancellationToken)
27+
{
28+
#if NET5_0
29+
using var response = _client.Send(new HttpRequestMessage(HttpMethod.Get, _server.Address), Options.Buffer ? HttpCompletionOption.ResponseContentRead : HttpCompletionOption.ResponseHeadersRead, cancellationToken);
30+
response.Content.CopyTo(Stream.Null, null, cancellationToken);
31+
#else
32+
RunAsync(cancellationToken).GetAwaiter().GetResult();
33+
#endif
34+
}
35+
36+
public override async Task RunAsync(CancellationToken cancellationToken)
37+
{
38+
using var response = await _client.GetAsync(_server.Address, Options.Buffer ? HttpCompletionOption.ResponseContentRead : HttpCompletionOption.ResponseHeadersRead, cancellationToken);
39+
await response.Content.CopyToAsync(Stream.Null);
40+
}
41+
42+
public override void Dispose(bool disposing)
43+
{
44+
base.Dispose(disposing);
45+
_server.Dispose();
46+
}
47+
}
48+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.IO;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Azure.Core.Pipeline;
9+
using Azure.Identity;
10+
using Azure.Test.Perf;
11+
12+
namespace Azure.Template.Perf
13+
{
14+
public class DownloadTest : PerfTest<DownloadTestOptions>
15+
{
16+
// Stream.CopyToAsync default buffer
17+
private const int BufferSize = 81920;
18+
private static InProcTestServer _server;
19+
private static HttpPipeline _pipeline;
20+
21+
public DownloadTest(DownloadTestOptions options) : base(options)
22+
{
23+
_server ??= InProcTestServer.CreateStaticResponse(options.Size);
24+
_pipeline ??= HttpPipelineBuilder.Build(new DefaultAzureCredentialOptions());
25+
}
26+
27+
public override void Run(CancellationToken cancellationToken)
28+
{
29+
var message = _pipeline.CreateMessage();
30+
message.Request.Uri.Reset(_server.Address);
31+
32+
_pipeline.Send(message, cancellationToken);
33+
message.Response.ContentStream.CopyTo(Stream.Null, BufferSize);
34+
}
35+
36+
public override async Task RunAsync(CancellationToken cancellationToken)
37+
{
38+
var message = _pipeline.CreateMessage();
39+
message.BufferResponse = Options.Buffer;
40+
message.Request.Uri.Reset(_server.Address);
41+
42+
await _pipeline.SendAsync(message, cancellationToken);
43+
await message.Response.ContentStream.CopyToAsync(Stream.Null, BufferSize, cancellationToken);
44+
}
45+
46+
public override void Dispose(bool disposing)
47+
{
48+
base.Dispose(disposing);
49+
_server.Dispose();
50+
}
51+
}
52+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Test.Perf;
5+
using CommandLine;
6+
7+
namespace Azure.Template.Perf
8+
{
9+
public class DownloadTestOptions : SizeOptions
10+
{
11+
[Option("buffer", Default = false, HelpText = "Whether to buffer the response")]
12+
public bool Buffer { get; set; }
13+
}
14+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Reflection;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.AspNetCore.Hosting.Server.Features;
11+
using Microsoft.AspNetCore.Http;
12+
using Microsoft.Extensions.DependencyInjection;
13+
14+
namespace Azure.Template.Perf
15+
{
16+
public class InProcTestServer : IStartup, IDisposable
17+
{
18+
// Stream.CopyToAsync default buffer
19+
private const int BufferSize = 81920;
20+
21+
private readonly RequestDelegate _app;
22+
private readonly IWebHost _host;
23+
24+
public Uri Address => new Uri(_host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
25+
26+
public InProcTestServer(RequestDelegate app)
27+
{
28+
_app = app;
29+
_host = new WebHostBuilder()
30+
.UseKestrel(options => options.Listen(new IPEndPoint(IPAddress.Loopback, 0)))
31+
.ConfigureServices(services => { services.AddSingleton<IStartup>(this); })
32+
.UseSetting(WebHostDefaults.ApplicationKey, typeof(InProcTestServer).GetTypeInfo().Assembly.FullName)
33+
.Build();
34+
35+
_host.Start();
36+
}
37+
38+
IServiceProvider IStartup.ConfigureServices(IServiceCollection services)
39+
{
40+
return services.BuildServiceProvider();
41+
}
42+
43+
void IStartup.Configure(IApplicationBuilder app)
44+
{
45+
app.Run(_app);
46+
}
47+
48+
public void Dispose()
49+
{
50+
_host?.Dispose();
51+
}
52+
53+
public static InProcTestServer CreateStaticResponse(long size)
54+
{
55+
var buffer = new byte[BufferSize];
56+
return new InProcTestServer(async context =>
57+
{
58+
long left = size;
59+
while (left > 0)
60+
{
61+
var amountToWrite = (int)Math.Min(left, buffer.Length);
62+
await context.Response.Body.WriteAsync(buffer, 0, amountToWrite);
63+
left -= amountToWrite;
64+
}
65+
});
66+
}
67+
}
68+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Reflection;
5+
using Azure.Test.Perf;
6+
7+
await PerfProgram.Main(Assembly.GetExecutingAssembly(), args);

0 commit comments

Comments
 (0)