Created
May 14, 2024 01:08
-
-
Save kekyo/004e6970d9e442c96b672838a9e18a9d to your computer and use it in GitHub Desktop.
Performance test for Cecil loader and hashing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Mono.Cecil; | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Security.Cryptography; | |
namespace ConsoleApp1; | |
public static class Program | |
{ | |
private static void RunCecil( | |
ReadingMode mode, byte[] image, | |
out TimeSpan load, out TimeSpan iterate) | |
{ | |
var parameters = new ReaderParameters(mode) | |
{ | |
InMemory = false, | |
ReadWrite = false, | |
}; | |
var sw = new Stopwatch(); | |
sw.Start(); | |
using var assembly = AssemblyDefinition.ReadAssembly( | |
new MemoryStream(image), | |
parameters); | |
load = sw.Elapsed; | |
var methods = assembly.MainModule.Types. | |
SelectMany(t => t.Methods.Cast<MemberReference>().Concat(t.Fields)). | |
ToArray(); | |
iterate = sw.Elapsed - load; | |
} | |
private static void RunHash( | |
HashAlgorithm alg, byte[] image, out TimeSpan calculate) | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var hash = alg.ComputeHash(new MemoryStream(image)); | |
calculate = sw.Elapsed; | |
} | |
public static void Main(string[] args) | |
{ | |
var image = File.ReadAllBytes("mscorlib.dll"); | |
RunCecil( | |
ReadingMode.Deferred, image, | |
out var deferredLoad, out var deferredIterate); | |
Console.WriteLine($"Cecil deferred: {deferredLoad}, {deferredIterate}, Total={deferredLoad + deferredIterate}"); | |
RunCecil( | |
ReadingMode.Immediate, image, | |
out var immediateLoad, out var immediateIterate); | |
Console.WriteLine($"Cecil immediate: {immediateLoad}, {immediateIterate}, Total={immediateLoad + immediateIterate}"); | |
RunHash( | |
MD5.Create(), | |
image, | |
out var md5Calculate); | |
Console.WriteLine($"MD5: {md5Calculate}"); | |
RunHash( | |
SHA1.Create(), | |
image, | |
out var sha1Calculate); | |
Console.WriteLine($"SHA1: {sha1Calculate}"); | |
RunHash( | |
SHA256.Create(), | |
image, | |
out var sha256Calculate); | |
Console.WriteLine($"SHA256: {sha256Calculate}"); | |
} | |
} |
Author
kekyo
commented
May 14, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment