Skip to content

Commit

Permalink
benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mus65 committed Aug 26, 2024
1 parent 45a97e9 commit 65bfd97
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<ItemGroup>
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="4.2.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageVersion Include="IdentityServer4" Version="3.1.4" />
Expand Down
7 changes: 7 additions & 0 deletions Swashbuckle.AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinimalAppWithHostedService
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcWithNullable", "test\WebSites\MvcWithNullable\MvcWithNullable.csproj", "{F88B6070-BE3C-45F9-978C-2ECBA9518C24}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -271,6 +273,10 @@ Global
{F88B6070-BE3C-45F9-978C-2ECBA9518C24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F88B6070-BE3C-45F9-978C-2ECBA9518C24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F88B6070-BE3C-45F9-978C-2ECBA9518C24}.Release|Any CPU.Build.0 = Release|Any CPU
{F9072F19-9238-42F0-8853-46C1545590B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9072F19-9238-42F0-8853-46C1545590B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9072F19-9238-42F0-8853-46C1545590B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9072F19-9238-42F0-8853-46C1545590B9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -316,6 +322,7 @@ Global
{07BB09CF-6C6F-4D00-A459-93586345C921} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
{D06A88E8-6F42-4F40-943A-E266C0AE6EC9} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
{F88B6070-BE3C-45F9-978C-2ECBA9518C24} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
{F9072F19-9238-42F0-8853-46C1545590B9} = {0ADCB223-F375-45AB-8BC4-834EC9C69554}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {36FC6A67-247D-4149-8EDD-79FFD1A75F51}
Expand Down
154 changes: 154 additions & 0 deletions test/Swashbuckle.AspNetCore.SwaggerGen.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System.Reflection;
using System.Xml;
using System.Xml.XPath;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen.Test;
using Swashbuckle.AspNetCore.TestSupport;

namespace Swashbuckle.AspNetCore.SwaggerGen.Benchmark;

[MemoryDiagnoser(true)]
public class XmlCommentsBenchmark
{
private XmlCommentsDocumentFilter _documentFilter;
private OpenApiDocument _document;
private DocumentFilterContext _documentFilterContext;

private XmlCommentsOperationFilter _operationFilter;
private OpenApiOperation _operation;
private OperationFilterContext _operationFilterContext;

private XmlCommentsParameterFilter _parameterFilter;
private OpenApiParameter _parameter;
private ParameterFilterContext _parameterFilterContext;

private XmlCommentsRequestBodyFilter _requestBodyFilter;
private OpenApiRequestBody _requestBody;
private RequestBodyFilterContext _requestBodyFilterContext;

private const int addMemberCount = 10_000;

[GlobalSetup]
public void Setup()
{
// Load XML
XmlDocument xmlDocument;
using (var xmlComments = File.OpenText($"{typeof(FakeControllerWithXmlComments).Assembly.GetName().Name}.xml"))
{
xmlDocument = new XmlDocument();
xmlDocument.Load(xmlComments);
}

// Append dummy members to XML document
XPathNavigator navigator = xmlDocument.CreateNavigator()!;
navigator.MoveToRoot();
navigator.MoveToChild("doc", "");
navigator.MoveToChild("members", "");

for (int i = 0; i < addMemberCount; i++)
{
navigator.PrependChild($"<member name=\"benchmark_{i}\"></member>");
}

MemoryStream xmlStream = new();
xmlDocument.Save(xmlStream);
xmlStream.Seek(0, SeekOrigin.Begin);
XPathDocument xPathDocument = new(xmlStream);

// Document
_document = new OpenApiDocument();
_documentFilterContext = new DocumentFilterContext(
new[]
{
new ApiDescription
{
ActionDescriptor = new ControllerActionDescriptor
{
ControllerTypeInfo = typeof(FakeControllerWithXmlComments).GetTypeInfo(),
ControllerName = nameof(FakeControllerWithXmlComments)
}
},
new ApiDescription
{
ActionDescriptor = new ControllerActionDescriptor
{
ControllerTypeInfo = typeof(FakeControllerWithXmlComments).GetTypeInfo(),
ControllerName = nameof(FakeControllerWithXmlComments)
}
}
},
null,
null);

_documentFilter = new XmlCommentsDocumentFilter(xPathDocument);

// Operation
_operation = new OpenApiOperation();
var methodInfo = typeof(FakeConstructedControllerWithXmlComments)
.GetMethod(nameof(FakeConstructedControllerWithXmlComments.ActionWithSummaryAndResponseTags));
var apiDescription = ApiDescriptionFactory.Create(methodInfo: methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource");
_operationFilterContext = new OperationFilterContext(apiDescription, null, null, methodInfo);
_operationFilter = new XmlCommentsOperationFilter(xPathDocument);

// Parameter
_parameter = new OpenApiParameter { Schema = new OpenApiSchema { Type = "string", Description = "schema-level description" } };
var propertyInfo = typeof(XmlAnnotatedType).GetProperty(nameof(XmlAnnotatedType.StringProperty));
var apiParameterDescription = new ApiParameterDescription { };
_parameterFilterContext = new ParameterFilterContext(apiParameterDescription, null, null, propertyInfo: propertyInfo);
_parameterFilter = new XmlCommentsParameterFilter(xPathDocument);

// Request Body
_requestBody = new OpenApiRequestBody
{
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType { Schema = new OpenApiSchema { Type = "string" } }
}
};
var parameterInfo = typeof(FakeControllerWithXmlComments)
.GetMethod(nameof(FakeControllerWithXmlComments.ActionWithParamTags))!
.GetParameters()[0];
var bodyParameterDescription = new ApiParameterDescription
{
ParameterDescriptor = new ControllerParameterDescriptor { ParameterInfo = parameterInfo }
};
_requestBodyFilterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);
_requestBodyFilter = new XmlCommentsRequestBodyFilter(xPathDocument);
}

[Benchmark]
public void Document()
{
_documentFilter.Apply(_document, _documentFilterContext);
}

[Benchmark]
public void Operation()
{
_operationFilter.Apply(_operation, _operationFilterContext);
}

[Benchmark]
public void Parameter()
{
_parameterFilter.Apply(_parameter, _parameterFilterContext);
}

[Benchmark]
public void RequestBody()
{
_requestBodyFilter.Apply(_requestBody, _requestBodyFilterContext);
}
}

public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<XmlCommentsBenchmark>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="Microsoft.OpenApi" />
</ItemGroup>

<ItemGroup>
<ProjectReference
Include="..\Swashbuckle.AspNetCore.SwaggerGen.Test\Swashbuckle.AspNetCore.SwaggerGen.Test.csproj" />
</ItemGroup>

</Project>

0 comments on commit 65bfd97

Please sign in to comment.