-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a046cfb
commit 3d57334
Showing
16 changed files
with
948 additions
and
235 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
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,91 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using AutoMapper; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Engines; | ||
using BenchmarkDotNet.Jobs; | ||
using Microsoft.IO; | ||
|
||
namespace Initialize.Benchmarks; | ||
|
||
[SimpleJob(runStrategy: RunStrategy.Throughput, launchCount: 1, invocationCount: 1, iterationCount:10, | ||
runtimeMoniker: RuntimeMoniker.Net70)] | ||
[MemoryDiagnoser] | ||
[HideColumns(Column.StdDev, Column.Median, Column.Error, Column.RatioSD)] | ||
public class ChampionChallengerEnumerable | ||
{ | ||
private static readonly RecyclableMemoryStreamManager manager = new(); | ||
private Mapper _autoMapper; | ||
private List<Test> _testObjects; | ||
private IEnumerable<Test> _testEnumerable; | ||
|
||
public ChampionChallengerEnumerable() | ||
{ | ||
//Initialize mapper | ||
var config = new MapperConfiguration(cfg => | ||
cfg.CreateMap<Test, Test2>()); | ||
_autoMapper = new Mapper(config); | ||
|
||
//Initialize mapper | ||
Mapper<Test, Test2>.Map(testObj); | ||
|
||
_testObjects = Enumerable.Range(0, 10000).Select(r => new Test() { PropString = r.ToString(), Prop = r }).ToList(); | ||
} | ||
[Params(100_000, 1_000_000)] | ||
public int Iterations { get; set; } | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
_testEnumerable = Enumerable.Range(0, Iterations) | ||
.Select(r => new Test() { PropString = r.ToString(), Prop = r }); | ||
|
||
_testObjects = _testEnumerable.ToList(); | ||
} | ||
|
||
private Test testObj = new Test() | ||
{ | ||
Prop = 1, | ||
PropString = "a", | ||
}; | ||
[Benchmark(Description = "Mapper", Baseline = true)] | ||
public void Mapper() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = Mapper<Test, Test2>.MapEnumerable(_testObjects); | ||
|
||
Debug.Assert(result.Count() == _testObjects.Count); | ||
} | ||
|
||
[Benchmark(Description = "AutoMapper")] | ||
public void AutoMapper() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = _autoMapper.Map<List<Test>>(_testObjects); | ||
|
||
Debug.Assert(result.Count() == _testObjects.Count); | ||
} | ||
[Benchmark(Description = "MapperEnumerable")] | ||
public void MapperEnumerable() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = Mapper<Test, Test2>.MapEnumerable(_testEnumerable); | ||
|
||
Debug.Assert(result.Count() == _testObjects.Count); | ||
} | ||
|
||
[Benchmark(Description = "AutoMapperEnumerable")] | ||
public void AutoMapperEnumerable() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = _autoMapper.Map<List<Test>>(_testEnumerable); | ||
|
||
Debug.Assert(result.Count() == _testObjects.Count); | ||
} | ||
} |
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,135 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using MapperA = AutoMapper.Mapper; | ||
using AutoMapper; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Engines; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Order; | ||
using Microsoft.IO; | ||
using Microsoft.Diagnostics.Runtime.Utilities; | ||
using System.Buffers; | ||
|
||
namespace Initialize.Benchmarks; | ||
|
||
[SimpleJob(runStrategy: RunStrategy.Throughput, launchCount: 1, invocationCount: 1, | ||
runtimeMoniker: RuntimeMoniker.Net70)] | ||
[Orderer(SummaryOrderPolicy.FastestToSlowest)] | ||
[MemoryDiagnoser] | ||
[HideColumns(Column.StdDev, Column.Median)] | ||
public class ExperimentBenchmarks | ||
{ | ||
private static readonly RecyclableMemoryStreamManager manager = new(); | ||
private MapperA _autoMapper; | ||
private IEnumerable<Test> _testEnumerable; | ||
|
||
public ExperimentBenchmarks() | ||
{ | ||
//Initialize mapper | ||
var config = new MapperConfiguration(cfg => | ||
cfg.CreateMap<Test, Test2>().ForMember(x=> | ||
x.PropString, | ||
x=>x.MapFrom(x=>x.PropString))); | ||
_autoMapper = new MapperA(config); | ||
|
||
//Initialize mapper | ||
Mapper<Test, Test2>.Map(testObj); | ||
|
||
} | ||
[Params(100_000, 1_000_000)] | ||
public int Iterations { get; set; } | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
_testEnumerable = Enumerable.Range(0, Iterations) | ||
.Select(r => new Test() { PropString = r.ToString(), Prop = r }); | ||
|
||
} | ||
private Test testObj = new Test() | ||
{ | ||
Prop = 1, | ||
PropString = "a", | ||
}; | ||
|
||
//[Benchmark(Description = "InitializeMapperColdStart")] | ||
//public void InitializeMapperColdStart() | ||
//{ | ||
// var test2 = new Test2(); | ||
|
||
// Mapper<Test, Test2>.Map(testObj, test2); | ||
//} | ||
|
||
//[Benchmark(Description = "AutoMapperColdStart")] | ||
//public void AutoMapperUnInitialized() | ||
//{ | ||
// //Initialize the mapper | ||
// var config = new MapperConfiguration(cfg => | ||
// cfg.CreateMap<Test, Test2>()); | ||
|
||
// var mapper = new MapperA(config); | ||
|
||
// var test2 = new Test2(); | ||
|
||
// mapper.Map(testObj, test2); | ||
//} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void MapArray() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = Mapper<Test, Test2>.MapArray(_testEnumerable); | ||
|
||
Debug.Assert(result.Count() == _testEnumerable.Count()); | ||
} | ||
//[Benchmark] | ||
//public void Span() | ||
//{ | ||
// var test2 = new Test2(); | ||
// int cnt = 0, current = 0; | ||
// var span = _testObjects.ToArray().AsSpan(); | ||
// Span<Test2> spanTo = new Test2[span.Length]; | ||
|
||
// for (int i = 0; i < cnt; i++) | ||
// spanTo[i] = Mapper<Test, Test2>.Map(span[i]); | ||
|
||
|
||
// Debug.Assert(spanTo.ToArray().Count() == _testObjects.Count()); | ||
//} | ||
[Benchmark] | ||
public void MapInline() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = Mapper<Test, Test2>.MapArrayInline(_testEnumerable); | ||
|
||
Debug.Assert(result.Count() == _testEnumerable.Count()); | ||
} | ||
[Benchmark] | ||
public void MapOptimized() | ||
{ | ||
var test2 = new Test2(); | ||
|
||
var result = Mapper<Test, Test2>.MapArrayOpt(_testEnumerable); | ||
|
||
Debug.Assert(result.Count() == _testEnumerable.Count()); | ||
} | ||
|
||
[Benchmark] | ||
public void ArrayPool() | ||
{ | ||
var test2 = new Test2(); | ||
int cnt = 0, current = 0; | ||
var span = _testEnumerable.ToArray(); | ||
Test2[] spanTo = ArrayPool<Test2>.Shared.Rent(span.Length); | ||
|
||
for (int i = 0; i < cnt; i++) | ||
spanTo[i] = Mapper<Test, Test2>.Map(span[i]); | ||
|
||
ArrayPool<Test2>.Shared.Return(spanTo); | ||
Debug.Assert(spanTo.Count() == _testEnumerable.Count()); | ||
} | ||
} |
Oops, something went wrong.