Skip to content

Instantly share code, notes, and snippets.

@m-wild
Created August 12, 2024 22:16
Show Gist options
  • Save m-wild/35395c6c4ec980f63d608f8e3155a72a to your computer and use it in GitHub Desktop.
Save m-wild/35395c6c4ec980f63d608f8e3155a72a to your computer and use it in GitHub Desktop.
LINQPad 8 Antivirus Performance Test

This is adjusted from LINQPad 8's Antivirus Performance Test but converted to run as a standard console app

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>antivirus_perf_test</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
</ItemGroup>
</Project>
// Antivirus Performance Test
//
// This query generates a tiny (2KB) DLL and times how long it takes to load.
//
// Normal results on a healthy computer:
// - 1 millisecond or less with antivirus software disabled
// - 30 milliseconds or less with antivirus software enabled
using System.Diagnostics;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
Random random = new();
Console.WriteLine($"ProcessName = {Path.GetFileName(Environment.ProcessPath)}");
Console.WriteLine($"OutputFolder = {Environment.CurrentDirectory}");
for (int i = 1; i < 11; i++)
{
string path = $"test{i}.dll";
CreateTinyAssembly (path);
var sw = Stopwatch.StartNew();
Assembly.LoadFile (Path.GetFullPath (path)); // This should not take long!
sw.Stop();
Console.WriteLine($"Test {i} - {Math.Round (sw.Elapsed.TotalMilliseconds, 3)} milliseconds");
}
Environment.Exit (0); // End the process right away to release locks on the assemblies.
void CreateTinyAssembly (string path)
{
// Use the Microsoft Roslyn API to compile a DLL from a really simple class.
// Start with a SyntaxTree containing the source code:
var tree = CSharpSyntaxTree.ParseText ($"class Test{random.Next()} {{}}");
var compilation = CSharpCompilation // Create a compilation
.Create ("test")
.WithOptions (new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary))
.AddSyntaxTrees (tree)
.AddReferences (MetadataReference.CreateFromFile (typeof (int).Assembly.Location));
EmitResult result = compilation.Emit (path); // Compile to disk
if (!result.Success)
{
throw new Exception ("Could not compile code");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment