Skip to content

Commit c626d2f

Browse files
author
Jeff Treuting
committed
Benchmarking console app for configuration
Can test the times it takes to load up an InMemoryRepository based on the various ways. directly, custom repository, config file, custom repos w/ config file, StructureMap, etc. Based on these benchmark numbers did some performance tuning of the RepositoryFactory and loading from the configuration file, and got some good improvements (when used 250,000 times in a row it shows up as 850ms improvement
1 parent ec384c5 commit c626d2f

15 files changed

Lines changed: 554 additions & 132 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<section name="sharpRepository" type="SharpRepository.Repository.Configuration.SharpRepositorySection, SharpRepository.Repository"/>
5+
</configSections>
6+
7+
8+
<sharpRepository>
9+
<repositories default="inMemory">
10+
<repository name="inMemory" factory="SharpRepository.InMemoryRepository.InMemoryConfigRepositoryFactory, SharpRepository.InMemoryRepository"/>
11+
</repositories>
12+
</sharpRepository>
13+
14+
15+
<startup>
16+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
17+
</startup>
18+
</configuration>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using SharpRepository.Ioc.StructureMap;
2+
using StructureMap;
3+
4+
namespace SharpRepository.Benchmarks.Configuration
5+
{
6+
public static class Bootstrapper
7+
{
8+
public static void Run()
9+
{
10+
ObjectFactory.Initialize(x =>
11+
{
12+
x.Scan(scan =>
13+
{
14+
scan.TheCallingAssembly();
15+
scan.WithDefaultConventions();
16+
});
17+
18+
x.ForRepositoriesUseSharpRepository();
19+
});
20+
}
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SharpRepository.Benchmarks.Configuration.Models
2+
{
3+
public class User
4+
{
5+
public int UserId { get; set; }
6+
public string Email { get; set; }
7+
public string FullName { get; set; }
8+
}
9+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
using SharpRepository.Benchmarks.Configuration.Models;
7+
using SharpRepository.Benchmarks.Configuration.Repositories;
8+
using SharpRepository.InMemoryRepository;
9+
using SharpRepository.Repository;
10+
using SharpRepository.Repository.Configuration;
11+
using StructureMap;
12+
13+
namespace SharpRepository.Benchmarks.Configuration
14+
{
15+
class BenchmarkItem
16+
{
17+
public Action Test { get; set; }
18+
public string Title { get; set; }
19+
public int Order { get; set; }
20+
}
21+
22+
class Program
23+
{
24+
static Program()
25+
{
26+
Bootstrapper.Run();
27+
}
28+
29+
private const int Max = 250000;
30+
static void Main(string[] args)
31+
{
32+
var tests = new List<BenchmarkItem>()
33+
{
34+
new BenchmarkItem()
35+
{
36+
Title = "Direct Creation: [new InMemoryRepository()]",
37+
Test = DirectCreation,
38+
Order = 1
39+
},
40+
new BenchmarkItem()
41+
{
42+
Title = "Custom repo hardcoded: [UserRepository : InMemoryRepository<User,int>]",
43+
Test = CustomRepositoryHardCoded,
44+
Order = 2
45+
},
46+
new BenchmarkItem()
47+
{
48+
Title = "From config file: [RepositoryFactory.GetInstance<User, int>()]",
49+
Test = CreateFromConfigFile,
50+
Order = 3
51+
},
52+
new BenchmarkItem()
53+
{
54+
Title = "From config obj: [RepositoryFactory.GetInstance<User, int>(config)]",
55+
Test = CreateFromConfigObject,
56+
Order = 4
57+
},
58+
new BenchmarkItem()
59+
{
60+
Title = "Custom repo config: [UserRepository : ConfigurationBasedRepository<User, int>]",
61+
Test = CustomRepositoryFromConfig,
62+
Order = 5
63+
},
64+
new BenchmarkItem()
65+
{
66+
Title = "StructureMap w/ config: [ObjectFactory.GetInstance<IRepository<User, int>>()]",
67+
Test = DirectFromStructureMap,
68+
Order = 6
69+
}
70+
};
71+
72+
// run thru each of them once because otherwise the first loop is slower for some reason
73+
foreach (var item in tests)
74+
{
75+
item.Test();
76+
}
77+
78+
Console.WriteLine("Running each test {0:#,0} times", Max);
79+
Console.WriteLine("----------------------------------------------------");
80+
Console.WriteLine();
81+
82+
var sw = new Stopwatch();
83+
84+
foreach (var benchmarkItem in tests.AsQueryable().OrderBy(x => x.Order))
85+
{
86+
Console.WriteLine(benchmarkItem.Title);
87+
sw.Reset();
88+
sw.Start();
89+
90+
for (var i = 0; i < Max; i++)
91+
{
92+
benchmarkItem.Test();
93+
}
94+
sw.Stop();
95+
Console.WriteLine(" {0} ms total -- {1} avg ms per\n", sw.Elapsed.TotalMilliseconds, sw.Elapsed.TotalMilliseconds / Convert.ToDouble(Max));
96+
}
97+
98+
Console.WriteLine("\nDone: press any key to quit");
99+
Console.Read();
100+
}
101+
102+
[MethodImpl(MethodImplOptions.NoInlining)]
103+
static void DirectCreation()
104+
{
105+
new InMemoryRepository<User, int>();
106+
}
107+
108+
[MethodImpl(MethodImplOptions.NoInlining)]
109+
static void CreateFromConfigFile()
110+
{
111+
RepositoryFactory.GetInstance<User, int>();
112+
}
113+
114+
[MethodImpl(MethodImplOptions.NoInlining)]
115+
static void CustomRepositoryFromConfig()
116+
{
117+
new UserFromConfigRepository();
118+
}
119+
120+
[MethodImpl(MethodImplOptions.NoInlining)]
121+
static void CustomRepositoryHardCoded()
122+
{
123+
new UserRepository();
124+
}
125+
126+
[MethodImpl(MethodImplOptions.NoInlining)]
127+
static void DirectFromStructureMap()
128+
{
129+
ObjectFactory.GetInstance<IRepository<User, int>>();
130+
}
131+
132+
[MethodImpl(MethodImplOptions.NoInlining)]
133+
static void CreateFromConfigObject()
134+
{
135+
var config = new SharpRepositoryConfiguration();
136+
config.AddRepository(new InMemoryRepositoryConfiguration("default"));
137+
RepositoryFactory.GetInstance<User, int>(config);
138+
}
139+
}
140+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ConfigurationBenchmarks")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ConfigurationBenchmarks")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e54c602b-b9af-421c-99c1-0bf8173db829")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SharpRepository.Benchmarks.Configuration.Models;
2+
using SharpRepository.InMemoryRepository;
3+
using SharpRepository.Repository;
4+
5+
namespace SharpRepository.Benchmarks.Configuration.Repositories
6+
{
7+
public class UserFromConfigRepository : ConfigurationBasedRepository<User, int>, IRepository<User,int>
8+
{
9+
}
10+
11+
public class UserRepository : InMemoryRepository<User, int>, IRepository<User, int>
12+
{}
13+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{4C4F3977-7327-4BBF-90ED-0C61172A48D4}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SharpRepository.Benchmarks.Configuration</RootNamespace>
11+
<AssemblyName>SharpRepository.Benchmarks.Configuration</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="StructureMap">
36+
<HintPath>..\packages\structuremap.2.6.4.1\lib\net40\StructureMap.dll</HintPath>
37+
</Reference>
38+
<Reference Include="System" />
39+
<Reference Include="System.Core" />
40+
<Reference Include="System.Xml.Linq" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="Microsoft.CSharp" />
43+
<Reference Include="System.Data" />
44+
<Reference Include="System.Xml" />
45+
</ItemGroup>
46+
<ItemGroup>
47+
<Compile Include="Bootstrapper.cs" />
48+
<Compile Include="Models\User.cs" />
49+
<Compile Include="Program.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="Repositories\UserRepository.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="App.config" />
55+
<None Include="packages.config" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<ProjectReference Include="..\SharpRepository.InMemoryRepository\SharpRepository.InMemoryRepository.csproj">
59+
<Project>{13ad3fca-4c9d-4674-b786-f30843638d3b}</Project>
60+
<Name>SharpRepository.InMemoryRepository</Name>
61+
</ProjectReference>
62+
<ProjectReference Include="..\SharpRepository.Ioc.StructureMap\SharpRepository.Ioc.StructureMap.csproj">
63+
<Project>{0b4bdc6d-6030-48fd-b22a-f9a9a7a00812}</Project>
64+
<Name>SharpRepository.Ioc.StructureMap</Name>
65+
</ProjectReference>
66+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
67+
<Project>{710dee79-25ce-4f68-b8b1-d08a135ad154}</Project>
68+
<Name>SharpRepository.Repository</Name>
69+
</ProjectReference>
70+
</ItemGroup>
71+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
72+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
73+
Other similar extension points exist, see Microsoft.Common.targets.
74+
<Target Name="BeforeBuild">
75+
</Target>
76+
<Target Name="AfterBuild">
77+
</Target>
78+
-->
79+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="structuremap" version="2.6.4.1" targetFramework="net45" />
4+
</packages>

0 commit comments

Comments
 (0)