Skip to content

Remove more setup overhead in RDG benchmarks #50302

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

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Http.Generators.Tests;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.AspNetCore.Http.Microbenchmarks;

Expand All @@ -12,21 +16,34 @@ public class RequestDelegateGeneratorBenchmarks : RequestDelegateCreationTestBas
[Params(10, 100, 1000, 10000)]
public int EndpointCount { get; set; }

private string _source;
private GeneratorDriver _driver;
private Compilation _compilation;

[GlobalSetup]
public void Setup()
public async Task Setup()
{
_source = "";
var project = CreateProject();
var innerSource = "";
for (var i = 0; i < EndpointCount; i++)
{
_source += $"""app.MapGet("/route{i}", (int? id) => "Hello World!");""";
innerSource += $"""app.MapGet("/route{i}", (int? id) => "Hello World!");""";
}
var source = GetMapActionString(innerSource);
project = project.AddDocument("TestMapActions.cs", SourceText.From(source, Encoding.UTF8)).Project;
_compilation = await project.GetCompilationAsync();

var generator = new RequestDelegateGenerator.RequestDelegateGenerator().AsSourceGenerator();
_driver = CSharpGeneratorDriver.Create(generators: new[]
{
generator
},
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true),
parseOptions: ParseOptions);
}

[Benchmark]
public async Task CreateRequestDelegate()
public void CreateRequestDelegate()
{
await RunGeneratorAsync(_source);
_driver.RunGeneratorsAndUpdateCompilation(_compilation, out var _, out var _);
}
}