Last active
November 3, 2016 09:04
-
-
Save gaevoy/428f57fbbb8a977e7507ed4862d4684e to your computer and use it in GitHub Desktop.
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
[TestFixture] | |
public class MongoDriverAsyncSyncTests | |
{ | |
private readonly IMongoCollection<SimpleDto> _collection; | |
private readonly SimpleDto[] _dtosToInsert; | |
public MongoDriverAsyncSyncTests() | |
{ | |
string connectionString = "mongodb://localhost/MongoDriverAsyncSyncTests"; | |
string databaseName = new MongoUrl(connectionString).DatabaseName; | |
_collection = new MongoClient(connectionString).GetDatabase(databaseName).GetCollection<SimpleDto>("SimpleDtos"); | |
_dtosToInsert = Enumerable.Range(0, 50000).Select(i => new SimpleDto { Id = Guid.NewGuid(), Value = i }).ToArray(); | |
} | |
[Test] | |
public void SyncVersion() | |
{ | |
// Warm up | |
for (int i = 0; i < 100; i++) | |
_collection.InsertOne(new SimpleDto { Id = Guid.NewGuid() }); | |
_collection.DeleteMany(new BsonDocument()); // delete all | |
// Measure | |
var duration = Stopwatch.StartNew(); | |
foreach (var dto in _dtosToInsert) | |
_collection.InsertOne(dto); | |
duration.Stop(); | |
Console.WriteLine("Sync: " + duration.Elapsed); | |
} | |
[Test] | |
public async Task AsyncVersion() | |
{ | |
// Warm up | |
for (int i = 0; i < 100; i++) | |
await _collection.InsertOneAsync(new SimpleDto { Id = Guid.NewGuid() }).ConfigureAwait(false); | |
_collection.DeleteMany(new BsonDocument()); // delete all | |
// Measure | |
var duration = Stopwatch.StartNew(); | |
foreach (var dto in _dtosToInsert) | |
await _collection.InsertOneAsync(dto).ConfigureAwait(false); | |
duration.Stop(); | |
Console.WriteLine("Async: " + duration.Elapsed); | |
} | |
public class SimpleDto | |
{ | |
public Guid Id { get; set; } | |
public int Value { get; set; } | |
} | |
} | |
[Config(typeof(Config))] | |
public class MongoDriverAsyncSyncPerf | |
{ | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
Add(new MemoryDiagnoser()); | |
Add(MarkdownExporter.GitHub); | |
Add(Job.Default.WithLaunchCount(1).WithWarmupCount(2).WithTargetCount(20)); | |
} | |
} | |
private readonly IMongoCollection<SimpleDto> _collection; | |
public MongoDriverAsyncSyncPerf() | |
{ | |
string connectionString = "mongodb://192.168.1.11/MongoDriverAsyncSyncTests"; | |
string databaseName = new MongoUrl(connectionString).DatabaseName; | |
_collection = new MongoClient(connectionString).GetDatabase(databaseName).GetCollection<SimpleDto>("SimplesPerf"); | |
_collection.DeleteMany(new BsonDocument()); | |
} | |
[Benchmark(Baseline = true)] | |
public void InsertOne() => _collection.InsertOne(new SimpleDto()); | |
[Benchmark] | |
public void InsertOne_100() | |
{ | |
for (int i = 0; i < 100; i++) | |
_collection.InsertOne(new SimpleDto()); | |
} | |
[Benchmark] | |
public async Task InsertOneAsync() => await _collection.InsertOneAsync(new SimpleDto()).ConfigureAwait(false); | |
[Benchmark] | |
public async Task InsertOneAsync_100() | |
{ | |
for (int i = 0; i < 100; i++) | |
await _collection.InsertOneAsync(new SimpleDto()).ConfigureAwait(false); | |
} | |
[Test] | |
public void Run() | |
{ | |
Directory.SetCurrentDirectory(Path.GetDirectoryName(typeof(MongoDriverAsyncSyncPerf).Assembly.Location)); | |
var summary = BenchmarkRunner.Run<MongoDriverAsyncSyncPerf>(); | |
Console.WriteLine(summary); | |
} | |
public class SimpleDto | |
{ | |
public BsonObjectId Id { get; set; } | |
} | |
} |
Host Process Environment Information:
BenchmarkDotNet.Core=v0.9.9.0
OS=Microsoft Windows NT 6.3.9600.0
Processor=Intel(R) Core(TM) i7-5500U CPU 2.40GHz, ProcessorCount=4
Frequency=2338342 ticks, Resolution=427.6534 ns, Timer=TSC
CLR=MS.NET 4.0.30319.42000, Arch=64-bit ? [RyuJIT]
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1055.0
Type=MongoDriverAsyncSyncPerf Mode=Throughput LaunchCount=1
WarmupCount=2 TargetCount=20
Method | Median | StdDev | Scaled | Scaled-SD | Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
---|---|---|---|---|---|---|---|---|
InsertOne | 462.2316 us | 59.5674 us | 1.00 | 0.00 | 70.00 | - | - | 5 519,71 |
InsertOneAsync | 577.4854 us | 132.3485 us | 1.23 | 0.30 | 152.80 | - | - | 10 695,80 |
Remote Mongo 3:
Sync: 00:00:23.6225098
Async: 00:00:26.5399851
Host Process Environment Information:
BenchmarkDotNet.Core=v0.9.9.0
OS=Microsoft Windows NT 6.3.9600.0
Processor=Intel(R) Core(TM) i7-5500U CPU 2.40GHz, ProcessorCount=4
Frequency=2338342 ticks, Resolution=427.6534 ns, Timer=TSC
CLR=MS.NET 4.0.30319.42000, Arch=64-bit ? [RyuJIT]
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1055.0
Type=MongoDriverAsyncSyncPerf Mode=Throughput LaunchCount=1
WarmupCount=2 TargetCount=20
Method | Median | StdDev | Scaled | Scaled-SD | Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
---|---|---|---|---|---|---|---|---|
InsertOne | 433.0183 us | 14.8393 us | 1.00 | 0.00 | 1.12 | - | - | 5 437,03 |
InsertOne_100 | 45,051.4189 us | 1,810.8001 us | 103.60 | 5.26 | 98.70 | - | - | 438 581,32 |
InsertOneAsync | 491.7714 us | 51.5949 us | 1.17 | 0.12 | 2.48 | - | - | 10 708,15 |
InsertOneAsync_100 | 45,672.9597 us | 1,077.7901 us | 106.13 | 4.19 | 283.00 | - | - | 1 262 490,18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Local Mongo:
Sync: 00:00:08.6252968
Async: 00:00:12.1327196
Remote Mongo:
Sync: 00:00:21.6069787
Async: 00:00:21.8636328