Skip to content

Commit

Permalink
Added a benchmarkdotnet project and added a CloneProviderBenchmark cl…
Browse files Browse the repository at this point in the history
…ass to it which can be run from the commandline
  • Loading branch information
Barsonax committed Jun 24, 2020
1 parent abe9e13 commit f5d4811
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 32 deletions.
34 changes: 34 additions & 0 deletions Benchmarks/DualityBenchmarks/CloneProviderBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using BenchmarkDotNet.Attributes;
using Duality.Cloning;
using Duality.Tests.Serialization;

namespace DualityBenchmarks
{
public class CloneProviderBenchmarks
{
[Benchmark]
public void CloneTestObjectGraph()
{
Random rnd = new Random(0);
TestObject data = new TestObject(rnd, 5);
TestObject[] results = new TestObject[200];

for (int i = 0; i < results.Length; i++)
{
results[i] = data.DeepClone();
}
}

[Benchmark(Baseline = true)]
public void CreateWithoutClone()
{
Random rnd = new Random(0);
TestObject[] results = new TestObject[200];
for (int i = 0; i < results.Length; i++)
{
results[i] = new TestObject(rnd, 5);
}
}
}
}
17 changes: 17 additions & 0 deletions Benchmarks/DualityBenchmarks/DualityBenchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\Core\Duality\Duality.csproj" />
<ProjectReference Include="..\..\Test\Core\DualityTests.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions Benchmarks/DualityBenchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using BenchmarkDotNet.Running;

namespace DualityBenchmarks
{
class Program
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
}
7 changes: 7 additions & 0 deletions Duality.sln
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DualityPhysics", "Source\Co
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DualityTemplates", "Source\DualityTemplates\DualityTemplates.csproj", "{3AB26D57-70D9-40DE-B12D-6B1489035805}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DualityBenchmarks", "Benchmarks\DualityBenchmarks\DualityBenchmarks.csproj", "{20985018-3635-478D-BA4F-6AF1E278C0AB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -260,6 +262,10 @@ Global
{3AB26D57-70D9-40DE-B12D-6B1489035805}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AB26D57-70D9-40DE-B12D-6B1489035805}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AB26D57-70D9-40DE-B12D-6B1489035805}.Release|Any CPU.Build.0 = Release|Any CPU
{20985018-3635-478D-BA4F-6AF1E278C0AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20985018-3635-478D-BA4F-6AF1E278C0AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20985018-3635-478D-BA4F-6AF1E278C0AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20985018-3635-478D-BA4F-6AF1E278C0AB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -305,6 +311,7 @@ Global
{D220795D-8A59-432F-A8EC-CE9143DD07D5} = {9D215950-8E34-4070-914F-7B7D8A54ED6C}
{165D83B8-EAE8-4CDC-9003-E595D4225B8F} = {9D215950-8E34-4070-914F-7B7D8A54ED6C}
{5CA66347-C3DA-47DA-B07B-E2B89E6E712A} = {FC08D0D6-612E-4AD2-950C-8E9BA895D5D1}
{20985018-3635-478D-BA4F-6AF1E278C0AB} = {FC08D0D6-612E-4AD2-950C-8E9BA895D5D1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {74E2A280-F2F3-400D-9CC2-7A91D6D3D15C}
Expand Down
32 changes: 0 additions & 32 deletions Test/Core/Cloning/CloneProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,37 +511,5 @@ [Test] public void PartialCloning()
provider.ClearCachedMapping();
Assert.DoesNotThrow(() => provider.CloneObject(targetStatic3, true));
}
[Test] public void PerformanceTest()
{
Random rnd = new Random(0);
TestObject data = new TestObject(rnd, 5);
TestObject[] results = new TestObject[200];

GC.Collect();

var watch = new System.Diagnostics.Stopwatch();
watch.Start();
for (int i = 0; i < results.Length; i++)
{
results[i] = data.DeepClone();
}
watch.Stop();
TestHelper.LogNumericTestResult(this, "CloneTestObjectGraph", watch.Elapsed.TotalMilliseconds, "ms");

GC.Collect();

var watch2 = new System.Diagnostics.Stopwatch();
watch2.Start();
for (int i = 0; i < results.Length; i++)
{
results[i] = new TestObject(rnd, 5);
}
watch2.Stop();
TestHelper.LogNumericTestResult(this, "CreateWithoutClone", watch2.Elapsed.TotalMilliseconds, "ms");
TestHelper.LogNumericTestResult(this, "CloneVersusRaw", watch.Elapsed.TotalMilliseconds / watch2.Elapsed.TotalMilliseconds, null);

GC.Collect();
Assert.Pass();
}
}
}

0 comments on commit f5d4811

Please sign in to comment.