-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssemblyBuilder.cs
143 lines (120 loc) · 5.1 KB
/
AssemblyBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using InterfacePerfTest.SourceCodeBuilders;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Emit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace InterfacePerfTest
{
internal static class AssemblyBuilder
{
public static Assembly GetBenchmarkAssembly(int methodCount, bool printSource = false)
{
Console.WriteLine("Generating source code for containers and calling code...");
const int maxPrintableMethods = 10;
if (printSource && methodCount > maxPrintableMethods)
{
throw new InvalidOperationException($"{nameof(printSource)} can only be set to true when {nameof(methodCount)} is less than {maxPrintableMethods}, as it is intended to help with understanding the benchmark only.");
}
var methodCallOrder = Enumerable.Range(0, methodCount).ToList();
methodCallOrder.Shuffle();
List<string> sourceCodeFiles = new();
var benchmarkClass = _getBenchmarkClass();
sourceCodeFiles.Add(benchmarkClass);
List<IContainerSourceCodeBuilder> builders = _getSourceCodeBuilders(methodCount, methodCallOrder);
sourceCodeFiles.AddRange(_getSourceCodeFromBuilders(builders));
if (printSource)
{
Console.WriteLine("The following source code will be benchmarked:");
//var asterisks = new string('*', 10);
//int i = 1;
foreach (var file in sourceCodeFiles)
{
//Console.WriteLine(asterisks + $" File {i++} " + asterisks);
Console.Write(file);
}
}
Console.WriteLine("Compiling source code and creating in-memory assembly...");
var compilation = CompilationBuilder.CreateCompilation(sourceCodeFiles);
var assembly = _getAssemblyFromCompilation(compilation);
Console.WriteLine("Compilation complete. Now we can start benchmarking.");
return assembly;
}
private static List<IContainerSourceCodeBuilder> _getSourceCodeBuilders(int methodCount, IEnumerable<int> methodCallOrder)
{
List<IContainerSourceCodeBuilder> builders = new()
{
new ContainerNoInterfacesSourceCodeBuilder(methodCount, methodCallOrder),
new ContainerWithInterfacesSourceCodeBuilder(methodCount, methodCallOrder)
};
return builders;
}
private static List<string> _getSourceCodeFromBuilders(List<IContainerSourceCodeBuilder> builders)
{
List<string> sourceCodeFiles = new();
foreach (var builder in builders)
{
sourceCodeFiles.Add(builder.GetContainerSourceText());
sourceCodeFiles.Add(builder.GetCallingCodeSourceText());
}
return sourceCodeFiles;
}
private static string _getBenchmarkClass()
{
return @"
namespace MyBenchmarks
{
using BenchmarkDotNet.Attributes;
using MyCode;
public class BenchmarkClass
{
//[Benchmark(OperationsPerInvoke = 4)] // uncomment for shorter run during debugging
[Benchmark]
public long NoInterfaceBenchmark()
{
return NoInterfaceContainerCaller.BenchmarkMe();
}
//[Benchmark(OperationsPerInvoke = 4)] // uncomment for shorter run during debugging
[Benchmark]
public long WithInterfacesBenchmark()
{
return WithInterfacesContainerCaller.BenchmarkMe();
}
}
}
";
}
private static Assembly _getAssemblyFromCompilation(Compilation compilation)
{
Assembly? assembly = null;
// Create an in-memory assembly from the compilation.
// https://www.tugberkugurlu.com/archive/compiling-c-sharp-code-into-memory-and-executing-it-with-roslyn
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
else
{
ms.Seek(0, SeekOrigin.Begin);
assembly = Assembly.Load(ms.ToArray());
}
}
if (assembly == null)
{
throw new InvalidOperationException($"{nameof(assembly)} is null.");
}
return assembly;
}
}
}