File tree Expand file tree Collapse file tree 8 files changed +42
-40
lines changed
WebApiClientCore.Benchmarks Expand file tree Collapse file tree 8 files changed +42
-40
lines changed Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff 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 number Diff line number Diff line change 11using BenchmarkDotNet . Attributes ;
22using Microsoft . Extensions . DependencyInjection ;
3- using System . Net . Http ;
4- using System . Net . Http . Json ;
53using System . Threading . Tasks ;
64
75namespace 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11using BenchmarkDotNet . Attributes ;
22using Microsoft . Extensions . DependencyInjection ;
3- using System . Net . Http ;
4- using System . Net . Http . Json ;
53using System . Threading . Tasks ;
64
75namespace 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 {
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff line change 44namespace 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
You can’t perform that action at this time.
0 commit comments