Tests: Add benchmarking to test stages

This commit is contained in:
Katy Coe
2020-12-21 05:11:48 +01:00
parent b19ef87ed2
commit 1ab9c03c2f

View File

@@ -5,6 +5,8 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -16,6 +18,22 @@ using NUnit.Framework;
namespace Il2CppInspector namespace Il2CppInspector
{ {
internal class Benchmark : IDisposable
{
private readonly Stopwatch timer = new Stopwatch();
private readonly string benchmarkName;
public Benchmark(string benchmarkName) {
this.benchmarkName = benchmarkName;
timer.Start();
}
public void Dispose() {
timer.Stop();
Console.WriteLine($"{benchmarkName}: {timer.Elapsed.TotalSeconds:N2} sec");
}
}
[TestFixture] [TestFixture]
public partial class TestRunner public partial class TestRunner
{ {
@@ -42,7 +60,9 @@ namespace Il2CppInspector
loadOptions.BinaryFilePath = testFile; loadOptions.BinaryFilePath = testFile;
var inspectors = Il2CppInspector.LoadFromFile(testFile, testPath + @"\global-metadata.dat", loadOptions); List<Il2CppInspector> inspectors;
using (new Benchmark("Load IL2CPP metadata and binary"))
inspectors = Il2CppInspector.LoadFromFile(testFile, testPath + @"\global-metadata.dat", loadOptions);
// If null here, there was a problem parsing the files // If null here, there was a problem parsing the files
if (inspectors == null) if (inspectors == null)
@@ -54,21 +74,30 @@ namespace Il2CppInspector
// Dump each image in the binary separately // Dump each image in the binary separately
Parallel.ForEach(inspectors, il2cpp => { Parallel.ForEach(inspectors, il2cpp => {
var model = new TypeModel(il2cpp); TypeModel model;
var appModel = new AppModel(model, makeDefaultBuild: false).Build(compiler: CppCompilerType.MSVC); using (new Benchmark("Create .NET type model"))
model = new TypeModel(il2cpp);
AppModel appModel;
using (new Benchmark("Create application model"))
appModel = new AppModel(model, makeDefaultBuild: false).Build(compiler: CppCompilerType.MSVC);
var nameSuffix = "-" + il2cpp.BinaryImage.Arch.ToLower(); var nameSuffix = "-" + il2cpp.BinaryImage.Arch.ToLower();
new CSharpCodeStubs(model) { using (new Benchmark("Create C# code stubs"))
ExcludedNamespaces = Constants.DefaultExcludedNamespaces, new CSharpCodeStubs(model) {
SuppressMetadata = false, ExcludedNamespaces = Constants.DefaultExcludedNamespaces,
MustCompile = true SuppressMetadata = false,
}.WriteSingleFile(testPath + $@"\test-result{nameSuffix}.cs"); MustCompile = true
}.WriteSingleFile(testPath + $@"\test-result{nameSuffix}.cs");
new JSONMetadata(appModel) using (new Benchmark("Create JSON metadata"))
.Write(testPath + $@"\test-result{nameSuffix}.json"); new JSONMetadata(appModel)
.Write(testPath + $@"\test-result{nameSuffix}.json");
new CppScaffolding(appModel) using (new Benchmark("Create C++ scaffolding"))
.Write(testPath + $@"\test-cpp-result{nameSuffix}"); new CppScaffolding(appModel)
.Write(testPath + $@"\test-cpp-result{nameSuffix}");
var python = new PythonScript(appModel); var python = new PythonScript(appModel);
foreach (var target in PythonScript.GetAvailableTargets()) foreach (var target in PythonScript.GetAvailableTargets())
@@ -78,8 +107,9 @@ namespace Il2CppInspector
}); });
// Compare test results with expected results // Compare test results with expected results
foreach (var il2cpp in inspectors) { using var _ = new Benchmark("Compare files");
var suffix = "-" + il2cpp.BinaryImage.Arch.ToLower(); for (var i = 0; i < inspectors.Count; i++) {
var suffix = (i > 0 ? "-" + i : "");
compareFiles(testPath, suffix + ".cs", $"test-result{suffix}.cs"); compareFiles(testPath, suffix + ".cs", $"test-result{suffix}.cs");
compareFiles(testPath, suffix + ".json", $"test-result{suffix}.json"); compareFiles(testPath, suffix + ".json", $"test-result{suffix}.json");