-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathBenchmark.cs
99 lines (89 loc) · 3.86 KB
/
Benchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Refit;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WebApiClientCore.Benchmarks.Requests
{
[InProcess]
[MemoryDiagnoser]
public abstract class Benchmark
{
protected IServiceProvider ServiceProvider { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
var services = new ServiceCollection();
services
.AddWebApiClient()
.ConfigureHttpApi(o =>
{
o.UseLogging = false;
o.UseParameterPropertyValidate = false;
o.UseReturnValuePropertyValidate = false;
});
services
.AddHttpApi<IWebApiClientCoreJsonApi>()
.AddHttpMessageHandler(() => new JsonResponseHandler())
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/"));
services
.AddHttpApi<IWebApiClientCoreXmlApi>()
.AddHttpMessageHandler(() => new XmlResponseHandler())
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/"));
services
.AddRefitClient<IRefitJsonApi>(new RefitSettings
{
ContentSerializer = new SystemTextJsonContentSerializer()
})
.AddHttpMessageHandler(() => new JsonResponseHandler())
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/"));
services
.AddRefitClient<IRefitXmlApi>(new RefitSettings
{
ContentSerializer = new XmlContentSerializer()
})
.AddHttpMessageHandler(() => new XmlResponseHandler())
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/"));
this.ServiceProvider = services.BuildServiceProvider();
// 服务显式加载预热
this.ServiceProvider.GetService<IWebApiClientCoreJsonApi>();
this.ServiceProvider.GetService<IWebApiClientCoreXmlApi>();
this.ServiceProvider.GetService<IRefitJsonApi>();
this.ServiceProvider.GetService<IRefitXmlApi>();
}
private class JsonResponseHandler : DelegatingHandler
{
private static readonly MediaTypeHeaderValue applicationJson = MediaTypeHeaderValue.Parse("application/json");
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var content = new StreamContent(new MemoryStream(User.Utf8Json, writable: false), User.Utf8Json.Length);
content.Headers.ContentType = applicationJson;
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = content
};
return Task.FromResult(response);
}
}
private class XmlResponseHandler : DelegatingHandler
{
private static readonly MediaTypeHeaderValue applicationXml = MediaTypeHeaderValue.Parse("application/xml");
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var content = new StringContent(User.XmlString, Encoding.UTF8);
content.Headers.ContentType = applicationXml;
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = content
};
return Task.FromResult(response);
}
}
}
}