Skip to content

Instantly share code, notes, and snippets.

@gaevoy
Last active November 3, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaevoy/428f57fbbb8a977e7507ed4862d4684e to your computer and use it in GitHub Desktop.
Save gaevoy/428f57fbbb8a977e7507ed4862d4684e to your computer and use it in GitHub Desktop.
[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; }
}
}
@gaevoy
Copy link
Author

gaevoy commented Nov 2, 2016

Local Mongo:
Sync: 00:00:08.6252968
Async: 00:00:12.1327196

Remote Mongo:
Sync: 00:00:21.6069787
Async: 00:00:21.8636328

@gaevoy
Copy link
Author

gaevoy commented Nov 2, 2016


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  
MethodMedianStdDevScaledScaled-SDGen 0Gen 1Gen 2Bytes Allocated/Op
InsertOne462.2316 us59.5674 us1.000.0070.00-- 5 519,71
InsertOneAsync577.4854 us132.3485 us1.230.30152.80-- 10 695,80

@gaevoy
Copy link
Author

gaevoy commented Nov 3, 2016

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 StdDevScaledScaled-SDGen 0Gen 1Gen 2Bytes Allocated/Op
InsertOne433.0183 us14.8393 us1.000.001.12-- 5 437,03
InsertOne_10045,051.4189 us1,810.8001 us103.605.2698.70-- 438 581,32
InsertOneAsync491.7714 us51.5949 us1.170.122.48-- 10 708,15
InsertOneAsync_10045,672.9597 us1,077.7901 us106.134.19283.00--1 262 490,18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment