From 12d42bb01821fc0a6e517f3730d207847c701840 Mon Sep 17 00:00:00 2001 From: Ezreal Date: Sun, 14 Dec 2025 12:26:45 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(benchmarks):=20=E6=B7=BB=E5=8A=A0=20Re?= =?UTF-8?q?stSharp=20=E5=9F=BA=E5=87=86=E6=B5=8B=E8=AF=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 RestSharp 作为新的 HTTP 客户端基准测试工具 在多个请求类型中实现 RestSharp 的基准测试方法 配置 RestSharp 客户端服务并添加相关包装类 --- .../Requests/Benchmark.cs | 43 +++++++++++++++++++ .../Requests/HttpGetBenchmark.cs | 10 +++++ .../Requests/HttpGetJsonBenchmark.cs | 12 +++++- .../Requests/HttpPostJsonBenchmark.cs | 15 ++++++- .../Requests/HttpPostXmlBenchmark.cs | 15 ++++++- .../Requests/HttpPutFormBenchmark.cs | 19 +++++++- .../WebApiClientCore.Benchmarks.csproj | 1 + 7 files changed, 110 insertions(+), 5 deletions(-) diff --git a/WebApiClientCore.Benchmarks/Requests/Benchmark.cs b/WebApiClientCore.Benchmarks/Requests/Benchmark.cs index 8c6bbb82..0165b62e 100644 --- a/WebApiClientCore.Benchmarks/Requests/Benchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/Benchmark.cs @@ -1,6 +1,7 @@ using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; using Refit; +using RestSharp; using System; using System.IO; using System.Net; @@ -57,6 +58,30 @@ public void GlobalSetup() .AddHttpMessageHandler(() => new XmlResponseHandler()) .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/")); + // 配置 RestSharp JSON 客户端 + services.AddHttpClient("RestSharpJsonClient") + .AddHttpMessageHandler(() => new JsonResponseHandler()) + .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/")); + + // 配置 RestSharp XML 客户端 + services.AddHttpClient("RestSharpXmlClient") + .AddHttpMessageHandler(() => new XmlResponseHandler()) + .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/")); + + // 注册 RestSharp JSON 客户端 + services.AddTransient(sp => + { + var httpClient = sp.GetRequiredService().CreateClient("RestSharpJsonClient"); + return new RestSharpJsonClient(httpClient); + }); + + // 注册 RestSharp XML 客户端 + services.AddTransient(sp => + { + var httpClient = sp.GetRequiredService().CreateClient("RestSharpXmlClient"); + return new RestSharpXmlClient(httpClient); + }); + this.ServiceProvider = services.BuildServiceProvider(); // 服务显式加载预热 @@ -64,6 +89,8 @@ public void GlobalSetup() this.ServiceProvider.GetService(); this.ServiceProvider.GetService(); this.ServiceProvider.GetService(); + this.ServiceProvider.GetService(); + this.ServiceProvider.GetService(); } private class JsonResponseHandler : DelegatingHandler @@ -95,5 +122,21 @@ protected override Task SendAsync(HttpRequestMessage reques return Task.FromResult(response); } } + + // RestSharp JSON客户端包装类 + public class RestSharpJsonClient : RestClient + { + public RestSharpJsonClient(HttpClient httpClient) : base(httpClient) + { + } + } + + // RestSharp XML客户端包装类 + public class RestSharpXmlClient : RestClient + { + public RestSharpXmlClient(HttpClient httpClient) : base(httpClient) + { + } + } } } diff --git a/WebApiClientCore.Benchmarks/Requests/HttpGetBenchmark.cs b/WebApiClientCore.Benchmarks/Requests/HttpGetBenchmark.cs index 79b2f4bf..56851e60 100644 --- a/WebApiClientCore.Benchmarks/Requests/HttpGetBenchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/HttpGetBenchmark.cs @@ -1,5 +1,6 @@ using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; +using RestSharp; using System.Threading.Tasks; namespace WebApiClientCore.Benchmarks.Requests @@ -21,5 +22,14 @@ public async Task Refit_GetAsync() var benchmarkApi = scope.ServiceProvider.GetRequiredService(); await benchmarkApi.GetAsync(id: "id001"); } + + [Benchmark] + public async Task RestSharp_GetAsync() + { + using var scope = this.ServiceProvider.CreateScope(); + var client = scope.ServiceProvider.GetRequiredService(); + var request = new RestRequest($"/benchmarks/id001"); + await client.ExecuteAsync(request); + } } } diff --git a/WebApiClientCore.Benchmarks/Requests/HttpGetJsonBenchmark.cs b/WebApiClientCore.Benchmarks/Requests/HttpGetJsonBenchmark.cs index 45ccebb7..eb461aef 100644 --- a/WebApiClientCore.Benchmarks/Requests/HttpGetJsonBenchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/HttpGetJsonBenchmark.cs @@ -1,5 +1,6 @@ -using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; +using RestSharp; using System.Threading.Tasks; namespace WebApiClientCore.Benchmarks.Requests @@ -21,5 +22,14 @@ public async Task Refit_GetJsonAsync() var benchmarkApi = scope.ServiceProvider.GetRequiredService(); return await benchmarkApi.GetJsonAsync(id: "id001"); } + + [Benchmark] + public async Task RestSharp_GetJsonAsync() + { + using var scope = this.ServiceProvider.CreateScope(); + var client = scope.ServiceProvider.GetRequiredService(); + var request = new RestRequest($"/benchmarks/id001"); + return await client.GetAsync(request); + } } } diff --git a/WebApiClientCore.Benchmarks/Requests/HttpPostJsonBenchmark.cs b/WebApiClientCore.Benchmarks/Requests/HttpPostJsonBenchmark.cs index 9ccaa97d..d8c93f9a 100644 --- a/WebApiClientCore.Benchmarks/Requests/HttpPostJsonBenchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/HttpPostJsonBenchmark.cs @@ -1,5 +1,6 @@ -using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; +using RestSharp; using System.Threading.Tasks; namespace WebApiClientCore.Benchmarks.Requests @@ -19,8 +20,18 @@ public async Task WebApiClientCore_PostJsonAsync() public async Task Refit_PostJsonAsync() { using var scope = this.ServiceProvider.CreateScope(); - var benchmarkApi = scope.ServiceProvider.GetRequiredService(); + var benchmarkApi = scope.ServiceProvider.GetRequiredService(); return await benchmarkApi.PostJsonAsync(User.Instance); } + + [Benchmark] + public async Task RestSharp_PostJsonAsync() + { + using var scope = this.ServiceProvider.CreateScope(); + var client = scope.ServiceProvider.GetRequiredService(); + var request = new RestRequest($"/benchmarks") + .AddJsonBody(User.Instance); + return await client.PostAsync(request); + } } } diff --git a/WebApiClientCore.Benchmarks/Requests/HttpPostXmlBenchmark.cs b/WebApiClientCore.Benchmarks/Requests/HttpPostXmlBenchmark.cs index f282158a..dbf7172c 100644 --- a/WebApiClientCore.Benchmarks/Requests/HttpPostXmlBenchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/HttpPostXmlBenchmark.cs @@ -1,6 +1,9 @@ -using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; +using RestSharp; +using System.Net.Http; using System.Threading.Tasks; +using System; namespace WebApiClientCore.Benchmarks.Requests { @@ -21,5 +24,15 @@ public async Task Refit_PostXmlAsync() var benchmarkApi = scope.ServiceProvider.GetRequiredService(); return await benchmarkApi.PostXmlAsync(User.Instance); } + + [Benchmark] + public async Task RestSharp_PostXmlAsync() + { + using var scope = this.ServiceProvider.CreateScope(); + var client = scope.ServiceProvider.GetRequiredService(); + var request = new RestRequest($"/benchmarks") + .AddXmlBody(User.Instance); + return await client.PostAsync(request); + } } } diff --git a/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs b/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs index 9672d6c1..dbdfdcc8 100644 --- a/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs @@ -1,5 +1,6 @@ -using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; +using RestSharp; using System.Threading.Tasks; namespace WebApiClientCore.Benchmarks.Requests @@ -21,5 +22,21 @@ public async Task Refit_PutFormAsync() var benchmarkApi = scope.ServiceProvider.GetRequiredService(); return await benchmarkApi.PutFormAsync(id: "id001", User.Instance); } + + [Benchmark] + public async Task RestSharp_PutFormAsync() + { + using var scope = this.ServiceProvider.CreateScope(); + var client = scope.ServiceProvider.GetRequiredService(); + var request = new RestRequest($"/benchmarks/id001") + .AddParameter("id", User.Instance.Id) + .AddParameter("name", User.Instance.Name) + .AddParameter("bio", User.Instance.Bio) + .AddParameter("followers", User.Instance.Followers) + .AddParameter("following", User.Instance.Following) + .AddParameter("url", User.Instance.Url) + .AddHeader("Content-Type", "application/x-www-form-urlencoded"); + return await client.PutAsync(request); + } } } diff --git a/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj b/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj index 8946fc7f..2f508dd5 100644 --- a/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj +++ b/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj @@ -16,6 +16,7 @@ + From 10405e939441d95786ae4046cc2c1ad3923851ff Mon Sep 17 00:00:00 2001 From: Ezreal Date: Tue, 16 Dec 2025 20:39:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?build:=20=E6=9B=B4=E6=96=B0=20RestSharp=20?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E7=89=88=E6=9C=AC=E8=87=B3=20112.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebApiClientCore.Benchmarks.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj b/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj index 2f508dd5..e0d2f07e 100644 --- a/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj +++ b/WebApiClientCore.Benchmarks/WebApiClientCore.Benchmarks.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1;net5.0;net8.0;net10.0 @@ -16,7 +16,7 @@ - + From ad6935aeb488055c16ce847bf6b91e221d7b9af8 Mon Sep 17 00:00:00 2001 From: Ezreal Date: Tue, 16 Dec 2025 21:03:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84Content-Type=E8=AF=B7=E6=B1=82=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs b/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs index dbdfdcc8..73d14942 100644 --- a/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs @@ -1,4 +1,4 @@ -using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes; using Microsoft.Extensions.DependencyInjection; using RestSharp; using System.Threading.Tasks; @@ -34,8 +34,7 @@ public async Task RestSharp_PutFormAsync() .AddParameter("bio", User.Instance.Bio) .AddParameter("followers", User.Instance.Followers) .AddParameter("following", User.Instance.Following) - .AddParameter("url", User.Instance.Url) - .AddHeader("Content-Type", "application/x-www-form-urlencoded"); + .AddParameter("url", User.Instance.Url); return await client.PutAsync(request); } }