Skip to content

Instantly share code, notes, and snippets.

@safern
Last active April 28, 2021 22:49
Show Gist options
  • Save safern/6a1354ee3e3d1e13272c5c3119a6e466 to your computer and use it in GitHub Desktop.
Save safern/6a1354ee3e3d1e13272c5c3119a6e466 to your computer and use it in GitHub Desktop.
Api compat benchmarks (optimizing for multiple rights for the same left)
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.ApiCompatibility;
using Microsoft.DotNet.ApiCompatibility.Abstractions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace apicompatbenchmarks
{
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
public class ApiCompatBenchmarks
{
private static readonly string[] _rightPaths = new string[]
{
@"C:\repos\runtime\artifacts\bin\System.Runtime\net6.0-Debug\System.Runtime.dll",
@"C:\repos\runtime\artifacts\bin\System.Runtime\net6.0-Debug\System.Runtime.dll",
@"C:\repos\runtime\artifacts\bin\System.Runtime\net6.0-Debug\System.Runtime.dll",
@"C:\repos\runtime\artifacts\bin\System.Runtime\net6.0-Debug\System.Runtime.dll",
};
private const string _leftPath = @"C:\repos\runtime\artifacts\bin\microsoft.netcore.app.ref\ref\net6.0\System.Runtime.dll";
private const string _coreLibDir = @"C:\repos\runtime\artifacts\bin\coreclr\windows.x64.Debug\IL\";
private List<IAssemblySymbol> _rightAssemblies = new();
private IAssemblySymbol _leftAssembly;
[GlobalSetup(Targets = new[] { nameof(UseOneApiCompatPerPath), nameof(CompareOnePerSide) })]
public void LoadAssembliesSetup()
{
Stopwatch sw = new Stopwatch();
sw.Start();
_rightAssemblies.Clear();
AssemblySymbolLoader leftLoader = new AssemblySymbolLoader();
_leftAssembly = leftLoader.LoadAssembly(_leftPath);
foreach (string path in _rightPaths)
{
AssemblySymbolLoader rightLoader = new AssemblySymbolLoader(resolveAssemblyReferences: true);
rightLoader.AddReferenceSearchDirectories(_coreLibDir);
IAssemblySymbol right = rightLoader.LoadAssembly(path);
_rightAssemblies.Add(right);
}
sw.Stop();
Console.WriteLine($"Time loading assemblies {sw.ElapsedMilliseconds}ms");
}
[Benchmark]
public void UseOneApiCompatPerPath()
{
foreach (IAssemblySymbol right in _rightAssemblies)
{
ApiComparer comparer = new ApiComparer();
var _ = comparer.GetDifferences(_leftAssembly, right);
}
}
[Benchmark]
public void CompareOnePerSide()
{
ApiComparer comparer = new ApiComparer();
var _ = comparer.GetDifferences(_leftAssembly, _rightAssemblies[0]);
}
#if NEW_APICOMPAT
private List<ElementContainer<IAssemblySymbol>> _rightElements = new();
[GlobalSetup(Target = nameof(UseOneApiCompatOverall))]
public void SetupToUseOneApiCompat()
{
LoadAssembliesSetup();
_rightElements.Clear();
for (int i = 0; i < _rightAssemblies.Count; i++)
{
_rightElements.Add(new ElementContainer<IAssemblySymbol>(_rightAssemblies[i], new MetadataInformation("", "", i.ToString())));
}
}
[Benchmark]
public void UseOneApiCompatOverall()
{
ApiComparer comparer = new ApiComparer();
var _ = comparer.GetDifferences(new ElementContainer<IAssemblySymbol>(_leftAssembly, default), _rightElements);
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment