-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix benchmarks to produce correct documents #8072
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...ctSystem/ProjectSnapshotManagerBenchmarkBase.StaticProjectSnapshotProjectEngineFactory.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,28 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Mvc.Razor.Extensions; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.CodeAnalysis.Razor; | ||
using Microsoft.CodeAnalysis.Razor.ProjectSystem; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Microbenchmarks; | ||
|
||
public abstract partial class ProjectSnapshotManagerBenchmarkBase | ||
{ | ||
private class StaticProjectSnapshotProjectEngineFactory : ProjectSnapshotProjectEngineFactory | ||
{ | ||
public override IProjectEngineFactory FindFactory(ProjectSnapshot project) | ||
=> throw new NotImplementedException(); | ||
|
||
public override IProjectEngineFactory FindSerializableFactory(ProjectSnapshot project) | ||
=> throw new NotImplementedException(); | ||
|
||
public override RazorProjectEngine Create( | ||
RazorConfiguration configuration, | ||
RazorProjectFileSystem fileSystem, | ||
Action<RazorProjectEngineBuilder> configure) | ||
=> RazorProjectEngine.Create(configuration, fileSystem, RazorExtensions.Register); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...robenchmarks/ProjectSystem/ProjectSnapshotManagerBenchmarkBase.StaticTagHelperResolver.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,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Telemetry; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Razor; | ||
using Microsoft.CodeAnalysis.Razor.ProjectSystem; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Microbenchmarks; | ||
|
||
public abstract partial class ProjectSnapshotManagerBenchmarkBase | ||
{ | ||
private class StaticTagHelperResolver : TagHelperResolver | ||
{ | ||
private readonly IReadOnlyList<TagHelperDescriptor> _tagHelpers; | ||
|
||
public StaticTagHelperResolver(IReadOnlyList<TagHelperDescriptor> tagHelpers, ITelemetryReporter telemetryReporter) | ||
: base(telemetryReporter) | ||
{ | ||
_tagHelpers = tagHelpers; | ||
} | ||
|
||
public override Task<TagHelperResolutionResult> GetTagHelpersAsync( | ||
Project project, | ||
ProjectSnapshot projectSnapshot, | ||
CancellationToken cancellationToken = default) | ||
=> Task.FromResult(new TagHelperResolutionResult(_tagHelpers, Array.Empty<RazorDiagnostic>())); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...or.Microbenchmarks/ProjectSystem/ProjectSnapshotManagerBenchmarkBase.TestErrorReporter.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,27 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Razor; | ||
using Microsoft.CodeAnalysis.Razor.ProjectSystem; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Microbenchmarks; | ||
|
||
public abstract partial class ProjectSnapshotManagerBenchmarkBase | ||
{ | ||
private class TestErrorReporter : ErrorReporter | ||
{ | ||
public override void ReportError(Exception exception) | ||
{ | ||
} | ||
|
||
public override void ReportError(Exception exception, ProjectSnapshot? project) | ||
{ | ||
} | ||
|
||
public override void ReportError(Exception exception, Project workspaceProject) | ||
{ | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ProjectSystem/ProjectSnapshotManagerBenchmarkBase.TestProjectSnapshotManagerDispatcher.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,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Razor; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Microbenchmarks; | ||
|
||
public abstract partial class ProjectSnapshotManagerBenchmarkBase | ||
{ | ||
private class TestProjectSnapshotManagerDispatcher : ProjectSnapshotManagerDispatcher | ||
{ | ||
public override bool IsDispatcherThread => true; | ||
|
||
public override TaskScheduler DispatcherScheduler => TaskScheduler.Default; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Razor.Extensions; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Telemetry; | ||
using Microsoft.AspNetCore.Razor.PooledObjects; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Razor; | ||
|
@@ -22,48 +18,54 @@ | |
|
||
namespace Microsoft.AspNetCore.Razor.Microbenchmarks; | ||
|
||
public class ProjectSnapshotManagerBenchmarkBase | ||
public abstract partial class ProjectSnapshotManagerBenchmarkBase | ||
{ | ||
public ProjectSnapshotManagerBenchmarkBase() | ||
internal HostProject HostProject { get; } | ||
internal ImmutableArray<HostDocument> Documents { get; } | ||
internal ImmutableArray<TextLoader> TextLoaders { get; } | ||
internal TagHelperResolver TagHelperResolver { get; } | ||
|
||
protected ProjectSnapshotManagerBenchmarkBase() | ||
{ | ||
var current = new DirectoryInfo(AppContext.BaseDirectory); | ||
while (current != null && !File.Exists(Path.Combine(current.FullName, "Razor.sln"))) | ||
while (current is not null && !File.Exists(Path.Combine(current.FullName, "Razor.sln"))) | ||
{ | ||
current = current.Parent; | ||
} | ||
|
||
var root = current; | ||
var root = current ?? throw new InvalidOperationException("Could not find Razor.sln"); | ||
var projectRoot = Path.Combine(root.FullName, "src", "Razor", "test", "testapps", "LargeProject"); | ||
|
||
HostProject = new HostProject(Path.Combine(projectRoot, "LargeProject.csproj"), FallbackRazorConfiguration.MVC_2_1, rootNamespace: null); | ||
|
||
TextLoaders = new TextLoader[4]; | ||
using var _1 = ArrayBuilderPool<TextLoader>.GetPooledObject(out var textLoaders); | ||
|
||
for (var i = 0; i < 4; i++) | ||
{ | ||
var filePath = Path.Combine(projectRoot, "Views", "Home", $"View00{i % 4}.cshtml"); | ||
var text = SourceText.From(filePath, encoding: null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🪲 A |
||
TextLoaders[i] = TextLoader.From(TextAndVersion.Create(text, VersionStamp.Create())); | ||
var fileText = File.ReadAllText(filePath); | ||
var text = SourceText.From(fileText); | ||
textLoaders.Add( | ||
TextLoader.From(TextAndVersion.Create(text, VersionStamp.Create(), filePath))); | ||
} | ||
|
||
Documents = new HostDocument[100]; | ||
for (var i = 0; i < Documents.Length; i++) | ||
TextLoaders = textLoaders.ToImmutable(); | ||
|
||
using var _2 = ArrayBuilderPool<HostDocument>.GetPooledObject(out var documents); | ||
|
||
for (var i = 0; i < 100; i++) | ||
{ | ||
var filePath = Path.Combine(projectRoot, "Views", "Home", $"View00{i % 4}.cshtml"); | ||
Documents[i] = new HostDocument(filePath, $"/Views/Home/View00{i}.cshtml", FileKinds.Legacy); | ||
documents.Add( | ||
new HostDocument(filePath, $"/Views/Home/View00{i}.cshtml", FileKinds.Legacy)); | ||
} | ||
|
||
Documents = documents.ToImmutable(); | ||
|
||
var tagHelpers = Path.Combine(root.FullName, "src", "Razor", "benchmarks", "Microsoft.AspNetCore.Razor.Microbenchmarks", "taghelpers.json"); | ||
TagHelperResolver = new StaticTagHelperResolver(ReadTagHelpers(tagHelpers), NoOpTelemetryReporter.Instance); | ||
} | ||
|
||
internal HostProject HostProject { get; } | ||
|
||
internal HostDocument[] Documents { get; } | ||
|
||
internal TextLoader[] TextLoaders { get; } | ||
|
||
internal TagHelperResolver TagHelperResolver { get; } | ||
|
||
internal DefaultProjectSnapshotManager CreateProjectSnapshotManager() | ||
{ | ||
var services = TestServices.Create( | ||
|
@@ -86,68 +88,10 @@ internal DefaultProjectSnapshotManager CreateProjectSnapshotManager() | |
private static IReadOnlyList<TagHelperDescriptor> ReadTagHelpers(string filePath) | ||
{ | ||
var serializer = new JsonSerializer(); | ||
serializer.Converters.Add(new RazorDiagnosticJsonConverter()); | ||
serializer.Converters.Add(RazorDiagnosticJsonConverter.Instance); | ||
serializer.Converters.Add(TagHelperDescriptorJsonConverter.Instance); | ||
|
||
using (var reader = new JsonTextReader(File.OpenText(filePath))) | ||
{ | ||
return serializer.Deserialize<IReadOnlyList<TagHelperDescriptor>>(reader); | ||
} | ||
} | ||
|
||
private class TestProjectSnapshotManagerDispatcher : ProjectSnapshotManagerDispatcher | ||
{ | ||
public override bool IsDispatcherThread => true; | ||
|
||
public override TaskScheduler DispatcherScheduler => TaskScheduler.Default; | ||
} | ||
|
||
private class TestErrorReporter : ErrorReporter | ||
{ | ||
public override void ReportError(Exception exception) | ||
{ | ||
} | ||
|
||
public override void ReportError(Exception exception, ProjectSnapshot project) | ||
{ | ||
} | ||
|
||
public override void ReportError(Exception exception, Project workspaceProject) | ||
{ | ||
} | ||
} | ||
|
||
private class StaticTagHelperResolver : TagHelperResolver | ||
{ | ||
private readonly IReadOnlyList<TagHelperDescriptor> _tagHelpers; | ||
|
||
public StaticTagHelperResolver(IReadOnlyList<TagHelperDescriptor> tagHelpers, ITelemetryReporter telemetryReporter) | ||
: base(telemetryReporter) | ||
{ | ||
_tagHelpers = tagHelpers; | ||
} | ||
|
||
public override Task<TagHelperResolutionResult> GetTagHelpersAsync(Project project, ProjectSnapshot projectSnapshot, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(new TagHelperResolutionResult(_tagHelpers, Array.Empty<RazorDiagnostic>())); | ||
} | ||
} | ||
|
||
private class StaticProjectSnapshotProjectEngineFactory : ProjectSnapshotProjectEngineFactory | ||
{ | ||
public override IProjectEngineFactory FindFactory(ProjectSnapshot project) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override IProjectEngineFactory FindSerializableFactory(ProjectSnapshot project) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override RazorProjectEngine Create(RazorConfiguration configuration, RazorProjectFileSystem fileSystem, Action<RazorProjectEngineBuilder> configure) | ||
{ | ||
return RazorProjectEngine.Create(configuration, fileSystem, b => RazorExtensions.Register(b)); | ||
} | ||
using var reader = new JsonTextReader(File.OpenText(filePath)); | ||
return serializer.Deserialize<IReadOnlyList<TagHelperDescriptor>>(reader) ?? Array.Empty<TagHelperDescriptor>(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉