|
| 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 | +} |
0 commit comments