-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use cached delegates to set property values rather than SetValue (#170)
* Use cached delegates to set property values rather than SetValue * Minor refactoring. Added previous code comments.
- Loading branch information
Showing
4 changed files
with
160 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Attributes.Exporters; | ||
using Bogus; | ||
|
||
namespace Benchmark | ||
{ | ||
[MarkdownExporter, MemoryDiagnoser] | ||
public class BenchGenerate | ||
{ | ||
public class Project | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
} | ||
|
||
private static Faker<Project> FakerDefault { get; set; } | ||
private static Faker<Project> FakerCustom { get; set; } | ||
private static Faker<Project> FakerWithRules { get; set; } | ||
private static Faker<Project> FakerWithRulesComplex { get; set; } | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
FakerDefault = new Faker<Project>().UseSeed(1337); | ||
FakerCustom = new Faker<Project>() | ||
.CustomInstantiator(f=> new Project()) | ||
.UseSeed(1337); | ||
FakerWithRules = new Faker<Project>() | ||
.CustomInstantiator(f=> new Project()) | ||
.RuleFor(p=>p.Id, f => f.IndexGlobal) | ||
.UseSeed(1337); | ||
FakerWithRulesComplex = new Faker<Project>() | ||
.CustomInstantiator(f=> new Project()) | ||
.RuleFor(p=>p.Id, f => f.IndexGlobal) | ||
.RuleFor(p => p.Name, f => f.Person.Company.Name + f.UniqueIndex.ToString()) | ||
.RuleFor(p => p.Description, f => f.Lorem.Paragraphs(3)) | ||
.UseSeed(1337); | ||
} | ||
|
||
// [Benchmark] | ||
public void Generate_Default() | ||
{ | ||
var projects = FakerDefault.Generate(10_000).ToList(); | ||
} | ||
|
||
// [Benchmark] | ||
public void Generate_CustomInstantiator() | ||
{ | ||
var projects = FakerDefault.Generate(10_000).ToList(); | ||
} | ||
|
||
[Benchmark] | ||
public void Generate_WithRules() | ||
{ | ||
var projects = FakerWithRules.Generate(10_000).ToList(); | ||
} | ||
|
||
//[Benchmark] | ||
public void Generate_WithRulesComplex() | ||
{ | ||
var projects = FakerWithRulesComplex.Generate(10_000).ToList(); | ||
} | ||
|
||
// [Benchmark] | ||
public void Constructor() | ||
{ | ||
var projects = Enumerable.Range(0, 10_000).Select(i => new Project {Id = i}).ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Bogus.Extensions | ||
{ | ||
public static class ExtensionsForPropertyInfo | ||
{ | ||
private static readonly MethodInfo GenericSetterCreationMethod = | ||
typeof(ExtensionsForPropertyInfo).GetMethod(nameof(CreateSetterGeneric), BindingFlags.Static | BindingFlags.NonPublic); | ||
|
||
public static Action<T, object> CreateSetter<T>(this PropertyInfo property) | ||
{ | ||
if (property == null) throw new ArgumentNullException(nameof(property)); | ||
|
||
var setter = property.GetSetMethod(); | ||
if (setter == null) throw new ArgumentException("The specified property does not have a public setter."); | ||
|
||
var genericHelper = GenericSetterCreationMethod.MakeGenericMethod(property.DeclaringType, property.PropertyType); | ||
return (Action<T, object>)genericHelper.Invoke(null, new object[] { setter }); | ||
} | ||
|
||
private static Action<T, object> CreateSetterGeneric<T, V>(MethodInfo setter) where T : class | ||
{ | ||
var setterTypedDelegate = | ||
#if STANDARD | ||
(Action<T, V>) setter.CreateDelegate(typeof(Action<T, V>)) | ||
#else | ||
(Action<T, V>) Delegate.CreateDelegate(typeof(Action<T, V>), setter) | ||
#endif | ||
; | ||
var setterDelegate = (Action<T, object>)((T instance, object value) => { setterTypedDelegate(instance, (V)value); }); | ||
return setterDelegate; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters