Skip to content
Permalink
Browse files

Add benchmark for index model building

  • Loading branch information
Turnerj committed Oct 18, 2019
1 parent 6245834 commit f5bdff3aadfb9631b071f7d57278751df8cff889
@@ -29,6 +29,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{BA1176
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoFramework.Benchmarks", "tests\MongoFramework.Benchmarks\MongoFramework.Benchmarks.csproj", "{0177C18B-96AB-45E1-B9FB-1D734B2B7504}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -47,6 +49,10 @@ Global
{A8438535-7E88-4578-B47A-D1A016E43416}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8438535-7E88-4578-B47A-D1A016E43416}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8438535-7E88-4578-B47A-D1A016E43416}.Release|Any CPU.Build.0 = Release|Any CPU
{0177C18B-96AB-45E1-B9FB-1D734B2B7504}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0177C18B-96AB-45E1-B9FB-1D734B2B7504}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0177C18B-96AB-45E1-B9FB-1D734B2B7504}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0177C18B-96AB-45E1-B9FB-1D734B2B7504}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -55,6 +61,7 @@ Global
{D871CD75-CC1E-4482-934F-42E74B2BF255} = {D007577A-7BDB-4658-BCB6-7CA96908058F}
{FAAAAE78-22D5-42EB-824F-4B8CEB109023} = {9CBF5A6D-EC65-4289-B2C9-875BDB654FC4}
{A8438535-7E88-4578-B47A-D1A016E43416} = {D007577A-7BDB-4658-BCB6-7CA96908058F}
{0177C18B-96AB-45E1-B9FB-1D734B2B7504} = {9CBF5A6D-EC65-4289-B2C9-875BDB654FC4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A202BFC6-FE6A-4ED0-AD06-90C7BAB29E28}
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BenchmarkDotNet.Attributes;
using MongoFramework.Attributes;
using MongoFramework.Infrastructure.Indexing;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Benchmarks.Infrastructure.Indexing
{
[CoreJob, MemoryDiagnoser]
public class IndexModelBuilderBenchmark
{
public class FlatIndexModel
{
[Index(IndexSortOrder.Ascending)]
public string NoNameIndex { get; set; }
[Index("MyCustomIndexName", IndexSortOrder.Ascending)]
public string NamedIndex { get; set; }

[Index("MyCompoundIndex", IndexSortOrder.Ascending, IndexPriority = 1)]
public string FirstPriority { get; set; }

[Index("MyCompoundIndex", IndexSortOrder.Ascending, IndexPriority = 3)]
public string ThirdPriority { get; set; }

[Index("MyCompoundIndex", IndexSortOrder.Ascending, IndexPriority = 2)]
public string SecondPriority { get; set; }
}

public class NestedIndexParentModel
{
[Index(IndexSortOrder.Ascending)]
public string NoNameIndex { get; set; }
[Index("MyCustomIndexName", IndexSortOrder.Ascending)]
public string NamedIndex { get; set; }
public IEnumerable<NestedIndexChildModel> ChildEnumerable { get; set; }
public NestedIndexChildModel[] ChildArray { get; set; }
public List<NestedIndexChildModel> ChildList { get; set; }
}
public class NestedIndexChildModel
{
[Index(IndexSortOrder.Ascending)]
public string ChildId { get; set; }
}

[GlobalSetup]
public void Setup()
{
EntityMapping.RemoveAllDefinitions();
MongoDbDriverHelper.ResetDriver();
EntityMapping.RegisterType(typeof(FlatIndexModel));
EntityMapping.RegisterType(typeof(NestedIndexParentModel));
}

[Benchmark]
public void FlatModel()
{
IndexModelBuilder<FlatIndexModel>.BuildModel().ToArray();
}

[Benchmark]
public void NestedModel()
{
IndexModelBuilder<NestedIndexParentModel>.BuildModel().ToArray();
}
}
}
@@ -0,0 +1,54 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;

namespace MongoFramework.Benchmarks
{
public static class MongoDbDriverHelper
{
public static void ResetDriver()
{
//Primarily introduced to better test TypeDiscoverySerializer, this is designed to reset the MongoDB driver
//as if the assembly just loaded. It is likely incomplete and would be easily subject to breaking in future
//driver updates. If someone knows a better way to reset the MongoDB driver, please open a pull request!

var classMapField = typeof(BsonClassMap).GetField("__classMaps", BindingFlags.NonPublic | BindingFlags.Static);
if (classMapField.GetValue(null) is Dictionary<Type, BsonClassMap> classMaps)
{
classMaps.Clear();
}

var knownTypesField = typeof(BsonSerializer).GetField("__typesWithRegisteredKnownTypes", BindingFlags.NonPublic | BindingFlags.Static);
if (knownTypesField.GetValue(null) is HashSet<Type> knownTypes)
{
knownTypes.Clear();
}

var discriminatorTypesField = typeof(BsonSerializer).GetField("__discriminatedTypes", BindingFlags.NonPublic | BindingFlags.Static);
if (discriminatorTypesField.GetValue(null) is HashSet<Type> discriminatorTypes)
{
discriminatorTypes.Clear();
}

var discriminatorsField = typeof(BsonSerializer).GetField("__discriminators", BindingFlags.NonPublic | BindingFlags.Static);
if (discriminatorsField.GetValue(null) is Dictionary<BsonValue, HashSet<Type>> discriminators)
{
discriminators.Clear();
}

var serializerRegistryField = typeof(BsonSerializer).GetField("__serializerRegistry", BindingFlags.NonPublic | BindingFlags.Static);
if (serializerRegistryField.GetValue(null) is BsonSerializerRegistry registry)
{
var cacheField = typeof(BsonSerializerRegistry).GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance);
var registryCache = cacheField.GetValue(registry) as ConcurrentDictionary<Type, IBsonSerializer>;
registryCache.Clear();
}
}
}
}
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>False</IsPackable>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\..\src\MongoFramework\MongoFramework.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="docs\" />
</ItemGroup>

</Project>
@@ -0,0 +1,13 @@
using System;
using BenchmarkDotNet.Running;

namespace MongoFramework.Benchmarks
{
class Program
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
}

0 comments on commit f5bdff3

Please sign in to comment.
You can’t perform that action at this time.