-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add project for running benchmarks. Adapted from mus65@65bfd97. See #3044. Co-Authored-By: mus65 <1933789+mus65@users.noreply.github.com>
- Loading branch information
1 parent
46c1bc9
commit 3405724
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
[Bb]in/ | ||
.vs/ | ||
.idea* | ||
BenchmarkDotNet.Artifacts*/ | ||
coverage | ||
coverage.* | ||
node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#! /usr/bin/env pwsh | ||
|
||
#Requires -PSEdition Core | ||
#Requires -Version 7 | ||
|
||
param( | ||
[Parameter(Mandatory = $false)][string] $Framework = "net8.0", | ||
[Parameter(Mandatory = $false)][string] $Job = "" | ||
) | ||
|
||
$ErrorActionPreference = "Stop" | ||
$ProgressPreference = "SilentlyContinue" | ||
|
||
$benchmarks = (Join-Path $PSScriptRoot "perf" "Swashbuckle.AspNetCore.Benchmarks" "Swashbuckle.AspNetCore.Benchmarks.csproj") | ||
|
||
$additionalArgs = @() | ||
|
||
if (-Not [string]::IsNullOrEmpty($Job)) { | ||
$additionalArgs += "--job" | ||
$additionalArgs += $Job | ||
} | ||
|
||
if (-Not [string]::IsNullOrEmpty(${env:GITHUB_SHA})) { | ||
$additionalArgs += "--exporters" | ||
$additionalArgs += "json" | ||
} | ||
|
||
dotnet run --project $benchmarks --configuration "Release" --framework $Framework -- $additionalArgs --% --filter * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
var switcher = new BenchmarkSwitcher(typeof(Program).Assembly); | ||
switcher.Run(args); |
16 changes: 16 additions & 0 deletions
16
perf/Swashbuckle.AspNetCore.Benchmarks/Swashbuckle.AspNetCore.Benchmarks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.Annotations\Swashbuckle.AspNetCore.Annotations.csproj" /> | ||
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.SwaggerGen\Swashbuckle.AspNetCore.SwaggerGen.csproj" /> | ||
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.Swagger\Swashbuckle.AspNetCore.Swagger.csproj" /> | ||
<ProjectReference Include="..\..\test\Swashbuckle.AspNetCore.SwaggerGen.Test\Swashbuckle.AspNetCore.SwaggerGen.Test.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" /> | ||
</ItemGroup> | ||
</Project> |
155 changes: 155 additions & 0 deletions
155
perf/Swashbuckle.AspNetCore.Benchmarks/XmlCommentsBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Xml; | ||
using System.Xml.XPath; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Swashbuckle.AspNetCore.SwaggerGen.Test; | ||
using Swashbuckle.AspNetCore.TestSupport; | ||
|
||
namespace Swashbuckle.AspNetCore.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
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", string.Empty); | ||
navigator.MoveToChild("members", string.Empty); | ||
|
||
for (int i = 0; i < AddMemberCount; i++) | ||
{ | ||
navigator.PrependChild(@$"<member name=""benchmark_{i}""></member>"); | ||
} | ||
|
||
using var xmlStream = new MemoryStream(); | ||
xmlDocument.Save(xmlStream); | ||
xmlStream.Seek(0, SeekOrigin.Begin); | ||
var xPathDocument = new XPathDocument(xmlStream); | ||
|
||
// Document | ||
_document = new OpenApiDocument(); | ||
_documentFilterContext = new DocumentFilterContext( | ||
[ | ||
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() | ||
{ | ||
Schema = new() | ||
{ | ||
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() | ||
{ | ||
Schema = new() | ||
{ | ||
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); | ||
} |