Skip to content

Commit 65bfd97

Browse files
committed
benchmark
1 parent 45a97e9 commit 65bfd97

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22
<ItemGroup>
33
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="4.2.2" />
4+
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
45
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
56
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
67
<PackageVersion Include="IdentityServer4" Version="3.1.4" />

Swashbuckle.AspNetCore.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinimalAppWithHostedService
117117
EndProject
118118
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcWithNullable", "test\WebSites\MvcWithNullable\MvcWithNullable.csproj", "{F88B6070-BE3C-45F9-978C-2ECBA9518C24}"
119119
EndProject
120+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Swashbuckle.AspNetCore.SwaggerGen.Benchmarks", "test\Swashbuckle.AspNetCore.SwaggerGen.Benchmarks\Swashbuckle.AspNetCore.SwaggerGen.Benchmarks.csproj", "{F9072F19-9238-42F0-8853-46C1545590B9}"
121+
EndProject
120122
Global
121123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
122124
Debug|Any CPU = Debug|Any CPU
@@ -271,6 +273,10 @@ Global
271273
{F88B6070-BE3C-45F9-978C-2ECBA9518C24}.Debug|Any CPU.Build.0 = Debug|Any CPU
272274
{F88B6070-BE3C-45F9-978C-2ECBA9518C24}.Release|Any CPU.ActiveCfg = Release|Any CPU
273275
{F88B6070-BE3C-45F9-978C-2ECBA9518C24}.Release|Any CPU.Build.0 = Release|Any CPU
276+
{F9072F19-9238-42F0-8853-46C1545590B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
277+
{F9072F19-9238-42F0-8853-46C1545590B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
278+
{F9072F19-9238-42F0-8853-46C1545590B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
279+
{F9072F19-9238-42F0-8853-46C1545590B9}.Release|Any CPU.Build.0 = Release|Any CPU
274280
EndGlobalSection
275281
GlobalSection(SolutionProperties) = preSolution
276282
HideSolutionNode = FALSE
@@ -316,6 +322,7 @@ Global
316322
{07BB09CF-6C6F-4D00-A459-93586345C921} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
317323
{D06A88E8-6F42-4F40-943A-E266C0AE6EC9} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
318324
{F88B6070-BE3C-45F9-978C-2ECBA9518C24} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
325+
{F9072F19-9238-42F0-8853-46C1545590B9} = {0ADCB223-F375-45AB-8BC4-834EC9C69554}
319326
EndGlobalSection
320327
GlobalSection(ExtensibilityGlobals) = postSolution
321328
SolutionGuid = {36FC6A67-247D-4149-8EDD-79FFD1A75F51}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using System.Reflection;
2+
using System.Xml;
3+
using System.Xml.XPath;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Running;
6+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
7+
using Microsoft.AspNetCore.Mvc.Controllers;
8+
using Microsoft.OpenApi.Models;
9+
using Swashbuckle.AspNetCore.SwaggerGen.Test;
10+
using Swashbuckle.AspNetCore.TestSupport;
11+
12+
namespace Swashbuckle.AspNetCore.SwaggerGen.Benchmark;
13+
14+
[MemoryDiagnoser(true)]
15+
public class XmlCommentsBenchmark
16+
{
17+
private XmlCommentsDocumentFilter _documentFilter;
18+
private OpenApiDocument _document;
19+
private DocumentFilterContext _documentFilterContext;
20+
21+
private XmlCommentsOperationFilter _operationFilter;
22+
private OpenApiOperation _operation;
23+
private OperationFilterContext _operationFilterContext;
24+
25+
private XmlCommentsParameterFilter _parameterFilter;
26+
private OpenApiParameter _parameter;
27+
private ParameterFilterContext _parameterFilterContext;
28+
29+
private XmlCommentsRequestBodyFilter _requestBodyFilter;
30+
private OpenApiRequestBody _requestBody;
31+
private RequestBodyFilterContext _requestBodyFilterContext;
32+
33+
private const int addMemberCount = 10_000;
34+
35+
[GlobalSetup]
36+
public void Setup()
37+
{
38+
// Load XML
39+
XmlDocument xmlDocument;
40+
using (var xmlComments = File.OpenText($"{typeof(FakeControllerWithXmlComments).Assembly.GetName().Name}.xml"))
41+
{
42+
xmlDocument = new XmlDocument();
43+
xmlDocument.Load(xmlComments);
44+
}
45+
46+
// Append dummy members to XML document
47+
XPathNavigator navigator = xmlDocument.CreateNavigator()!;
48+
navigator.MoveToRoot();
49+
navigator.MoveToChild("doc", "");
50+
navigator.MoveToChild("members", "");
51+
52+
for (int i = 0; i < addMemberCount; i++)
53+
{
54+
navigator.PrependChild($"<member name=\"benchmark_{i}\"></member>");
55+
}
56+
57+
MemoryStream xmlStream = new();
58+
xmlDocument.Save(xmlStream);
59+
xmlStream.Seek(0, SeekOrigin.Begin);
60+
XPathDocument xPathDocument = new(xmlStream);
61+
62+
// Document
63+
_document = new OpenApiDocument();
64+
_documentFilterContext = new DocumentFilterContext(
65+
new[]
66+
{
67+
new ApiDescription
68+
{
69+
ActionDescriptor = new ControllerActionDescriptor
70+
{
71+
ControllerTypeInfo = typeof(FakeControllerWithXmlComments).GetTypeInfo(),
72+
ControllerName = nameof(FakeControllerWithXmlComments)
73+
}
74+
},
75+
new ApiDescription
76+
{
77+
ActionDescriptor = new ControllerActionDescriptor
78+
{
79+
ControllerTypeInfo = typeof(FakeControllerWithXmlComments).GetTypeInfo(),
80+
ControllerName = nameof(FakeControllerWithXmlComments)
81+
}
82+
}
83+
},
84+
null,
85+
null);
86+
87+
_documentFilter = new XmlCommentsDocumentFilter(xPathDocument);
88+
89+
// Operation
90+
_operation = new OpenApiOperation();
91+
var methodInfo = typeof(FakeConstructedControllerWithXmlComments)
92+
.GetMethod(nameof(FakeConstructedControllerWithXmlComments.ActionWithSummaryAndResponseTags));
93+
var apiDescription = ApiDescriptionFactory.Create(methodInfo: methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource");
94+
_operationFilterContext = new OperationFilterContext(apiDescription, null, null, methodInfo);
95+
_operationFilter = new XmlCommentsOperationFilter(xPathDocument);
96+
97+
// Parameter
98+
_parameter = new OpenApiParameter { Schema = new OpenApiSchema { Type = "string", Description = "schema-level description" } };
99+
var propertyInfo = typeof(XmlAnnotatedType).GetProperty(nameof(XmlAnnotatedType.StringProperty));
100+
var apiParameterDescription = new ApiParameterDescription { };
101+
_parameterFilterContext = new ParameterFilterContext(apiParameterDescription, null, null, propertyInfo: propertyInfo);
102+
_parameterFilter = new XmlCommentsParameterFilter(xPathDocument);
103+
104+
// Request Body
105+
_requestBody = new OpenApiRequestBody
106+
{
107+
Content = new Dictionary<string, OpenApiMediaType>
108+
{
109+
["application/json"] = new OpenApiMediaType { Schema = new OpenApiSchema { Type = "string" } }
110+
}
111+
};
112+
var parameterInfo = typeof(FakeControllerWithXmlComments)
113+
.GetMethod(nameof(FakeControllerWithXmlComments.ActionWithParamTags))!
114+
.GetParameters()[0];
115+
var bodyParameterDescription = new ApiParameterDescription
116+
{
117+
ParameterDescriptor = new ControllerParameterDescriptor { ParameterInfo = parameterInfo }
118+
};
119+
_requestBodyFilterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);
120+
_requestBodyFilter = new XmlCommentsRequestBodyFilter(xPathDocument);
121+
}
122+
123+
[Benchmark]
124+
public void Document()
125+
{
126+
_documentFilter.Apply(_document, _documentFilterContext);
127+
}
128+
129+
[Benchmark]
130+
public void Operation()
131+
{
132+
_operationFilter.Apply(_operation, _operationFilterContext);
133+
}
134+
135+
[Benchmark]
136+
public void Parameter()
137+
{
138+
_parameterFilter.Apply(_parameter, _parameterFilterContext);
139+
}
140+
141+
[Benchmark]
142+
public void RequestBody()
143+
{
144+
_requestBodyFilter.Apply(_requestBody, _requestBodyFilterContext);
145+
}
146+
}
147+
148+
public class Program
149+
{
150+
public static void Main(string[] args)
151+
{
152+
var summary = BenchmarkRunner.Run<XmlCommentsBenchmark>();
153+
}
154+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" />
11+
<PackageReference Include="Microsoft.OpenApi" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference
16+
Include="..\Swashbuckle.AspNetCore.SwaggerGen.Test\Swashbuckle.AspNetCore.SwaggerGen.Test.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)