-
-
Save rstam/19a104f0f6fd21fe118d 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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization; | |
using MongoDB.Driver; | |
using MongoDB.Driver.Builders; | |
namespace BenchmarkFindv1x | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
try | |
{ | |
var client = new MongoClient("mongodb://localhost"); | |
var server = client.GetServer(); | |
server.Connect(); | |
Console.WriteLine("Running tests against server version: {0} using driver version 1.x.", server.BuildInfo.Version); | |
Console.WriteLine(); | |
var database = server.GetDatabase("test"); | |
var collection = database.GetCollection("test"); | |
RunBenchmarks(collection, 1, 1, 1); | |
Console.WriteLine(); | |
var iterations = 1000; | |
foreach (var documentSize in new[] { 1, 100, 1000 }) | |
{ | |
Console.WriteLine("Benchmarking documents of size: {0}", documentSize); | |
foreach (var numberOfDocuments in new[] { 1, 10, 100, 1000 }) | |
{ | |
RunBenchmarks(collection, iterations, numberOfDocuments, documentSize); | |
} | |
Console.WriteLine(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Unhandled exception:"); | |
Console.WriteLine(ex); | |
} | |
Console.WriteLine("Press Enter to continue"); | |
Console.ReadLine(); | |
} | |
private static void RunBenchmarks(MongoCollection<BsonDocument> collection, int iterations, int numberOfDocuments, int documentSize) | |
{ | |
CreateData(collection, numberOfDocuments, documentSize); | |
RunBenchmark(collection, iterations, numberOfDocuments); | |
} | |
private static void CreateData(MongoCollection<BsonDocument> collection, int numberOfDocuments, int documentSize) | |
{ | |
collection.Drop(); | |
var documentWithNoFiller = new BsonDocument{ { "_id", 0 }, { "filler", "" } }; | |
var documentWithNoFillerSize = documentWithNoFiller.ToBson().Length; | |
var fillerSize = Math.Max(0, documentSize - documentWithNoFillerSize); | |
for (var id = 0; id < numberOfDocuments; id++) | |
{ | |
var document = new BsonDocument | |
{ | |
{ "_id", id }, | |
{ "filler", new string('x', fillerSize )} | |
}; | |
collection.Insert(document); | |
} | |
} | |
private static void RunBenchmark(MongoCollection<BsonDocument> collection, int iterations, int numberOfDocuments) | |
{ | |
var document = collection.FindOne(); | |
var bson = document.ToBson(); | |
var totalNumberOfDocumentsRead = 0; | |
var stopwatch = Stopwatch.StartNew(); | |
for (var i = 0; i < iterations; i++) | |
{ | |
//var query = Query.LT("_id", numberOfDocuments); | |
//var cursor = collection.Find(query); | |
//var count = cursor.ToList().Count; | |
// totalNumberOfDocumentsRead += count; | |
for (var n = 0; n < numberOfDocuments; n++) | |
{ | |
document = BsonSerializer.Deserialize<BsonDocument>(bson); | |
totalNumberOfDocumentsRead += 1; | |
} | |
} | |
stopwatch.Stop(); | |
var elapsedSeconds = stopwatch.Elapsed.TotalSeconds; | |
var documentsPerSecond = totalNumberOfDocumentsRead / elapsedSeconds; | |
Console.WriteLine("Read {0} documents in {1} seconds, {2} documents/second", totalNumberOfDocumentsRead, elapsedSeconds, documentsPerSecond); | |
} | |
} | |
} |
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.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization.Serializers; | |
using MongoDB.Driver; | |
using MongoDB.Driver.Builders; | |
namespace BenchmarkFindv1x | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
try | |
{ | |
MainAsync().GetAwaiter().GetResult(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Unhandled exception:"); | |
Console.WriteLine(ex); | |
} | |
Console.WriteLine("Press Enter to continue"); | |
Console.ReadLine(); | |
} | |
public static async Task MainAsync() | |
{ | |
var client = new MongoClient("mongodb://localhost"); | |
var serverVersion = await GetServerVersionAsync(client); | |
Console.WriteLine("Running tests against server version: {0} using driver version 2.0 async API.", serverVersion); | |
Console.WriteLine(); | |
var database = client.GetDatabase("test"); | |
var collection = database.GetCollection<BsonDocument>("test"); | |
await RunBenchmarksAsync(database, collection, 1, 1, 1); | |
Console.WriteLine(); | |
var iterations = 1000; | |
foreach (var documentSize in new[] { 1, 100, 1000 }) | |
{ | |
Console.WriteLine("Benchmarking documents of size: {0}", documentSize); | |
foreach (var numberOfDocuments in new[] { 1, 10, 100, 1000 }) | |
{ | |
await RunBenchmarksAsync(database, collection, iterations, numberOfDocuments, documentSize); | |
} | |
Console.WriteLine(); | |
} | |
} | |
private static async Task<Version> GetServerVersionAsync(MongoClient client) | |
{ | |
var database = client.GetDatabase("admin"); | |
var command = new BsonDocument("buildInfo", 1); | |
var buildInfo = await database.RunCommandAsync<BsonDocument>(command); | |
return new Version(buildInfo["version"].AsString); | |
} | |
private static async Task RunBenchmarksAsync(IMongoDatabase database, IMongoCollection<BsonDocument> collection, int iterations, int numberOfDocuments, int documentSize) | |
{ | |
await CreateDataAsync(database, collection, numberOfDocuments, documentSize); | |
await RunBenchmarkAsync(collection, iterations, numberOfDocuments); | |
} | |
private static async Task CreateDataAsync(IMongoDatabase database, IMongoCollection<BsonDocument> collection, int numberOfDocuments, int documentSize) | |
{ | |
await database.DropAsync(); | |
var documentWithNoFiller = new BsonDocument { { "_id", 0 }, { "filler", "" } }; | |
var documentWithNoFillerSize = documentWithNoFiller.ToBson().Length; | |
var fillerSize = Math.Max(0, documentSize - documentWithNoFillerSize); | |
for (var id = 0; id < numberOfDocuments; id++) | |
{ | |
var document = new BsonDocument | |
{ | |
{ "_id", id }, | |
{ "filler", new string('x', fillerSize )} | |
}; | |
await collection.InsertOneAsync(new InsertOneModel<BsonDocument>(document)); | |
} | |
} | |
private static async Task RunBenchmarkAsync(IMongoCollection<BsonDocument> collection, int iterations, int numberOfDocuments) | |
{ | |
var totalNumberOfDocumentsRead = 0; | |
var stopwatch = Stopwatch.StartNew(); | |
for (var i = 0; i < iterations; i++) | |
{ | |
var query = Query.LT("_id", numberOfDocuments); | |
var cursor = await collection.FindAsync(new FindModel<BsonDocument> { Criteria = query, ResultSerializer = BsonDocumentSerializer.Instance }); | |
while (await cursor.MoveNextAsync()) | |
{ | |
var batch = cursor.Current; | |
foreach (var document in batch) | |
{ | |
totalNumberOfDocumentsRead += 1; | |
} | |
} | |
} | |
stopwatch.Stop(); | |
var elapsedSeconds = stopwatch.Elapsed.TotalSeconds; | |
var documentsPerSecond = totalNumberOfDocumentsRead / elapsedSeconds; | |
Console.WriteLine("Read {0} documents in {1} seconds, {2} documents/second", totalNumberOfDocumentsRead, elapsedSeconds, documentsPerSecond); | |
} | |
} | |
} |
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.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization; | |
using MongoDB.Driver; | |
using MongoDB.Driver.Builders; | |
namespace BenchmarkFindv1x | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
try | |
{ | |
var client = new MongoClient("mongodb://localhost"); | |
var server = client.GetServer(); | |
server.Connect(); | |
Console.WriteLine("Running tests against server version: {0} using driver version 2.0 legacy API.", server.BuildInfo.Version); | |
Console.WriteLine(); | |
var database = server.GetDatabase("test"); | |
var collection = database.GetCollection("test"); | |
RunBenchmarks(collection, 1, 1, 1); | |
Console.WriteLine(); | |
var iterations = 1000; | |
foreach (var documentSize in new[] { 1, 100, 1000 }) | |
{ | |
Console.WriteLine("Benchmarking documents of size: {0}", documentSize); | |
foreach (var numberOfDocuments in new[] { 1, 10, 100, 1000 }) | |
{ | |
RunBenchmarks(collection, iterations, numberOfDocuments, documentSize); | |
} | |
Console.WriteLine(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Unhandled exception:"); | |
Console.WriteLine(ex); | |
} | |
Console.WriteLine("Press Enter to continue"); | |
Console.ReadLine(); | |
} | |
private static void RunBenchmarks(MongoCollection<BsonDocument> collection, int iterations, int numberOfDocuments, int documentSize) | |
{ | |
CreateData(collection, numberOfDocuments, documentSize); | |
RunBenchmark(collection, iterations, numberOfDocuments); | |
} | |
private static void CreateData(MongoCollection<BsonDocument> collection, int numberOfDocuments, int documentSize) | |
{ | |
collection.Drop(); | |
var documentWithNoFiller = new BsonDocument { { "_id", 0 }, { "filler", "" } }; | |
var documentWithNoFillerSize = documentWithNoFiller.ToBson().Length; | |
var fillerSize = Math.Max(0, documentSize - documentWithNoFillerSize); | |
for (var id = 0; id < numberOfDocuments; id++) | |
{ | |
var document = new BsonDocument | |
{ | |
{ "_id", id }, | |
{ "filler", new string('x', fillerSize )} | |
}; | |
collection.Insert(document); | |
} | |
} | |
private static void RunBenchmark(MongoCollection<BsonDocument> collection, int iterations, int numberOfDocuments) | |
{ | |
var document = collection.FindOne(); | |
var bson = document.ToBson(); | |
var totalNumberOfDocumentsRead = 0; | |
var stopwatch = Stopwatch.StartNew(); | |
for (var i = 0; i < iterations; i++) | |
{ | |
//var query = Query.LT("_id", numberOfDocuments); | |
//var cursor = collection.Find(query); | |
//var count = cursor.ToList().Count; | |
//totalNumberOfDocumentsRead += count; | |
for (var n = 0; n < numberOfDocuments; n++) | |
{ | |
document = BsonSerializer.Deserialize<BsonDocument>(bson); | |
totalNumberOfDocumentsRead += 1; | |
} | |
} | |
stopwatch.Stop(); | |
var elapsedSeconds = stopwatch.Elapsed.TotalSeconds; | |
var documentsPerSecond = totalNumberOfDocumentsRead / elapsedSeconds; | |
Console.WriteLine("Read {0} documents in {1} seconds, {2} documents/second", totalNumberOfDocumentsRead, elapsedSeconds, documentsPerSecond); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment