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

Improve HttpClient resilience benchmarks #4100

Merged
merged 4 commits into from
Jun 26, 2023
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -9,11 +10,12 @@

namespace Microsoft.Extensions.Http.Resilience.Bench;

public class Benchmark
public class HedgingBenchmark
{
private static HttpRequestMessage Request => new(HttpMethod.Post, "https://bogus");
private static readonly Uri _uri = new("https://bogus");
private static HttpRequestMessage Request => new(HttpMethod.Post, _uri);

private System.Net.Http.HttpClient _client = null!;
private HttpClient _client = null!;

[GlobalSetup]
public void GlobalSetup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Compliance.Redaction;
using Microsoft.Extensions.Compliance.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Telemetry.Metering;

#pragma warning disable R9A033 // Replace uses of 'Enum.GetName' and 'Enum.ToString' with the '[EnumStrings]' code generator for improved performance
Expand All @@ -24,6 +28,10 @@ public enum HedgingClientType

internal static class HttpClientFactory
{
internal const string EmptyClient = "Empty";

internal const string StandardClient = "Standard";

private const string HedgingEndpoint1 = "http://localhost1";
private const string HedgingEndpoint2 = "http://localhost2";

Expand All @@ -34,14 +42,25 @@ public static ServiceProvider InitializeServiceProvider(HedgingClientType client
.RegisterMetering()
.AddSingleton<IRedactorProvider>(NullRedactorProvider.Instance)
.AddTransient<NoRemoteCallHandler>()
.AddHedging(clientType);
.AddHedging(clientType)
.AddHttpClient(StandardClient, client => client.Timeout = Timeout.InfiniteTimeSpan)
.AddStandardResilienceHandler()
.Services
.AddHttpClient(StandardClient)
.AddHttpMessageHandler<NoRemoteCallHandler>()
.Services
.AddHttpClient(EmptyClient, client => client.Timeout = Timeout.InfiniteTimeSpan)
.AddHttpMessageHandler<NoRemoteCallHandler>();

services.RemoveAll<ILoggerFactory>();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);

return services.BuildServiceProvider();
}

private static IServiceCollection AddHedging(this IServiceCollection services, HedgingClientType clientType)
{
var clientBuilder = services.AddHttpClient(clientType.ToString());
var clientBuilder = services.AddHttpClient(clientType.ToString(), client => client.Timeout = Timeout.InfiniteTimeSpan);
var hedgingBuilder = clientBuilder.AddStandardHedgingHandler().SelectPipelineByAuthority(SimpleClassifications.PublicData);
_ = clientBuilder.AddHttpMessageHandler<NoRemoteCallHandler>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Http.Resilience.Bench;

public class HttpResilienceBenchmark
{
private static readonly Uri _uri = new("https://bogus");

private HttpClient _client = null!;
private HttpClient _standardClient = null!;
private HttpClient _hedgingClient = null!;

private static HttpRequestMessage Request
{
get
{
var request = new HttpRequestMessage(HttpMethod.Post, _uri);
request.Options.Set(new HttpRequestOptionsKey<string>("dummy"), "dummy");
return request;
}
}

[GlobalSetup]
public void GlobalSetup()
{
var serviceProvider = HttpClientFactory.InitializeServiceProvider(HedgingClientType.Ordered);
var factory = serviceProvider.GetRequiredService<IHttpClientFactory>();
_client = factory.CreateClient(HttpClientFactory.EmptyClient);
_standardClient = factory.CreateClient(HttpClientFactory.StandardClient);
_hedgingClient = factory.CreateClient(nameof(HedgingClientType.Ordered));
}

[Benchmark(Baseline = true)]
public Task<HttpResponseMessage> DefaultClient()
{
return _client.SendAsync(Request, CancellationToken.None);
}

[Benchmark]
public Task<HttpResponseMessage> StandardResilienceHandler()
{
return _standardClient.SendAsync(Request, CancellationToken.None);
}

[Benchmark]
public Task<HttpResponseMessage> StandardHedgingHandler()
{
return _hedgingClient.SendAsync(Request, CancellationToken.None);
}
}