-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathProgram.cs
More file actions
228 lines (211 loc) · 6.91 KB
/
Program.cs
File metadata and controls
228 lines (211 loc) · 6.91 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using BenchmarkDotNet.Running;
using static Towel.CommandLine;
namespace Towel_Benchmarking;
public class Program
{
internal const string Name = nameof(Name);
internal const string OutputFile = nameof(OutputFile);
public static void Main(string[] args)
{
if (args is null || args.Length == 0)
{
run();
}
else
{
HandleArguments(args);
}
}
internal static readonly Type[] Benchmarks =
{
typeof(SortBenchmarks),
typeof(DataStructuresBenchmarks),
typeof(WeightedRandomBenchmarks),
typeof(ToEnglishWordsBenchmarks),
typeof(PermuteBenchmarks),
typeof(MapVsDictionaryAddBenchmarks),
typeof(MapVsDictionaryLookUpBenchmarks),
typeof(SpanVsArraySortingBenchmarks),
typeof(RandomWithExclusionsBenchmarks),
typeof(HeapGenericsVsDelegatesBenchmarks),
typeof(LazyInitializationBenchmarks),
typeof(LazyCachingBenchmarks),
typeof(LazyConstructionBenchmarks),
typeof(SourceOfBenchmarks),
};
/// <summary>Runs the benchmarks.</summary>
/// <param name="updateDocumentation">Whether or not to update the docfx documentation.</param>
/// <param name="refreshToc">Whether or not to refresh "toc.yml".</param>
/// <param name="tocPath">The path to the docfx documentation file.</param>
/// <param name="includeRegex">Runs all the benchmarks that match a regex pattern.</param>
/// <example>dotnet run --configuration Release run --updateDocumentation True --refreshToc True</example>
/// <example>dotnet run --configuration Release run --updateDocumentation True --refreshToc True --includeRegex PATTERN</example>
[Command]
public static void run(
bool updateDocumentation = false,
bool refreshToc = false,
string? tocPath = null,
string? includeRegex = null)
{
string thisPath = Path.GetDirectoryName(sourcefilepath())!;
if (refreshToc)
{
tocPath ??= Path.Combine(thisPath, "..", "docfx_project", "benchmarks", "toc.yml");
string[] lines =
{
"- name: Introduction",
" href: index.md",
};
File.WriteAllLines(tocPath, lines);
}
string? benchmarks_mdPath = default;
if (updateDocumentation)
{
benchmarks_mdPath = Path.Combine(thisPath, "..", "docfx_project", "benchmarks", "index.md");
string[] lines =
{
"# Towel Benchmarks",
"",
@"<a href=""https://github.com/ZacharyPatten/Towel"" alt=""Github Repository""><img alt=""github repo"" src=""https://img.shields.io/badge/github-repo-black?logo=github&style=flat"" title=""Go To Github Repo"" alt=""Github Repository""></a>",
"",
"The source code for all becnhmarks are in [Tools/ Towel.Benchmarking](https://github.com/ZacharyPatten/Towel/tree/main/Tools/Towel_Benchmarking).",
"",
};
File.WriteAllLines(benchmarks_mdPath, lines);
}
foreach (Type type in Benchmarks)
{
if (includeRegex is null || Regex.Match(type.Name, includeRegex).Success)
{
StringBuilder stringBuilder = new();
string output = RunBenchmarkAndGetMarkdownOutput(type);
stringBuilder.AppendLine($"# {type.GetTag(Name).Value ?? type.Name}");
stringBuilder.AppendLine();
stringBuilder.AppendLine(@"<a href=""https://github.com/ZacharyPatten/Towel"" alt=""Github Repository""><img alt=""github repo"" src=""https://img.shields.io/badge/github-repo-black?logo=github&style=flat"" title=""Go To Github Repo"" alt=""Github Repository""></a>");
stringBuilder.AppendLine();
stringBuilder.AppendLine("The source code for all benchmarks are in [Tools/Towel.Benchmarking](https://github.com/ZacharyPatten/Towel/tree/main/Tools/Towel_Benchmarking).");
stringBuilder.AppendLine();
stringBuilder.AppendLine(output);
if (updateDocumentation)
{
string documentationPath = Path.Combine(thisPath, "..", "docfx_project", "benchmarks", type.GetTag(OutputFile).Value + ".md");
if (Directory.Exists(Path.GetDirectoryName(documentationPath)))
{
File.WriteAllText(documentationPath, stringBuilder.ToString());
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("-----------------------------------");
Console.Error.WriteLine("ERROR: documentation path not found");
Console.Error.WriteLine($" documentation path: {documentationPath}");
Console.Error.WriteLine("-----------------------------------");
Console.ResetColor();
}
}
else
{
Console.WriteLine(stringBuilder.ToString());
}
}
if (refreshToc)
{
string[] lines =
{
$"- name: {type.GetTag(Name).Value}",
$" href: {type.GetTag(OutputFile).Value}.md",
};
File.AppendAllLines(tocPath!, lines);
}
if (updateDocumentation)
{
string[] lines =
{
$"- [{type.GetTag(Name).Value}]({type.GetTag(OutputFile).Value}.md)",
};
File.AppendAllLines(benchmarks_mdPath!, lines);
}
}
Console.WriteLine("Benchmarking Complete. :)");
}
public static string RunBenchmarkAndGetMarkdownOutput(Type type)
{
var summary = BenchmarkRunner.Run(type);
string markdownFile = type.FullName + "-report-github.md";
string markdownPath = Path.Combine(summary.ResultsDirectoryPath, markdownFile);
try
{
return File.Exists(markdownPath)
? File.ReadAllText(markdownPath)
: $"Error: markdown file output for {type.Name} not found.";
}
catch (Exception exception)
{
return $"Error: {Environment.NewLine}{exception}";
}
}
}
#region Shared Random Data Generation
public class Person
{
public Guid Id;
public string? FirstName;
public string? LastName;
public DateTime DateOfBirth;
}
public struct EquatePerson : IFunc<Person, Person, bool>
{
public bool Invoke(Person a, Person b) => a.Id == b.Id;
}
public struct HashPerson : IFunc<Person, int>
{
public int Invoke(Person a) => a.Id.GetHashCode();
}
public struct ComparePersonFirstName : IFunc<Person, Person, CompareResult>
{
public CompareResult Invoke(Person a, Person b) => Compare(a.FirstName, b.FirstName);
}
public struct RandomNext : IFunc<int, int, int>
{
public Random Random;
public RandomNext() => Random = new();
public int Invoke(int arg1, int arg2) => Random.Next(arg1, arg2);
}
public static partial class RandomData
{
public static class DataStructures
{
public static Person[][] RandomData => GenerateBenchmarkData();
public static Person[][] GenerateBenchmarkData()
{
DateTime minimumBirthDate = new(1950, 1, 1);
DateTime maximumBirthDate = new(2000, 1, 1);
Random random = new(7);
Person[] GenerateData(int count)
{
Person[] data = new Person[count];
for (int i = 0; i < count; i++)
{
data[i] = new Person()
{
Id = Guid.NewGuid(),
FirstName = random.NextEnglishAlphabeticString(random.Next(5, 11)),
LastName = random.NextEnglishAlphabeticString(random.Next(5, 11)),
DateOfBirth = random.NextDateTime(minimumBirthDate, maximumBirthDate)
};
}
return data;
}
return new Person[][]
{
GenerateData(10),
GenerateData(100),
GenerateData(1000),
};
}
}
}
#endregion