File tree 8 files changed +42
-40
lines changed
WebApiClientCore.Benchmarks
8 files changed +42
-40
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,9 @@ namespace WebApiClientCore.Benchmarks
7
7
class Program
8
8
{
9
9
static void Main ( string [ ] args )
10
- {
10
+ {
11
11
BenchmarkRunner . Run < HttpGetBenchmark > ( ) ;
12
+ BenchmarkRunner . Run < HttpGetJsonBenchmark > ( ) ;
12
13
BenchmarkRunner . Run < HttpPostJsonBenchmark > ( ) ;
13
14
BenchmarkRunner . Run < HttpPutFormBenchmark > ( ) ;
14
15
Console . ReadLine ( ) ;
Original file line number Diff line number Diff line change @@ -21,11 +21,6 @@ public abstract class Benchmark
21
21
public void GlobalSetup ( )
22
22
{
23
23
var services = new ServiceCollection ( ) ;
24
-
25
- services
26
- . AddHttpClient ( typeof ( HttpClient ) . FullName )
27
- . AddHttpMessageHandler ( ( ) => new UserResponseHandler ( ) ) ;
28
-
29
24
services
30
25
. AddHttpApi < IWebApiClientCoreApi > ( o =>
31
26
{
Original file line number Diff line number Diff line change 1
1
using BenchmarkDotNet . Attributes ;
2
2
using Microsoft . Extensions . DependencyInjection ;
3
- using System . Net . Http ;
4
- using System . Net . Http . Json ;
5
3
using System . Threading . Tasks ;
6
4
7
5
namespace WebApiClientCore . Benchmarks . Requests
8
6
{
9
7
public class HttpGetBenchmark : Benchmark
10
8
{
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
-
22
9
[ Benchmark ( Baseline = true ) ]
23
- public async Task < User > WebApiClientCore_GetAsync ( )
10
+ public async Task WebApiClientCore_GetAsync ( )
24
11
{
25
12
using var scope = this . ServiceProvider . CreateScope ( ) ;
26
13
var benchmarkApi = scope . ServiceProvider . GetRequiredService < IWebApiClientCoreApi > ( ) ;
27
- return await benchmarkApi . GetJsonAsync ( id : "id " ) ;
14
+ await benchmarkApi . GetAsync ( id : "id001 " ) ;
28
15
}
29
16
30
17
[ Benchmark ]
31
- public async Task < User > Refit_GetAsync ( )
18
+ public async Task Refit_GetAsync ( )
32
19
{
33
20
using var scope = this . ServiceProvider . CreateScope ( ) ;
34
21
var benchmarkApi = scope . ServiceProvider . GetRequiredService < IRefitApi > ( ) ;
35
- return await benchmarkApi . GetAsync ( id : "id " ) ;
22
+ await benchmarkApi . GetAsync ( id : "id001 " ) ;
36
23
}
37
24
}
38
25
}
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 1
1
using BenchmarkDotNet . Attributes ;
2
2
using Microsoft . Extensions . DependencyInjection ;
3
- using System . Net . Http ;
4
- using System . Net . Http . Json ;
5
3
using System . Threading . Tasks ;
6
4
7
5
namespace WebApiClientCore . Benchmarks . Requests
8
6
{
9
- [ MemoryDiagnoser ]
10
7
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
+ {
22
9
[ Benchmark ( Baseline = true ) ]
23
10
public async Task < User > WebApiClientCore_PostJsonAsync ( )
24
11
{
Original file line number Diff line number Diff line change @@ -11,15 +11,15 @@ public async Task<User> WebApiClientCore_PutFormAsync()
11
11
{
12
12
using var scope = this . ServiceProvider . CreateScope ( ) ;
13
13
var benchmarkApi = scope . ServiceProvider . GetRequiredService < IWebApiClientCoreApi > ( ) ;
14
- return await benchmarkApi . PutFormAsync ( "id001" , User . Instance ) ;
14
+ return await benchmarkApi . PutFormAsync ( id : "id001" , User . Instance ) ;
15
15
}
16
16
17
17
[ Benchmark ]
18
18
public async Task < User > Refit_PutFormAsync ( )
19
19
{
20
20
using var scope = this . ServiceProvider . CreateScope ( ) ;
21
21
var benchmarkApi = scope . ServiceProvider . GetRequiredService < IRefitApi > ( ) ;
22
- return await benchmarkApi . PutFormAsync ( "id001" , User . Instance ) ;
22
+ return await benchmarkApi . PutFormAsync ( id : "id001" , User . Instance ) ;
23
23
}
24
24
}
25
25
}
Original file line number Diff line number Diff line change @@ -6,7 +6,10 @@ namespace WebApiClientCore.Benchmarks.Requests
6
6
public interface IRefitApi
7
7
{
8
8
[ 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 ) ;
10
13
11
14
[ Post ( "/benchmarks" ) ]
12
15
Task < User > PostJsonAsync ( [ Body ( BodySerializationMethod . Serialized ) ] User model ) ;
Original file line number Diff line number Diff line change 4
4
namespace WebApiClientCore . Benchmarks . Requests
5
5
{
6
6
[ JsonReturn ]
7
+ [ XmlReturn ( Enable = false ) ]
7
8
public interface IWebApiClientCoreApi
8
9
{
10
+ [ HttpGet ( "/benchmarks/{id}" ) ]
11
+ Task GetAsync ( string id ) ;
12
+
9
13
[ HttpGet ( "/benchmarks/{id}" ) ]
10
14
Task < User > GetJsonAsync ( string id ) ;
11
15
You can’t perform that action at this time.
0 commit comments