Skip to content
Merged
Show file tree
Hide file tree
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
150 changes: 150 additions & 0 deletions benchmarks/E128.Benchmarks/PackageAnalyzerBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using E128.Analyzers.Design;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;

namespace E128.Benchmarks;

/// <summary>
/// Starter benchmark for the consumer critical path of the E128.Analyzers package: every analyzer in the
/// package runs over one ordinary service-style compilation, as it does in a consumer build. The compilation
/// references the whole shared framework and is built once, so the number shows warm analyzer cost, not
/// first-compile cost. The code fixers are not measured here.
/// BenchmarkDotNet requires an unsealed class, so this class is not sealed.
/// </summary>
[MemoryDiagnoser]
public class PackageAnalyzerBenchmarks
{
private const string Source = """
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Sample;

public sealed record Order(int Id, string Customer, decimal Total, DateTime CreatedAt);

public interface IOrderStore
{
Task<IReadOnlyList<Order>> LoadAsync(CancellationToken cancellationToken);
}

public sealed class OrderReport
{
private readonly IOrderStore _store;
private readonly HttpClient _client;

public OrderReport(IOrderStore store, HttpClient client)
{
_store = store;
_client = client;
}

public async Task<string> BuildAsync(string path, CancellationToken cancellationToken)
{
var orders = await _store.LoadAsync(cancellationToken).ConfigureAwait(false);
var builder = new StringBuilder();
foreach (var group in orders.GroupBy(order => order.Customer))
{
var total = group.Sum(order => order.Total);
builder.Append(group.Key + ": " + total);
builder.AppendLine();
}

var status = await _client.GetStringAsync(new Uri("https://example.com/status"), cancellationToken)
.ConfigureAwait(false);
if (status == "down" || status == "maintenance" || status == "degraded")
{
return string.Empty;
}

var text = builder.ToString();
await File.WriteAllTextAsync(path, text, cancellationToken).ConfigureAwait(false);
return File.ReadAllText(path);
}

public static Order? Latest(List<Order> orders)
{
return orders.OrderByDescending(order => order.CreatedAt).FirstOrDefault();
}

public static DateTime ParseDate(string value)
{
return DateTime.Parse(value);
}
}
""";

private readonly CSharpCompilation _compilation = CreateCompilation();

private readonly ImmutableArray<DiagnosticAnalyzer> _analyzers =
[
.. typeof(ExcessiveInheritanceAnalyzer).Assembly.GetTypes()
.Where(static type => !type.IsAbstract
&& typeof(DiagnosticAnalyzer).IsAssignableFrom(type)
&& Attribute.IsDefined(type, typeof(DiagnosticAnalyzerAttribute), inherit: false))
.Select(static type => (DiagnosticAnalyzer)(Activator.CreateInstance(type)
?? throw new InvalidOperationException($"Cannot create analyzer {type.FullName}.")))
];

[GlobalSetup]
public async Task VerifyPackageReportsAsync()
{
// An empty analyzer set or a run that reports nothing times only the cheap path. Fail fast instead.
if (_analyzers.IsEmpty)
{
throw new InvalidOperationException("No analyzer was found in the E128.Analyzers assembly.");
}

var diagnostics = await AllPackageAnalyzersAsync().ConfigureAwait(false);
if (!diagnostics.IsEmpty)
{
return;
}

throw new InvalidOperationException("The package analyzers reported no diagnostic for the sample source.");
}

[Benchmark]
public Task<ImmutableArray<Diagnostic>> AllPackageAnalyzersAsync()
{
return _compilation
.WithAnalyzers(_analyzers)
.GetAnalyzerDiagnosticsAsync(CancellationToken.None);
}

private static CSharpCompilation CreateCompilation()
{
var platformAssemblies = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") as string
?? throw new InvalidOperationException("The trusted platform assembly list is unavailable.");

var references = platformAssemblies
.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries)
.Select(static path => MetadataReference.CreateFromFile(path));

var compilation = CSharpCompilation.Create(
"PackageBenchmarkSample",
[CSharpSyntaxTree.ParseText(Source, cancellationToken: CancellationToken.None)],
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: NullableContextOptions.Enable));

var errors = compilation.GetDiagnostics(CancellationToken.None)
.Where(static diagnostic => diagnostic.Severity is DiagnosticSeverity.Error);

return errors.Any()
? throw new InvalidOperationException("The sample source does not compile.")
: compilation;
}
}
4 changes: 2 additions & 2 deletions lode/dotnet/analyzers/testing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Analyzer Testing

*Updated: 2026-11-16T20:16:49Z*
*Updated: 2026-11-16T22:58:37Z*

How a test drives an analyzer and a code fix. The harness types live in
`Microsoft.CodeAnalysis.CSharp.Testing`. The repo wrapper lives in
Expand Down Expand Up @@ -157,7 +157,7 @@ instead.

The NuGet package `E128.Analyzers` carries two assemblies: `E128.Analyzers` and
`E128.Analyzers.CodeFixers`. Add their branch totals to get the package surface.
As of 2026-11-16, that surface covers about 87 percent of its branches. See
As of 2026-11-16T22:58:32Z, that surface covers about 91 percent of its branches. See
[Code Coverage](../code-coverage.md) for collection and the CI floors.

## Related
Expand Down
17 changes: 10 additions & 7 deletions lode/dotnet/benchmarks.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# Benchmarks
*Updated: 2026-11-09T21:51:08Z*
*Updated: 2026-11-16T21:48:36Z*

## Overview

`benchmarks/E128.Benchmarks` is an executable project that holds BenchmarkDotNet starter benchmarks. A human must review the cases. They cover the public surface, not a measured hot path. The solution file lists the project in its `/benchmarks/` folder.

| Class | Subject |
| ----------------------- | -------------------------------------------------------------------------- |
| `CoreBenchmarks` | `Greeter`, `GreetingService`, and `InMemoryGreetingRepository` |
| `WebEndpointBenchmarks` | `GET /`, `POST /greetings`, and `GET /greetings` of the Web project |
| `AnalyzerBenchmarks` | Three E128 analyzers, each alone and together, over one sample compilation |
| Class | Subject |
| --------------------------- | -------------------------------------------------------------------------- |
| `CoreBenchmarks` | `Greeter`, `GreetingService`, and `InMemoryGreetingRepository` |
| `WebEndpointBenchmarks` | `GET /`, `POST /greetings`, and `GET /greetings` of the Web project |
| `AnalyzerBenchmarks` | Three E128 analyzers, each alone and together, over one sample compilation |
| `PackageAnalyzerBenchmarks` | Every analyzer in the `E128.Analyzers` assembly over one sample service |

Each class uses `[MemoryDiagnoser]`. The packages are in [dep-map.md](../dotnet-reference/dep-map.md).

## Invariants

- BenchmarkDotNet rejects a sealed benchmark class. Each class is public and unsealed. A doc comment on the class states this.
- E128005 skips a class whose base type is `object`. `SealedClassTests` loads only the Core assembly. Neither gate covers this project. See [architecture-testing.md](architecture-testing.md).
- `benchmarks/.globalconfig` sets CA1515 to `none` at `global_level = 101`. BenchmarkDotNet needs public types. See [analyzers.md](analyzers.md).
- `benchmarks/.globalconfig` sets CA1515 to `none` at `global_level = 101`. BenchmarkDotNet needs public types. It also sets E128227 to `none` for the fixed in-process response. See [analyzers.md](analyzers.md).
- `EntryPoint` replaces a top-level `Program`. The Web project exposes `Program` through `InternalsVisibleTo`, and a second `Program` causes CS0436.
- `EntryPoint.Main` sets the working directory to `AppContext.BaseDirectory`. `WebApplicationFactory` reads `MvcTestingAppManifest.json` from the working directory. A relative path in a command-line argument resolves against that folder, so pass absolute paths.
- `EntryPoint.Main` adds `InProcessEmitToolchain` to the default config, so the benchmarks run in the host process. JIT and tiering state carries over between cases.
- `WebEndpointBenchmarks` runs the app behind the test server. The numbers exclude the network and the Kestrel socket layer. A `FixedGreetingRepository` replaces the in-memory repository, so stored state stays constant and the numbers do not drift. The repository cost is not measured.
- `AnalyzerBenchmarks` builds the sample compilation once, so the numbers show warm analyzer cost. It throws when the sample has a compile error. `GlobalSetup` throws when an analyzer reports no diagnostic.
- `PackageAnalyzerBenchmarks` measures the consumer build path. It finds each `[DiagnosticAnalyzer]` type in the analyzer assembly by reflection, so a new rule joins the run without an edit.
- `PackageAnalyzerBenchmarks` references every trusted platform assembly of the host runtime. `GlobalSetup` throws when it finds no analyzer or the run reports no diagnostic. The code fixers are not measured.
- `CoreBenchmarks` uses a `FixedGreetingRepository` with no greetings for the save benchmark, and a seeded in-memory repository for the read benchmark.

## Running
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Threading.Tasks;
using E128.Analyzers.Performance;
using Xunit;

namespace E128.Analyzers.Tests;

public sealed class AggressiveOptimizationCodeFixProviderBranchCoverageMoreTests
{
private static Task VerifyFixAsync(string source, string fixedCode)
{
return CodeFixTestRunner.RunAsync<AggressiveOptimizationAnalyzer, AggressiveOptimizationCodeFixProvider>(source, fixedCode);
}

[Fact]
[Trait("Category", "CI")]
public Task Fix_KeepsBothRemainingOptionsOfANestedCombination()
{
return VerifyFixAsync(
"""
using System.Runtime.CompilerServices;
class C
{
[{|E128116:MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)|}]
void M()
{
}
}
""",
"""
using System.Runtime.CompilerServices;
class C
{
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoInlining)]
void M()
{
}
}
""");
}

[Fact]
[Trait("Category", "CI")]
public Task Fix_DropsAnArgumentThatHoldsOnlyTheOptionTwice()
{
return VerifyFixAsync(
"""
using System.Runtime.CompilerServices;
class C
{
[{|E128116:MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveOptimization, MethodCodeType = MethodCodeType.Native)|}]
void M()
{
}
}
""",
"""
using System.Runtime.CompilerServices;
class C
{
[MethodImpl(MethodCodeType = MethodCodeType.Native)]
void M()
{
}
}
""");
}

[Fact]
[Trait("Category", "CI")]
public Task Fix_RemovesTheAttributeListOfALocalFunction()
{
return VerifyFixAsync(
"""
using System.Runtime.CompilerServices;
class C
{
void M()
{
[{|E128116:MethodImpl(MethodImplOptions.AggressiveOptimization)|}]
void Local()
{
}

Local();
}
}
""",
"""
using System.Runtime.CompilerServices;
class C
{
void M()
{
void Local()
{
}

Local();
}
}
""");
}
}
Loading