From 4812929127e01d54b7e23de38fb3033657e88a83 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Tue, 19 Jun 2018 12:30:27 -0700 Subject: [PATCH] Added benchmarks for syntax tree generation --- .../SyntaxTreeGenerationBenckmark.cs | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 benchmarks/Microsoft.AspNetCore.Razor.Performance/SyntaxTreeGenerationBenckmark.cs diff --git a/benchmarks/Microsoft.AspNetCore.Razor.Performance/SyntaxTreeGenerationBenckmark.cs b/benchmarks/Microsoft.AspNetCore.Razor.Performance/SyntaxTreeGenerationBenckmark.cs new file mode 100644 index 0000000000..d1ec9a0a55 --- /dev/null +++ b/benchmarks/Microsoft.AspNetCore.Razor.Performance/SyntaxTreeGenerationBenckmark.cs @@ -0,0 +1,77 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Microsoft.AspNetCore.Mvc.Razor.Extensions; +using Microsoft.AspNetCore.Razor.Language; + +namespace Microsoft.AspNetCore.Razor.Performance +{ + public class SyntaxTreeGenerationBenchmark + { + public SyntaxTreeGenerationBenchmark() + { + var current = new DirectoryInfo(AppContext.BaseDirectory); + while (current != null && !File.Exists(Path.Combine(current.FullName, "MSN.cshtml"))) + { + current = current.Parent; + } + + var root = current; + var fileSystem = RazorProjectFileSystem.Create(root.FullName); + + ProjectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, b => RazorExtensions.Register(b)); ; + + var projectItem = fileSystem.GetItem(Path.Combine(root.FullName, "MSN.cshtml")); + MSN = RazorSourceDocument.ReadFrom(projectItem); + + var directiveFeature = ProjectEngine.EngineFeatures.OfType().FirstOrDefault(); + Directives = directiveFeature?.Directives.ToArray() ?? Array.Empty(); + } + + public RazorProjectEngine ProjectEngine { get; } + + public RazorSourceDocument MSN { get; } + + public DirectiveDescriptor[] Directives { get; } + + [Benchmark(Description = "Razor Design Time Syntax Tree Generation of MSN.com")] + public void SyntaxTreeGeneration_DesignTime_LargeStaticFile() + { + var options = RazorParserOptions.CreateDesignTime(o => + { + foreach (var directive in Directives) + { + o.Directives.Add(directive); + } + }); + var syntaxTree = RazorSyntaxTree.Parse(MSN, options); + + if (syntaxTree.Diagnostics.Count != 0) + { + throw new Exception("Error!" + Environment.NewLine + string.Join(Environment.NewLine, syntaxTree.Diagnostics)); + } + } + + [Benchmark(Description = "Razor Runtime Syntax Tree Generation of MSN.com")] + public void SyntaxTreeGeneration_Runtime_LargeStaticFile() + { + var options = RazorParserOptions.Create(o => + { + foreach (var directive in Directives) + { + o.Directives.Add(directive); + } + }); + var syntaxTree = RazorSyntaxTree.Parse(MSN, options); + + if (syntaxTree.Diagnostics.Count != 0) + { + throw new Exception("Error!" + Environment.NewLine + string.Join(Environment.NewLine, syntaxTree.Diagnostics)); + } + } + } +}