|
| 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 | +} |
0 commit comments