Created
January 31, 2018 13:00
-
-
Save Daniel-Svensson/c0dabcb6c4dd8ccab06dedb9923c81a6 to your computer and use it in GitHub Desktop.
EntitySet benchmarks
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Attributes.Jobs; | |
using Cities; | |
using OpenRiaServices.DomainServices.Client; | |
using OpenRiaServices.DomainServices.Client.Test; | |
namespace ConsoleApplication1 | |
{ | |
[MemoryDiagnoser] | |
[ShortRunJob] | |
public class EntitySetBenchmarks | |
{ | |
private EntitySet<City> _entitySet; | |
private List<City> _cities; | |
[Params(10, 500, 5000)] | |
public int NumEntities { get; set; } = 500; | |
//[IterationSetup] | |
public void Setup() | |
{ | |
_entitySet = CreateEntitySet<City>(); | |
_cities = CreateCities(NumEntities).ToList(); | |
} | |
[Benchmark] | |
public void None() | |
{ | |
Setup(); | |
} | |
[Benchmark] | |
public void Add() | |
{ | |
Setup(); | |
foreach (var city in GetCities()) | |
{ | |
_entitySet.Add(city); | |
} | |
} | |
[Benchmark] | |
public void Attach() | |
{ | |
Setup(); | |
foreach (var city in GetCities()) | |
{ | |
_entitySet.Attach(city); | |
} | |
} | |
[Benchmark] | |
public void AddAndModify() | |
{ | |
Add(); | |
foreach (var entity in _entitySet.ToList()) | |
{ | |
entity.Name = "changed"; | |
} | |
} | |
[Benchmark] | |
public void AddAndRemove() | |
{ | |
Add(); | |
foreach (var entity in _entitySet.ToList()) | |
{ | |
_entitySet.Remove(entity); | |
} | |
} | |
[Benchmark] | |
public void AddAndDetach() | |
{ | |
Add(); | |
foreach (var entity in _entitySet.ToList()) | |
{ | |
_entitySet.Detach(entity); | |
} | |
} | |
private EntitySet<T> CreateEntitySet<T>() where T : Entity | |
{ | |
return this.CreateEntitySet<T>(EntitySetOperations.All); | |
} | |
private EntitySet<T> CreateEntitySet<T>(EntitySetOperations operations) where T : Entity | |
{ | |
DynamicEntityContainer container = new DynamicEntityContainer(); | |
return container.AddEntitySet<T>(operations); | |
} | |
private IEnumerable<City> GetCities() => _cities; | |
private static IEnumerable<City> CreateCities(int num) | |
{ | |
for (var i = 0; i < num; i++) | |
{ | |
yield return new City { Name = "" + i, CountyName = "c", StateName = "s" }; | |
} | |
} | |
/// <summary> | |
/// An dynamic EntityContainer class that allows external configuration of | |
/// EntitySets for testing purposes. | |
/// </summary> | |
public class DynamicEntityContainer : EntityContainer | |
{ | |
public EntitySet<T> AddEntitySet<T>(EntitySetOperations operations) where T : Entity | |
{ | |
base.CreateEntitySet<T>(operations); | |
return GetEntitySet<T>(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment