This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 770
/
BenchmarkRunner.cs
239 lines (207 loc) · 7.47 KB
/
BenchmarkRunner.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
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
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using ICSharpCode.Profiler.Controller;
using ICSharpCode.Profiler.Controller.Data;
namespace BenchmarkRunner
{
class Program
{
const int TestRunCount = 5;
public static void Main(string[] args)
{
ProcessStartInfo benchmarkStartInfo = new ProcessStartInfo("Benchmark.exe", "/automated");
benchmarkStartInfo.UseShellExecute = false;
ProcessStartInfo benchmark32StartInfo = new ProcessStartInfo("Benchmark32.exe", "/automated");
benchmark32StartInfo.UseShellExecute = false;
List<TestSeries> testSeries = new List<TestSeries>();
if (File.Exists(benchmark32StartInfo.FileName)) {
testSeries.Add(new TestSeries("Without profiler (32-bit)", () => WithoutProfiler(benchmark32StartInfo)));
testSeries.Add(new TestSeries("With profiler (32-bit)", () => WithProfiler(benchmark32StartInfo)));
} else {
Console.WriteLine("32-bit tests not executed: could not find Benchmark32.exe");
}
if (IntPtr.Size == 8) {
// in 64-bit mode, also test 64-bit profiler
if (File.Exists(benchmarkStartInfo.FileName)) {
testSeries.Add(new TestSeries("Without profiler (64-bit)", () => WithoutProfiler(benchmarkStartInfo)));
testSeries.Add(new TestSeries("With profiler (64-bit)", () => WithProfiler(benchmarkStartInfo)));
} else {
Console.WriteLine("64-bit tests not executed: could not find Benchmark.exe");
}
}
if (testSeries.Any()) {
for (int i = 0; i < TestRunCount; i++) {
foreach (TestSeries ts in testSeries) {
ts.ExecuteRun();
}
}
ResultSet[] results = testSeries.Select(ts=>ts.GetResult()).ToArray();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
DisplayResults(results);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
class ProfilingResults
{
public string DatabaseSize;
}
static ProfilingResults WithoutProfiler(ProcessStartInfo startInfo)
{
Process p = Process.Start(startInfo);
p.WaitForExit();
return new ProfilingResults {
DatabaseSize = FormatBytes(0)
};
}
static ProfilingResults WithProfiler(ProcessStartInfo startInfo)
{
string fileName = Path.Combine(Path.GetDirectoryName(typeof(Profiler).Assembly.Location), "output.sdps");
if (File.Exists(fileName))
File.Delete(fileName);
using (var profiler = new Profiler(
startInfo, new ProfilingDataSQLiteWriter(fileName), new ProfilerOptions()
)) {
using (ManualResetEvent mre = new ManualResetEvent(false)) {
profiler.SessionEnded += delegate {
mre.Set();
};
profiler.Start();
mre.WaitOne();
}
}
return new ProfilingResults {
DatabaseSize = FormatBytes(new FileInfo(fileName).Length)
};
}
class TestSeries
{
string name;
Func<ProfilingResults> runTestSet;
List<List<Measurement>> measurements = new List<List<Measurement>>();
public TestSeries(string name, Func<ProfilingResults> runTestSet)
{
this.name = name;
this.runTestSet = runTestSet;
}
public void ExecuteRun()
{
Thread.Sleep(100);
Console.WriteLine("Testset '" + name + "', run " + (measurements.Count+1));
Stopwatch watch = new Stopwatch();
watch.Start();
ProfilingResults results = runTestSet();
watch.Stop();
List<string> content = new List<string>();
content.Add("Total external time: " + FormatTime(watch.Elapsed.TotalMilliseconds));
foreach (var field in typeof(ProfilingResults).GetFields()) {
string value = field.GetValue(results) as string;
if (value != null)
content.Add(field.Name + ": " + value);
}
content.ForEach(Console.WriteLine);
measurements.Add(File.ReadAllLines("benchmark.out").Concat(content).Select(line=>Measurement.Parse(line)).ToList());
}
public ResultSet GetResult()
{
Measurement[][] mSwapped = new Measurement[measurements[0].Count][];
for (int j = 0; j < mSwapped.Length; j++) {
mSwapped[j] = new Measurement[measurements.Count];
for (int i = 0; i < measurements.Count; i++) {
mSwapped[j][i] = measurements[i][j];
}
}
return new ResultSet {
Name = name,
Results = mSwapped.Select(m=>new Result(SelectMiddle(m))).ToList()
};
}
}
static string FormatTime(double milliseconds)
{
return milliseconds.ToString("f6", System.Globalization.NumberFormatInfo.InvariantInfo) + "ms";
}
static string FormatBytes(long bytes)
{
return (bytes / 1048576.0).ToString("f4", System.Globalization.NumberFormatInfo.InvariantInfo) + "MB";
}
static Measurement[] SelectMiddle(Measurement[] m)
{
return m.OrderBy(x=>x.Result).Skip(m.Length / 10).Take(7 * m.Length / 10).ToArray();
}
static void DisplayResults(ResultSet[] resultSets)
{
int maxNameLength = 2 + resultSets.Max(s => s.Name.Length);
for (int i = 0; i < resultSets[0].Results.Count; i++) {
Console.WriteLine();
Console.WriteLine(resultSets[0].Results[i].Name);
var averageStrings = resultSets.Select(s => s.Results[i].Average.ToString("f4")).ToArray();
var sdStrings = resultSets.Select(s => s.Results[i].StandardDeviation.ToString("f4")).ToArray();
int maxAverageLength = averageStrings.Max(s => s.Length);
int maxSDLength = sdStrings.Max(s => s.Length);
var bestResult = resultSets.Min(s => s.Results[i].Average);
var factorStrings = resultSets.Select(s => (s.Results[i].Average / bestResult).ToString("f2")).ToArray();
int maxFactorLength = factorStrings.Max(s => s.Length);
for (int j = 0; j < resultSets.Length; j++) {
Console.WriteLine(" " + (resultSets[j].Name + ":").PadRight(maxNameLength)
+ averageStrings[j].PadLeft(maxAverageLength) + resultSets[0].Results[i].Unit
+ " +- " + sdStrings[j].PadLeft(maxSDLength) + resultSets[0].Results[i].Unit
+ " (" + factorStrings[j].PadLeft(maxFactorLength) + "x)");
}
}
}
class Measurement
{
public string Name;
public double Result;
public string Unit;
public static Measurement Parse(string text)
{
Match m = Regex.Match(text, @"^([\w\d\s]+):\s+([\d.]+)(ms|MB)");
if (m.Success) {
return new Measurement {
Name = m.Groups[1].Value,
Result = double.Parse(m.Groups[2].Value, System.Globalization.NumberFormatInfo.InvariantInfo),
Unit = m.Groups[3].Value
};
} else {
throw new Exception("Unknown output line " + text);
}
}
}
class Result
{
public string Name;
public double Average;
public double StandardDeviation;
public string Unit;
public Result(Measurement[] measurements)
{
Name = measurements[0].Name;
Unit = measurements[0].Unit;
double total = measurements.Sum(m=>m.Result);
Average = total / measurements.Length;
double variance = measurements.Sum(m => {
double dist = m.Result - Average;
return dist * dist;
});
variance /= (measurements.Length - 1);
StandardDeviation = Math.Sqrt(variance);
}
}
class ResultSet
{
public string Name;
public List<Result> Results;
}
}
}