Skip to content

Commit 359aee3

Browse files
committed
增加GET
1 parent aaefc67 commit 359aee3

8 files changed

+42
-40
lines changed

WebApiClientCore.Benchmarks/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ namespace WebApiClientCore.Benchmarks
77
class Program
88
{
99
static void Main(string[] args)
10-
{
10+
{
1111
BenchmarkRunner.Run<HttpGetBenchmark>();
12+
BenchmarkRunner.Run<HttpGetJsonBenchmark>();
1213
BenchmarkRunner.Run<HttpPostJsonBenchmark>();
1314
BenchmarkRunner.Run<HttpPutFormBenchmark>();
1415
Console.ReadLine();

WebApiClientCore.Benchmarks/Requests/Benchmark.cs

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ public abstract class Benchmark
2121
public void GlobalSetup()
2222
{
2323
var services = new ServiceCollection();
24-
25-
services
26-
.AddHttpClient(typeof(HttpClient).FullName)
27-
.AddHttpMessageHandler(() => new UserResponseHandler());
28-
2924
services
3025
.AddHttpApi<IWebApiClientCoreApi>(o =>
3126
{
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,25 @@
11
using BenchmarkDotNet.Attributes;
22
using Microsoft.Extensions.DependencyInjection;
3-
using System.Net.Http;
4-
using System.Net.Http.Json;
53
using System.Threading.Tasks;
64

75
namespace WebApiClientCore.Benchmarks.Requests
86
{
97
public class HttpGetBenchmark : Benchmark
108
{
11-
[Benchmark]
12-
public async Task<User> HttpClient_GetAsync()
13-
{
14-
using var scope = this.ServiceProvider.CreateScope();
15-
var httpClient = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(typeof(HttpClient).FullName);
16-
17-
var id = "id";
18-
var requestUri = $"http://webapiclient.com/{id}";
19-
return await httpClient.GetFromJsonAsync<User>(requestUri);
20-
}
21-
229
[Benchmark(Baseline = true)]
23-
public async Task<User> WebApiClientCore_GetAsync()
10+
public async Task WebApiClientCore_GetAsync()
2411
{
2512
using var scope = this.ServiceProvider.CreateScope();
2613
var benchmarkApi = scope.ServiceProvider.GetRequiredService<IWebApiClientCoreApi>();
27-
return await benchmarkApi.GetJsonAsync(id: "id");
14+
await benchmarkApi.GetAsync(id: "id001");
2815
}
2916

3017
[Benchmark]
31-
public async Task<User> Refit_GetAsync()
18+
public async Task Refit_GetAsync()
3219
{
3320
using var scope = this.ServiceProvider.CreateScope();
3421
var benchmarkApi = scope.ServiceProvider.GetRequiredService<IRefitApi>();
35-
return await benchmarkApi.GetAsync(id: "id");
22+
await benchmarkApi.GetAsync(id: "id001");
3623
}
3724
}
3825
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System.Threading.Tasks;
4+
5+
namespace WebApiClientCore.Benchmarks.Requests
6+
{
7+
public class HttpGetJsonBenchmark : Benchmark
8+
{
9+
[Benchmark(Baseline = true)]
10+
public async Task<User> WebApiClientCore_GetJsonAsync()
11+
{
12+
using var scope = this.ServiceProvider.CreateScope();
13+
var benchmarkApi = scope.ServiceProvider.GetRequiredService<IWebApiClientCoreApi>();
14+
return await benchmarkApi.GetJsonAsync(id: "id001");
15+
}
16+
17+
[Benchmark]
18+
public async Task<User> Refit_GetJsonAsync()
19+
{
20+
using var scope = this.ServiceProvider.CreateScope();
21+
var benchmarkApi = scope.ServiceProvider.GetRequiredService<IRefitApi>();
22+
return await benchmarkApi.GetJsonAsync(id: "id001");
23+
}
24+
}
25+
}

WebApiClientCore.Benchmarks/Requests/HttpPostJsonBenchmark.cs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
using BenchmarkDotNet.Attributes;
22
using Microsoft.Extensions.DependencyInjection;
3-
using System.Net.Http;
4-
using System.Net.Http.Json;
53
using System.Threading.Tasks;
64

75
namespace WebApiClientCore.Benchmarks.Requests
86
{
9-
[MemoryDiagnoser]
107
public class HttpPostJsonBenchmark : Benchmark
11-
{
12-
[Benchmark]
13-
public async Task<User> HttpClient_PostJsonAsync()
14-
{
15-
using var scope = this.ServiceProvider.CreateScope();
16-
var httpClient = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(typeof(HttpClient).FullName);
17-
18-
var response = await httpClient.PostAsJsonAsync($"http://webapiclient.com/", User.Instance);
19-
return await response.Content.ReadFromJsonAsync<User>();
20-
}
21-
8+
{
229
[Benchmark(Baseline = true)]
2310
public async Task<User> WebApiClientCore_PostJsonAsync()
2411
{

WebApiClientCore.Benchmarks/Requests/HttpPutFormBenchmark.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public async Task<User> WebApiClientCore_PutFormAsync()
1111
{
1212
using var scope = this.ServiceProvider.CreateScope();
1313
var benchmarkApi = scope.ServiceProvider.GetRequiredService<IWebApiClientCoreApi>();
14-
return await benchmarkApi.PutFormAsync("id001", User.Instance);
14+
return await benchmarkApi.PutFormAsync(id: "id001", User.Instance);
1515
}
1616

1717
[Benchmark]
1818
public async Task<User> Refit_PutFormAsync()
1919
{
2020
using var scope = this.ServiceProvider.CreateScope();
2121
var benchmarkApi = scope.ServiceProvider.GetRequiredService<IRefitApi>();
22-
return await benchmarkApi.PutFormAsync("id001", User.Instance);
22+
return await benchmarkApi.PutFormAsync(id: "id001", User.Instance);
2323
}
2424
}
2525
}

WebApiClientCore.Benchmarks/Requests/IRefitApi.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace WebApiClientCore.Benchmarks.Requests
66
public interface IRefitApi
77
{
88
[Get("/benchmarks/{id}")]
9-
Task<User> GetAsync(string id);
9+
Task GetAsync(string id);
10+
11+
[Get("/benchmarks/{id}")]
12+
Task<User> GetJsonAsync(string id);
1013

1114
[Post("/benchmarks")]
1215
Task<User> PostJsonAsync([Body(BodySerializationMethod.Serialized)] User model);

WebApiClientCore.Benchmarks/Requests/IWebApiClientCoreApi.cs

+4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
namespace WebApiClientCore.Benchmarks.Requests
55
{
66
[JsonReturn]
7+
[XmlReturn(Enable = false)]
78
public interface IWebApiClientCoreApi
89
{
10+
[HttpGet("/benchmarks/{id}")]
11+
Task GetAsync(string id);
12+
913
[HttpGet("/benchmarks/{id}")]
1014
Task<User> GetJsonAsync(string id);
1115

0 commit comments

Comments
 (0)