Skip to content

Commit

Permalink
Adds Benchmarks (#367)
Browse files Browse the repository at this point in the history
* Fix first run bug.

* Remove unneeded .ToArray()

* Delete SqliteFilename

* Fix Roslyn Warnings

* Update DatabaseManager.cs

* Update InsertTestsWithIntermittentTransactions.cs

* Allow journal mode configuration

Fix pragmas to apply everytime a connection is opened per dotnet/efcore#5024

* Use a settings object for passing database settings.

Add a few more settings (pragma passthroughs).

* Add CommitTests

* Rename files.

* Move/Rename files.

Adds Query Tests

* Update QueryTests.cs

Documentation

* Make Synchronous a string so the values are more obvious

* Code Cleanup

* Add LiteDbManager placeholder.

* Add prelim litedb support (for benchmarking).

* Progress towards LiteDb Benchmark

* Small fixes

* Update LiteDbManager.cs

* Add System.Data.Sqlite implementation of DB manager

* Add nullable checks.

* Nullable fixes

* More nullable fixes

* Nullable Fixes pt. 1

Mostly finished with lib.

* Finish nullable work in lib

Aside from the experimental litedb work.

* More nullable fixes.

* More nullable work.

Re-adds SystemSQLite support.

* Fixes for SystemSQLite support

* Finish nullable work

* clean up GetCollectModified

* Add LiteDb Query Tests

* Move System.Data.SQLite and LiteDB code into the benchmarks

Removes those dependencies from the main library and cli.

* Fix null reference exceptions

* Remove old default arguments

We now use null

* Add a GetAllMissing function

* Update BaseCompare to use GetAllMissing

* Fix constructors so Objects are properly deserializable.

* Add new GetAllMissing2 function

This is a hybrid between the two approaches.

* Adds crypto tests

* Pull get missing query out out function

* Update FileSystemCollector.cs

* Update csproj files to improve assembly names

* Update csproj and pipelines

* Fix pipelines

* Rename tests file.

* Update to use SHA512

* Nullable work.

* Fix build.

* Help the pipeline find the tests properly

* Update Strings.cs

* Update Strings.cs

* Update AsaLibTests.cs

* Update pr-validation.yml

Now properly fail when tests fail but still report test results.

* Add BatchSize for writenext

* Update SqlConnectionHolder.cs

* Update SqlConnectionHolder.cs

* Update batching

* fix race condition

* Update InsertTestsWithoutTransactions.cs

* Fix batch size default and max batch size.

* Fix comobjectcollector

* Fix registry collector.

* Centralize code for translating sid to names

Only attempt to do so when it could possibly succeed, this reduces number of exceptions dramatically.

* Update AsaLibTests.cs

* Update AsaLibTests.cs

* Fix registry objects to distinguish which view they were gathered from.

* Gather Com Objects in Both Views
  • Loading branch information
gfs authored Mar 30, 2020
1 parent 101c358 commit e36c760
Show file tree
Hide file tree
Showing 129 changed files with 4,854 additions and 1,970 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bld/
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
Benchmarks/BenchmarkDotNet.Artifacts/

# MSTest test Results
[Tt]est[Rr]esult*/
Expand Down
16 changes: 0 additions & 16 deletions AsaBenchmarks/AsaBenchmarks.csproj

This file was deleted.

Binary file removed AsaBenchmarks/SqliteFilename
Binary file not shown.
42 changes: 21 additions & 21 deletions AttackSurfaceAnalyzer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.json = version.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsaLib", "Lib\AsaLib.csproj", "{5069C7AB-2264-4768-AB42-358251B0863A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenerateDocfx", "GenerateDocfx\GenerateDocfx.csproj", "{99B076F7-8861-40DC-8523-5A67EA58295D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Asa", "Asa\Asa.csproj", "{6429632B-E160-47C1-AB12-18D4438CDF4B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{51CF7466-7048-4A48-888D-7B4589CFEF5F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cli", "Cli\Cli.csproj", "{F8C81EAB-ECD0-4AC5-9100-59A0E37A536E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsaTests", "AsaTests\AsaTests.csproj", "{0C720734-2DA7-470A-9EAD-8FB5BCCF6924}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib", "Lib\Lib.csproj", "{458D8CAC-31E5-4843-BEBE-AC1DF52CA2EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsaBenchmarks", "AsaBenchmarks\AsaBenchmarks.csproj", "{0D893A37-772B-48FC-910C-9F09D55DF15D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{3A05B910-65F9-428C-9F27-FB69F893F250}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5069C7AB-2264-4768-AB42-358251B0863A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5069C7AB-2264-4768-AB42-358251B0863A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5069C7AB-2264-4768-AB42-358251B0863A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5069C7AB-2264-4768-AB42-358251B0863A}.Release|Any CPU.Build.0 = Release|Any CPU
{99B076F7-8861-40DC-8523-5A67EA58295D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99B076F7-8861-40DC-8523-5A67EA58295D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99B076F7-8861-40DC-8523-5A67EA58295D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99B076F7-8861-40DC-8523-5A67EA58295D}.Release|Any CPU.Build.0 = Release|Any CPU
{6429632B-E160-47C1-AB12-18D4438CDF4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6429632B-E160-47C1-AB12-18D4438CDF4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6429632B-E160-47C1-AB12-18D4438CDF4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6429632B-E160-47C1-AB12-18D4438CDF4B}.Release|Any CPU.Build.0 = Release|Any CPU
{0C720734-2DA7-470A-9EAD-8FB5BCCF6924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C720734-2DA7-470A-9EAD-8FB5BCCF6924}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C720734-2DA7-470A-9EAD-8FB5BCCF6924}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C720734-2DA7-470A-9EAD-8FB5BCCF6924}.Release|Any CPU.Build.0 = Release|Any CPU
{0D893A37-772B-48FC-910C-9F09D55DF15D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D893A37-772B-48FC-910C-9F09D55DF15D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D893A37-772B-48FC-910C-9F09D55DF15D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D893A37-772B-48FC-910C-9F09D55DF15D}.Release|Any CPU.Build.0 = Release|Any CPU
{51CF7466-7048-4A48-888D-7B4589CFEF5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{51CF7466-7048-4A48-888D-7B4589CFEF5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51CF7466-7048-4A48-888D-7B4589CFEF5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{51CF7466-7048-4A48-888D-7B4589CFEF5F}.Release|Any CPU.Build.0 = Release|Any CPU
{F8C81EAB-ECD0-4AC5-9100-59A0E37A536E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8C81EAB-ECD0-4AC5-9100-59A0E37A536E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8C81EAB-ECD0-4AC5-9100-59A0E37A536E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8C81EAB-ECD0-4AC5-9100-59A0E37A536E}.Release|Any CPU.Build.0 = Release|Any CPU
{458D8CAC-31E5-4843-BEBE-AC1DF52CA2EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{458D8CAC-31E5-4843-BEBE-AC1DF52CA2EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{458D8CAC-31E5-4843-BEBE-AC1DF52CA2EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{458D8CAC-31E5-4843-BEBE-AC1DF52CA2EA}.Release|Any CPU.Build.0 = Release|Any CPU
{3A05B910-65F9-428C-9F27-FB69F893F250}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A05B910-65F9-428C-9F27-FB69F893F250}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A05B910-65F9-428C-9F27-FB69F893F250}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A05B910-65F9-428C-9F27-FB69F893F250}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
34 changes: 34 additions & 0 deletions Benchmarks/AsaDatabaseBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using AttackSurfaceAnalyzer.Objects;
using AttackSurfaceAnalyzer.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace AttackSurfaceAnalyzer.Benchmarks
{
public class AsaDatabaseBenchmark
{
// Bag of reusable objects to write to the database.
public static readonly ConcurrentBag<FileSystemObject> BagOfObjects = new ConcurrentBag<FileSystemObject>();

public static FileSystemObject GetRandomObject(int ObjectPadding = 0)
{
BagOfObjects.TryTake(out FileSystemObject? obj);

if (obj != null)
{
obj.Path = CryptoHelpers.GetRandomString(32);
return obj;
}
else
{
return new FileSystemObject(CryptoHelpers.GetRandomString(32))
{
// Pad this field with extra data.
FileType = CryptoHelpers.GetRandomString(ObjectPadding),
};
}
}
}
}
27 changes: 27 additions & 0 deletions Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ReleaseVersion>2.1-alpha</ReleaseVersion>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="LiteDB" Version="5.0.4" />
<PackageReference Include="System.Data.SQLite" Version="1.0.112" />
<PackageReference Include="murmurhash" Version="1.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Lib\Lib.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="BenchmarkDotNet.Artifacts\OldResults\AttackSurfaceAnalyzer.Benchmarks.LiteDbInsertTests-report-github.md" />
<None Include="BenchmarkDotNet.Artifacts\OldResults\AttackSurfaceAnalyzer.Benchmarks.LiteDbInsertTests-report.csv" />
<None Include="BenchmarkDotNet.Artifacts\OldResults\AttackSurfaceAnalyzer.Benchmarks.LiteDbInsertTests-report.html" />
<None Include="BenchmarkDotNet.Artifacts\OldResults\AttackSurfaceAnalyzer.Benchmarks.LiteDbInsertTests-report-full.json" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions Benchmarks/CommitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using AttackSurfaceAnalyzer.Utils;
using BenchmarkDotNet.Attributes;

namespace AttackSurfaceAnalyzer.Benchmarks
{
[MarkdownExporterAttribute.GitHub]
[JsonExporterAttribute.Full]
public class CommitTest
{
//Rows to write in the open transaction before the commit
[Params(10000, 20000, 40000)]
public int N { get; set; }

// The number of Shards/Threads to use for Database operations
[Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)]
public int Shards { get; set; }

[Params("OFF", "DELETE", "WAL", "MEMORY")]
public string JournalMode { get; set; }

public CommitTest()
{
Logger.Setup(true, true);
Strings.Setup();
}

[Benchmark]
public void CommitTransaction()
{
DatabaseManager.Commit();
}

public void Setup()
{
DatabaseManager.Setup(filename: $"AsaBenchmark_{Shards}.sqlite", new DBSettings()
{
JournalMode = JournalMode,
ShardingFactor = Shards
});
}

[IterationSetup]
public void IterationSetup()
{
Setup();
DatabaseManager.BeginTransaction();
InsertTestsWithoutTransactions.Insert_X_Objects(N);
}

[IterationCleanup]
public void IterationCleanup()
{
DatabaseManager.Destroy();
}
}
}
108 changes: 108 additions & 0 deletions Benchmarks/CryptoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using AttackSurfaceAnalyzer.Objects;
using AttackSurfaceAnalyzer.Utils;
using BenchmarkDotNet.Attributes;
using Murmur;
using Serilog;
using System.Collections.Concurrent;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
using Utf8Json;

namespace AttackSurfaceAnalyzer.Benchmarks
{
[MarkdownExporterAttribute.GitHub]
[JsonExporterAttribute.Full]
public class CryptoTests : AsaDatabaseBenchmark
{
// The amount of padding to add to the object in bytes
// Default size is approx 530 bytes serialized
// Does not include SQL overhead
[Params(0)]
public int ObjectPadding { get; set; }

// The number of iterations per run
[Params(100000)]
public int N { get; set; }

static HashAlgorithm murmur128 = MurmurHash.Create128();
static HashAlgorithm sha256 = SHA256.Create();

static HashAlgorithm sha512 = SHA512.Create();

private ConcurrentQueue<byte[]> hashObjects = new ConcurrentQueue<byte[]>();

#nullable disable
public CryptoTests()
#nullable restore
{

}

[Benchmark (Baseline = true)]
public void Generate_N_SHA256_Hashes()
{
for (int i = 0; i < N; i++)
{
hashObjects.TryDequeue(out byte[]? result);
if (result is byte[])
{
_ = sha256.ComputeHash(result);
hashObjects.Enqueue(result);
}
else
{
Log.Information("The queue is polluted with nulls");
}
}
}

[Benchmark]
public void Generate_N_SHA512_Hashes()
{
for (int i = 0; i < N; i++)
{
hashObjects.TryDequeue(out byte[]? result);
if (result is byte[])
{
_ = sha512.ComputeHash(result);
hashObjects.Enqueue(result);
}
else
{
Log.Information("The queue is polluted with nulls");
}
}
}

[Benchmark]
public void Generate_N_Murmur_Hashes()
{
for (int i = 0; i < N; i++)
{
hashObjects.TryDequeue(out byte[]? result);
if (result is byte[])
{
_ = murmur128.ComputeHash(result);
hashObjects.Enqueue(result);
}
else
{
Log.Information("The queue is polluted with nulls");
}
}
}


[GlobalSetup]
public void GlobalSetup()
{
while (hashObjects.Count < N)
{
hashObjects.Enqueue(JsonSerializer.Serialize<FileSystemObject>(GetRandomObject(ObjectPadding)));
}
}
}
}
Loading

0 comments on commit e36c760

Please sign in to comment.