diff --git a/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs b/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs index 57e4e0542e1..6cb66186ac1 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs @@ -21,74 +21,73 @@ using MongoDB.Bson; using MongoDB.Driver; -namespace MongoDB.Benchmarks +namespace MongoDB.Benchmarks; + +public static class BenchmarkHelper { - public static class BenchmarkHelper - { - public const string DataFolderPath = "../../../../../../../data/"; + public const string DataFolderPath = "../../../../../../../data/"; - public static void AddFilesToQueue(ConcurrentQueue<(string, int)> filesQueue, string directoryPath, string fileNamePrefix, int fileCount) + public static void AddFilesToQueue(ConcurrentQueue<(string, int)> filesQueue, string directoryPath, string fileNamePrefix, int fileCount) + { + var addingLDJSONfiles = fileNamePrefix == "ldjson"; + for (int i = 0; i < fileCount; i++) { - var addingLDJSONfiles = fileNamePrefix == "ldjson"; - for (int i = 0; i < fileCount; i++) - { - var fileName = addingLDJSONfiles ? $"{fileNamePrefix}{i:D3}.txt" : $"{fileNamePrefix}{i:D2}.txt"; - filesQueue.Enqueue(($"{directoryPath}/{fileName}", i)); // enqueue complete filepath and filenumber - } + var fileName = addingLDJSONfiles ? $"{fileNamePrefix}{i:D3}.txt" : $"{fileNamePrefix}{i:D2}.txt"; + filesQueue.Enqueue(($"{directoryPath}/{fileName}", i)); // enqueue complete filepath and filenumber } + } - public static double CalculateCompositeScore(IEnumerable benchmarkResults, string benchmarkCategory) - { - var identifiedBenchmarksScores = benchmarkResults - .Where(benchmark => benchmark.Categories.Contains(benchmarkCategory)) - .Select(benchmark => benchmark.Score).ToArray(); - - if (identifiedBenchmarksScores.Any()) - { - return identifiedBenchmarksScores.Average(); - } - - return 0; - } + public static double CalculateCompositeScore(IEnumerable benchmarkResults, string benchmarkCategory) + { + var identifiedBenchmarksScores = benchmarkResults + .Where(benchmark => benchmark.Categories.Contains(benchmarkCategory)) + .Select(benchmark => benchmark.Score).ToArray(); - public static void CreateEmptyDirectory(string path) + if (identifiedBenchmarksScores.Any()) { - if (Directory.Exists(path)) - { - Directory.Delete(path, true); - } - Directory.CreateDirectory(path); + return identifiedBenchmarksScores.Average(); } - public static BsonDocument ReadExtendedJson(string resourcePath) - { - var extendedJson = File.ReadAllText(DataFolderPath + resourcePath); - return BsonDocument.Parse(extendedJson); - } + return 0; + } - public static byte[] ReadExtendedJsonToBytes(string resourcePath) + public static void CreateEmptyDirectory(string path) + { + if (Directory.Exists(path)) { - var extendedJson = File.ReadAllText(DataFolderPath + resourcePath); - var document = BsonDocument.Parse(extendedJson); - return document.ToBson(); + Directory.Delete(path, true); } + Directory.CreateDirectory(path); + } - public static class MongoConfiguration - { - public const string PerfTestDatabaseName = "perftest"; - public const string PerfTestCollectionName = "corpus"; + public static BsonDocument ReadExtendedJson(string resourcePath) + { + var extendedJson = File.ReadAllText(DataFolderPath + resourcePath); + return BsonDocument.Parse(extendedJson); + } - public static IMongoClient CreateClient() - { - var mongoUri = Environment.GetEnvironmentVariable("MONGODB_URI"); - var settings = mongoUri != null ? MongoClientSettings.FromConnectionString(mongoUri) : new(); - settings.ClusterSource = DisposingClusterSource.Instance; + public static byte[] ReadExtendedJsonToBytes(string resourcePath) + { + var extendedJson = File.ReadAllText(DataFolderPath + resourcePath); + var document = BsonDocument.Parse(extendedJson); + return document.ToBson(); + } + + public static class MongoConfiguration + { + public const string PerfTestDatabaseName = "perftest"; + public const string PerfTestCollectionName = "corpus"; + + public static IMongoClient CreateClient() + { + var mongoUri = Environment.GetEnvironmentVariable("MONGODB_URI"); + var settings = mongoUri != null ? MongoClientSettings.FromConnectionString(mongoUri) : new(); + settings.ClusterSource = DisposingClusterSource.Instance; - var client = new MongoClient(settings); - client.DropDatabase(PerfTestDatabaseName); + var client = new MongoClient(settings); + client.DropDatabase(PerfTestDatabaseName); - return client; - } + return client; } } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkResult.cs b/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkResult.cs index 8eefeeef684..78d367e269d 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkResult.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkResult.cs @@ -17,37 +17,36 @@ using BenchmarkDotNet.Reports; using MongoDB.Benchmarks.Bson; -namespace MongoDB.Benchmarks +namespace MongoDB.Benchmarks; + +public sealed class BenchmarkResult { - public sealed class BenchmarkResult + public HashSet Categories { get; } + public string Name { get; } + public double Score { get; } + + public BenchmarkResult(BenchmarkReport benchmarkReport) { - public HashSet Categories { get; } - public string Name { get; } - public double Score { get; } + Categories = new HashSet(benchmarkReport.BenchmarkCase.Descriptor.Categories); - public BenchmarkResult(BenchmarkReport benchmarkReport) + int dataSetSize; + if (Categories.Contains(DriverBenchmarkCategory.BsonBench)) { - Categories = new HashSet(benchmarkReport.BenchmarkCase.Descriptor.Categories); - - int dataSetSize; - if (Categories.Contains(DriverBenchmarkCategory.BsonBench)) - { - var bsonBenchmarkData = (BsonBenchmarkData)benchmarkReport.BenchmarkCase.Parameters["BenchmarkData"]; - Name = bsonBenchmarkData.DataSetName + benchmarkReport.BenchmarkCase.Descriptor.Type.Name; - dataSetSize = bsonBenchmarkData.DataSetSize; - } - else - { - Name = Categories.Contains(DriverBenchmarkCategory.BulkWriteBench) - ? benchmarkReport.BenchmarkCase.Descriptor.WorkloadMethod.Name - : benchmarkReport.BenchmarkCase.Descriptor.Type.Name; - - dataSetSize = (int)benchmarkReport.BenchmarkCase.Parameters["BenchmarkDataSetSize"]; - } + var bsonBenchmarkData = (BsonBenchmarkData)benchmarkReport.BenchmarkCase.Parameters["BenchmarkData"]; + Name = bsonBenchmarkData.DataSetName + benchmarkReport.BenchmarkCase.Descriptor.Type.Name; + dataSetSize = bsonBenchmarkData.DataSetSize; + } + else + { + Name = Categories.Contains(DriverBenchmarkCategory.BulkWriteBench) + ? benchmarkReport.BenchmarkCase.Descriptor.WorkloadMethod.Name + : benchmarkReport.BenchmarkCase.Descriptor.Type.Name; - // change the median from nanoseconds to seconds for calculating the score. - // since dataSetSize is in bytes, divide the score to convert to MB/s - Score = (dataSetSize / (benchmarkReport.ResultStatistics.Median / 1_000_000_000D)) / 1_000_000D; + dataSetSize = (int)benchmarkReport.BenchmarkCase.Parameters["BenchmarkDataSetSize"]; } + + // change the median from nanoseconds to seconds for calculating the score. + // since dataSetSize is in bytes, divide the score to convert to MB/s + Score = (dataSetSize / (benchmarkReport.ResultStatistics.Median / 1_000_000_000D)) / 1_000_000D; } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkRunner.cs b/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkRunner.cs index 0a4a3f95036..04a2c66c268 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkRunner.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/BenchmarkRunner.cs @@ -19,47 +19,46 @@ using BenchmarkDotNet.Running; using MongoDB.Benchmarks.Exporters; -namespace MongoDB.Benchmarks +namespace MongoDB.Benchmarks; + +public class BenchmarkRunner { - public class BenchmarkRunner + public static int Main(string[] args) { - public static int Main(string[] args) - { - var rootCommand = new RootCommand("CSharp Driver benchmarks runner"); - rootCommand.TreatUnmatchedTokensAsErrors = false; - var evergreenOption = new Option("--evergreen", () => false); - rootCommand.AddOption(evergreenOption); - var driverBenchmarksOption = new Option("--driverBenchmarks", () => false); - rootCommand.AddOption(driverBenchmarksOption); - var evergreenOutputFileOption = new Option(["--o", "--output-file"], () => "evergreen-results.json"); - rootCommand.AddOption(evergreenOutputFileOption); + var rootCommand = new RootCommand("CSharp Driver benchmarks runner"); + rootCommand.TreatUnmatchedTokensAsErrors = false; + var evergreenOption = new Option("--evergreen", () => false); + rootCommand.AddOption(evergreenOption); + var driverBenchmarksOption = new Option("--driverBenchmarks", () => false); + rootCommand.AddOption(driverBenchmarksOption); + var evergreenOutputFileOption = new Option(["--o", "--output-file"], () => "evergreen-results.json"); + rootCommand.AddOption(evergreenOutputFileOption); - rootCommand.SetHandler(invocationContext => - { - var evergreenValue = invocationContext.ParseResult.GetValueForOption(evergreenOption); - var driverBenchmarksValue = invocationContext.ParseResult.GetValueForOption(driverBenchmarksOption); - var evergreenOutputFileValue = invocationContext.ParseResult.GetValueForOption(evergreenOutputFileOption); + rootCommand.SetHandler(invocationContext => + { + var evergreenValue = invocationContext.ParseResult.GetValueForOption(evergreenOption); + var driverBenchmarksValue = invocationContext.ParseResult.GetValueForOption(driverBenchmarksOption); + var evergreenOutputFileValue = invocationContext.ParseResult.GetValueForOption(evergreenOutputFileOption); - var config = DefaultConfig.Instance; + var config = DefaultConfig.Instance; - // use a modified config if running driver benchmarks - if (driverBenchmarksValue) - { - config = config - .WithOption(ConfigOptions.JoinSummary, true) - .AddExporter(new LocalExporter()) - .HideColumns("BenchmarkDataSetSize"); - } + // use a modified config if running driver benchmarks + if (driverBenchmarksValue) + { + config = config + .WithOption(ConfigOptions.JoinSummary, true) + .AddExporter(new LocalExporter()) + .HideColumns("BenchmarkDataSetSize"); + } - if (evergreenValue) - { - config = config.AddExporter(new EvergreenExporter(evergreenOutputFileValue)); - } + if (evergreenValue) + { + config = config.AddExporter(new EvergreenExporter(evergreenOutputFileValue)); + } - BenchmarkSwitcher.FromAssembly(typeof(BenchmarkRunner).Assembly).Run(invocationContext.ParseResult.UnmatchedTokens.ToArray(), config); - }); + BenchmarkSwitcher.FromAssembly(typeof(BenchmarkRunner).Assembly).Run(invocationContext.ParseResult.UnmatchedTokens.ToArray(), config); + }); - return rootCommand.Invoke(args); - } + return rootCommand.Invoke(args); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkData.cs b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkData.cs index ea5793b00be..28176e7e3a9 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkData.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkData.cs @@ -1,22 +1,23 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -namespace MongoDB.Benchmarks.Bson +using System; + +namespace MongoDB.Benchmarks.Bson; + +public readonly record struct BsonBenchmarkData(string FilePath, string DataSetName, int DataSetSize, Type PocoType) { - public readonly record struct BsonBenchmarkData(string FilePath, string DataSetName, int DataSetSize) - { - public override string ToString() => $"{DataSetName}"; - } + public override string ToString() => $"{DataSetName}"; } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkDataTypes.cs b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkDataTypes.cs new file mode 100644 index 00000000000..ff8f5db7586 --- /dev/null +++ b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkDataTypes.cs @@ -0,0 +1,331 @@ +/* Copyright 2010-present MongoDB Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace MongoDB.Benchmarks.Bson; + +public sealed class FlatPoco +{ + [BsonId] + public ObjectId Id { get; set; } + + // Boolean fields + public bool IsorvnMR { get; set; } + public bool vvUeXASH { get; set; } + public bool HicJbMpj { get; set; } + public bool pKjOghFa { get; set; } + public bool VcCSqSmp { get; set; } + public bool JhImQOkw { get; set; } + public bool MNuWZMLP { get; set; } + public bool WHSQVLKG { get; set; } + public bool wjfyueDC { get; set; } + public bool FpduyhQP { get; set; } + public bool zDzSGNnW { get; set; } + public bool UpdMADoN { get; set; } + public bool XGxlHrXf { get; set; } + public bool zEgGhhZf { get; set; } + public bool RemSsnnR { get; set; } + public bool yeTUgNrU { get; set; } + public bool LVNIFCYm { get; set; } + public bool aicoMxZq { get; set; } + public bool TRpgnInA { get; set; } + public bool WmMOvgFc { get; set; } + public bool qrJASGzU { get; set; } + public bool egxZaSsw { get; set; } + public bool kfvcFmKw { get; set; } + public bool sGWJTAcT { get; set; } + + // Int32 fields + public int XeRkAyCq { get; set; } + public int SUWXijHT { get; set; } + public int ijwXMKqI { get; set; } + public int ddVenEkK { get; set; } + public int MXMxLVBk { get; set; } + public int VVvwKVRG { get; set; } + public int UKwbAKGw { get; set; } + public int MqfkBZJF { get; set; } + public int VplFgewF { get; set; } + public int nBKWWUWk { get; set; } + public int Rbxpznea { get; set; } + public int dCLfYqqM { get; set; } + public int nKhiSITP { get; set; } + public int ddPdLgGg { get; set; } + public int AgYYbYPr { get; set; } + public int pOMEwSod { get; set; } + public int BwTXiovJ { get; set; } + public int KyxOoCqS { get; set; } + public int GiAHzFII { get; set; } + public int DJsnHZIC { get; set; } + public int PvfnpsMV { get; set; } + public int qHzOMXeT { get; set; } + public int omnwvBbA { get; set; } + public int CFujXoob { get; set; } + + // Double fields + public double pfZSRHnn { get; set; } + public double taoNnQYY { get; set; } + public double TgSwBbgp { get; set; } + public double yKfZnGKG { get; set; } + public double XxvXmHiQ { get; set; } + public double jWaFvVAz { get; set; } + public double obcwwqWZ { get; set; } + public double HrUPbFHD { get; set; } + public double XEBqaXkB { get; set; } + public double LngvlnTV { get; set; } + public double gySFZeAE { get; set; } + public double JzgaUWVG { get; set; } + public double iwfbMdcv { get; set; } + public double hwHOTmmW { get; set; } + public double dVkWIafN { get; set; } + public double HQiykral { get; set; } + public double pacTBmxE { get; set; } + public double JXMyYkfb { get; set; } + public double ReOZakjB { get; set; } + public double mlfZVfVT { get; set; } + public double CDIGOuIZ { get; set; } + public double PjKiuWnQ { get; set; } + public double pQyCJaEd { get; set; } + public double HQeCoswW { get; set; } + + // Int64 fields + public long FDYGeSiR { get; set; } + public long TmUnYUrv { get; set; } + public long VCSKFCoE { get; set; } + public long AtWNZJXa { get; set; } + public long wmDLUkXt { get; set; } + public long zSYvADVf { get; set; } + public long doshbrpF { get; set; } + public long zswQbWEI { get; set; } + public long XXKbyIXG { get; set; } + public long xWpeGNjl { get; set; } + public long KMKBtlov { get; set; } + public long cVjWCrlu { get; set; } + public long ahFCBmqT { get; set; } + public long TkXMwZlU { get; set; } + public long bkuaZWRT { get; set; } + public long DQBQcQFj { get; set; } + public long sYtnozSc { get; set; } + public long UtbwOKLt { get; set; } + public long SqNvlUZF { get; set; } + public long HVHyetUM { get; set; } + public long AgSNVyBb { get; set; } + public long reiKnuza { get; set; } + public long xZOksssj { get; set; } + public long AMQrGQmu { get; set; } + + // String fields + public string oRWMNJTE { get; set; } + public string hnVgYIQi { get; set; } + public string cxOHMeDJ { get; set; } + public string MeUYSkPS { get; set; } + public string McpOBmaR { get; set; } + public string CqCssWxW { get; set; } + public string vkEDWgmN { get; set; } + public string xrzGnsEK { get; set; } + public string OCsIhHxq { get; set; } + public string iFFGfTXc { get; set; } + public string RwAVVKHM { get; set; } + public string jmglLvAS { get; set; } + public string ZmtEJFSO { get; set; } + public string eRTIdIJR { get; set; } + public string vlSZaxCV { get; set; } + public string QobifTeZ { get; set; } + public string dNSuxlSU { get; set; } + public string WYJdGJLu { get; set; } + public string RPsQhgRD { get; set; } + public string rmzUAgmk { get; set; } + public string Ibrdrtgg { get; set; } + public string KnhgtAOJ { get; set; } + public string OfTmCvDx { get; set; } + public string JrJzKiIx { get; set; } + public string SYtZkQbC { get; set; } + public string tIJEYSYM { get; set; } + public string dHsYhRbV { get; set; } + public string LUPqMOHS { get; set; } + public string CYhSCkWB { get; set; } + public string vSLTtfDF { get; set; } + public string CEtYKsdd { get; set; } + public string YDHWnEXV { get; set; } + public string xWUlYggc { get; set; } + public string dpbwfSRb { get; set; } + public string fEheUtop { get; set; } + public string TDUzNJiH { get; set; } + public string zMCFzcWY { get; set; } + public string yXSBbPeT { get; set; } + public string wjAWaOog { get; set; } + public string cepcgozk { get; set; } + public string BBqZInWV { get; set; } + public string dtywOLeD { get; set; } + public string WoFGfdvb { get; set; } + public string uMDWqLMf { get; set; } + public string jbUymqiB { get; set; } + public string VtzeOlCT { get; set; } + public string gErhgZTh { get; set; } + public string pPtPsgRl { get; set; } +} + +public sealed class FullPoco +{ + [BsonId] + public ObjectId Id { get; set; } + + // Int32 fields + public int KpnXZaDQ { get; set; } + public int nyYyaFrV { get; set; } + public int NAKOhrML { get; set; } + public int SQIAGqNE { get; set; } + public int bRaWfHwf { get; set; } + public int RFhPJzgh { get; set; } + + // Binary fields + [BsonRepresentation(BsonType.Binary)] + public byte[] BOQAeydE { get; set; } + + [BsonRepresentation(BsonType.Binary)] + public byte[] kqKTGXUm { get; set; } + + [BsonRepresentation(BsonType.Binary)] + public byte[] hAcOFtfN { get; set; } + + [BsonRepresentation(BsonType.Binary)] + public byte[] SHvLWKjg { get; set; } + + [BsonRepresentation(BsonType.Binary)] + public byte[] vSvdWAnJ { get; set; } + + [BsonRepresentation(BsonType.Binary)] + public byte[] DQuSYbZR { get; set; } + + // Int64 fields + public long kVDldkCH { get; set; } + public long iRVlyXVm { get; set; } + public long CAWdqfmI { get; set; } + public long qPDeTZWq { get; set; } + public long dSMWIFLD { get; set; } + public long GqcHZeLf { get; set; } + public long jMeFYftg { get; set; } + public long JEVgGziE { get; set; } + public long rclBQefx { get; set; } + public long EAYSerbF { get; set; } + public long TtYjxGJH { get; set; } + public long juiYRtal { get; set; } + + // Empty document fields + public BsonDocument olUSRZtj { get; set; } + public BsonDocument hhVNsfZZ { get; set; } + public BsonDocument iEoENLRz { get; set; } + public BsonDocument caPGDEGj { get; set; } + + // MaxKey fields + public BsonMaxKey ayrwiTMT { get; set; } + public BsonMaxKey kFaSNVoS { get; set; } + public BsonMaxKey LCHLPrcd { get; set; } + public BsonMaxKey uhmyCSEv { get; set; } + public BsonMaxKey uSbYKmdX { get; set; } + public BsonMaxKey IsYFQBkP { get; set; } + + // Boolean fields + public bool zlVZQePF { get; set; } + public bool DfzEYsYW { get; set; } + public bool OagyDZLm { get; set; } + public bool RBrgnptQ { get; set; } + public bool wShiBppY { get; set; } + public bool mUXXRWFX { get; set; } + public bool WqNFzMgH { get; set; } + public bool yPKwWWxb { get; set; } + public bool fvLXcTQB { get; set; } + public bool kQhDjXIO { get; set; } + public bool WboSdRaB { get; set; } + public bool hjSPiBiC { get; set; } + + // MinKey fields + public BsonMinKey VFKRjaPW { get; set; } + public BsonMinKey EuKkhmHw { get; set; } + public BsonMinKey wKCwNLSh { get; set; } + public BsonMinKey BGAqpNkE { get; set; } + public BsonMinKey zqpnKCOL { get; set; } + public BsonMinKey QdkSZoFx { get; set; } + + // DateTime fields + public DateTime pDeYcUIu { get; set; } + public DateTime buEAkIge { get; set; } + public DateTime uNvbuffj { get; set; } + public DateTime PGiZAWYN { get; set; } + public DateTime SQOAGVaT { get; set; } + public DateTime IJOIvDcw { get; set; } + + // JavaScript Code with Scope fields + public BsonJavaScriptWithScope giVmVwzU { get; set; } + public BsonJavaScriptWithScope xoLWHvAD { get; set; } + public BsonJavaScriptWithScope MsFvjFIM { get; set; } + public BsonJavaScriptWithScope UuDsAOGk { get; set; } + public BsonJavaScriptWithScope kcVXzXHn { get; set; } + public BsonJavaScriptWithScope lyWwkZGg { get; set; } + public BsonJavaScriptWithScope iNbJUZuj { get; set; } + public BsonJavaScriptWithScope ARdpUnvr { get; set; } + public BsonJavaScriptWithScope yltfrVvf { get; set; } + public BsonJavaScriptWithScope LQygaTou { get; set; } + public BsonJavaScriptWithScope AZbAtRCC { get; set; } + public BsonJavaScriptWithScope duzmrLJI { get; set; } + + // Timestamp fields + public BsonTimestamp geaenijV { get; set; } + public BsonTimestamp xCNVQDhK { get; set; } + public BsonTimestamp ZmCYgzpS { get; set; } + public BsonTimestamp fWBoraHq { get; set; } + public BsonTimestamp duTaTKGF { get; set; } + public BsonTimestamp LNpbbRfA { get; set; } + + // String fields + public string KUVCyPFv { get; set; } + public string ebfJeJwS { get; set; } + public string tuTaYuPG { get; set; } + public string JQbxXYUE { get; set; } + public string JlhVrQmD { get; set; } + public string nLsRKLJQ { get; set; } + + // Regular Expression fields + public BsonRegularExpression ZZbaMgQp { get; set; } + public BsonRegularExpression BZogAEUM { get; set; } + + // Array fields + public int[] NzsNfcyY { get; set; } + public int[] VIJGlGfX { get; set; } + public int[] bxrxTsho { get; set; } + public int[] dNVHpBIF { get; set; } + public int[] hUhWCZbY { get; set; } + public int[] UwZlNrHv { get; set; } +} + +public sealed class DeepPocoRoot +{ + public DeepPocoNode right { get; set; } + public DeepPocoNode left { get; set; } +} + +public sealed class DeepPocoNode +{ + public object right { get; set; } + public object left { get; set; } + + public DeepPocoNode GetRightNode() => right as DeepPocoNode; + public DeepPocoNode GetLeftNode() => left as DeepPocoNode; + + public string GetRightValue() => right as string; + public string GetLeftValue() => left as string; +} diff --git a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonDecodingBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonDecodingBenchmark.cs index 3a9186a31a6..9495675f233 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonDecodingBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonDecodingBenchmark.cs @@ -21,51 +21,65 @@ using MongoDB.Bson.Serialization.Serializers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.Bson +namespace MongoDB.Benchmarks.Bson; + +[IterationTime(3000)] +[BenchmarkCategory(DriverBenchmarkCategory.BsonBench)] +public class BsonDecodingBenchmark { - [IterationTime(3000)] - [BenchmarkCategory(DriverBenchmarkCategory.BsonBench)] - public class BsonDecodingBenchmark - { - private byte[] _bytes; - private BsonDeserializationContext _context; - private BsonBinaryReader _reader; - private MemoryStream _stream; + private const int Iterations = 10_000; - [ParamsSource(nameof(BenchmarkDataSources))] - public BsonBenchmarkData BenchmarkData { get; set; } + private byte[] _bytes; + private BsonDeserializationContext _context; + private BsonBinaryReader _reader; + private MemoryStream _stream; + private IBsonSerializer _pocoSerializer; - [GlobalSetup] - public void Setup() - { - _bytes = ReadExtendedJsonToBytes(BenchmarkData.FilePath); - _stream = new MemoryStream(_bytes); - _reader = new BsonBinaryReader(_stream); - _context = BsonDeserializationContext.CreateRoot(_reader); - } + [ParamsSource(nameof(BenchmarkDataSources))] + public BsonBenchmarkData BenchmarkData { get; set; } + + [GlobalSetup] + public void Setup() + { + _bytes = ReadExtendedJsonToBytes(BenchmarkData.FilePath); + _stream = new MemoryStream(_bytes); + _reader = new BsonBinaryReader(_stream); + _context = BsonDeserializationContext.CreateRoot(_reader); + _pocoSerializer = BsonSerializer.LookupSerializer(BenchmarkData.PocoType); + } - [Benchmark] - public void BsonDecoding() + [Benchmark] + public void BsonDecoding() + { + for (int i = 0; i < Iterations; i++) { - for (int i = 0; i < 10000; i++) - { - BsonDocumentSerializer.Instance.Deserialize(_context); - _stream.Position = 0; - } + _ = BsonDocumentSerializer.Instance.Deserialize(_context); + _stream.Position = 0; } + } - [GlobalCleanup] - public void Cleanup() + [Benchmark] + public void BsonDecodingPoco() + { + var args = new BsonDeserializationArgs() { NominalType = BenchmarkData.PocoType }; + for (int i = 0; i < Iterations; i++) { - _reader.Dispose(); - _stream.Dispose(); + _ = _pocoSerializer.Deserialize(_context, args); + _stream.Position = 0; } + } - public IEnumerable BenchmarkDataSources() => new BsonBenchmarkData[] - { - new("extended_bson/flat_bson.json", "Flat", 75_310_000), - new("extended_bson/full_bson.json", "Full", 57_340_000), - new("extended_bson/deep_bson.json", "Deep", 19_640_000) - }; + [GlobalCleanup] + public void Cleanup() + { + _reader.Dispose(); + _stream.Dispose(); } + + public IEnumerable BenchmarkDataSources() => + [ + new("extended_bson/flat_bson.json", "Flat", 75_310_000, typeof(FlatPoco)), + new("extended_bson/full_bson.json", "Full", 57_340_000, typeof(FullPoco)), + new("extended_bson/deep_bson.json", "Deep", 19_640_000, typeof(DeepPocoRoot)) + ]; } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonEncodingBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonEncodingBenchmark.cs index 1cf7ec08e15..c0f6cba087e 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonEncodingBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonEncodingBenchmark.cs @@ -22,51 +22,66 @@ using MongoDB.Bson.Serialization.Serializers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.Bson +namespace MongoDB.Benchmarks.Bson; + +[IterationTime(3000)] +[BenchmarkCategory(DriverBenchmarkCategory.BsonBench)] +public class BsonEncodingBenchmark { - [IterationTime(3000)] - [BenchmarkCategory(DriverBenchmarkCategory.BsonBench)] - public class BsonEncodingBenchmark - { - private BsonSerializationContext _context; - private BsonDocument _document; - private MemoryStream _stream; - private BsonBinaryWriter _writer; + private const int Iterations = 10_000; - [ParamsSource(nameof(BenchmarkDataSources))] - public BsonBenchmarkData BenchmarkData { get; set; } + private BsonSerializationContext _context; + private BsonDocument _document; + private object _documentPoco; + private MemoryStream _stream; + private BsonBinaryWriter _writer; + private IBsonSerializer _pocoSerializer; - [GlobalSetup] - public void Setup() - { - _stream = new MemoryStream(); - _writer = new BsonBinaryWriter(_stream); - _context = BsonSerializationContext.CreateRoot(_writer); - _document = ReadExtendedJson(BenchmarkData.FilePath); - } + [ParamsSource(nameof(BenchmarkDataSources))] + public BsonBenchmarkData BenchmarkData { get; set; } + + [GlobalSetup] + public void Setup() + { + _stream = new MemoryStream(); + _writer = new BsonBinaryWriter(_stream); + _context = BsonSerializationContext.CreateRoot(_writer); + _document = ReadExtendedJson(BenchmarkData.FilePath); + _documentPoco = BsonSerializer.Deserialize(_document, BenchmarkData.PocoType); + _pocoSerializer = BsonSerializer.LookupSerializer(BenchmarkData.PocoType); + } - [Benchmark] - public void BsonEncoding() + [Benchmark] + public void BsonEncoding() + { + for (int i = 0; i < Iterations; i++) { - for (int i = 0; i < 10000; i++) - { - BsonDocumentSerializer.Instance.Serialize(_context, _document); - _stream.Position = 0; - } + BsonDocumentSerializer.Instance.Serialize(_context, _document); + _stream.Position = 0; } + } - [GlobalCleanup] - public void Cleanup() + [Benchmark] + public void BsonEncodingPoco() + { + for (int i = 0; i < Iterations; i++) { - _writer.Dispose(); - _stream.Dispose(); + _pocoSerializer.Serialize(_context, _documentPoco); + _stream.Position = 0; } + } - public IEnumerable BenchmarkDataSources() => new BsonBenchmarkData[] - { - new("extended_bson/flat_bson.json", "Flat", 75_310_000), - new("extended_bson/full_bson.json", "Full", 57_340_000), - new("extended_bson/deep_bson.json", "Deep", 19_640_000) - }; + [GlobalCleanup] + public void Cleanup() + { + _writer.Dispose(); + _stream.Dispose(); } + + public IEnumerable BenchmarkDataSources() => + [ + new("extended_bson/flat_bson.json", "Flat", 75_310_000, typeof(FlatPoco)), + new("extended_bson/full_bson.json", "Full", 57_340_000, typeof(FullPoco)), + new("extended_bson/deep_bson.json", "Deep", 19_640_000, typeof(DeepPocoRoot)) + ]; } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/DriverBenchmarkCategory.cs b/benchmarks/MongoDB.Driver.Benchmarks/DriverBenchmarkCategory.cs index f8b1db2057f..a07a584bc5e 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/DriverBenchmarkCategory.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/DriverBenchmarkCategory.cs @@ -15,21 +15,20 @@ using System.Collections.Generic; -namespace MongoDB.Benchmarks +namespace MongoDB.Benchmarks; + +public static class DriverBenchmarkCategory { - public static class DriverBenchmarkCategory - { - public const string BsonBench = "BSONBench"; - public const string DriverBench = "DriverBench"; - public const string MultiBench = "MultiBench"; - public const string ParallelBench = "ParallelBench"; - public const string ReadBench = "ReadBench"; - public const string SingleBench = "SingleBench"; - public const string WriteBench = "WriteBench"; + public const string BsonBench = "BSONBench"; + public const string DriverBench = "DriverBench"; + public const string MultiBench = "MultiBench"; + public const string ParallelBench = "ParallelBench"; + public const string ReadBench = "ReadBench"; + public const string SingleBench = "SingleBench"; + public const string WriteBench = "WriteBench"; - // not included in AllCategories as it's not part of the benchmarking spec - public const string BulkWriteBench = "BulkWriteBench"; + // not included in AllCategories as it's not part of the benchmarking spec + public const string BulkWriteBench = "BulkWriteBench"; - public static readonly IEnumerable AllCategories = new[] {BsonBench, ReadBench, WriteBench, MultiBench, SingleBench, ParallelBench, DriverBench}; - } -} + public static readonly IEnumerable AllCategories = new[] {BsonBench, ReadBench, WriteBench, MultiBench, SingleBench, ParallelBench, DriverBench}; +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/Exporters/EvergreenExporter.cs b/benchmarks/MongoDB.Driver.Benchmarks/Exporters/EvergreenExporter.cs index 5e37ac64b23..dffd4c2af5c 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/Exporters/EvergreenExporter.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/Exporters/EvergreenExporter.cs @@ -22,67 +22,66 @@ using MongoDB.Bson.IO; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.Exporters +namespace MongoDB.Benchmarks.Exporters; + +public sealed class EvergreenExporter : IExporter { - public sealed class EvergreenExporter : IExporter + private readonly string _outputFile; + + public string Name => GetType().Name; + + public EvergreenExporter(string outputFile) { - private readonly string _outputFile; + _outputFile = outputFile; + } - public string Name => GetType().Name; + public void ExportToLog(Summary summary, ILogger logger) + { + } - public EvergreenExporter(string outputFile) - { - _outputFile = outputFile; - } + public IEnumerable ExportToFiles(Summary summary, ILogger consoleLogger) + { + var benchmarkResults = summary.Reports.Select(report => new BenchmarkResult(report)).ToArray(); - public void ExportToLog(Summary summary, ILogger logger) - { - } + var resultsPath = Path.Combine(summary.ResultsDirectoryPath, _outputFile); - public IEnumerable ExportToFiles(Summary summary, ILogger consoleLogger) + using (var resultsFileWriter = File.CreateText(resultsPath)) + using (var jsonWriter = new JsonWriter(resultsFileWriter, new JsonWriterSettings { Indent = true })) { - var benchmarkResults = summary.Reports.Select(report => new BenchmarkResult(report)).ToArray(); - - var resultsPath = Path.Combine(summary.ResultsDirectoryPath, _outputFile); + jsonWriter.WriteStartArray(); - using (var resultsFileWriter = File.CreateText(resultsPath)) - using (var jsonWriter = new JsonWriter(resultsFileWriter, new JsonWriterSettings { Indent = true })) + // write composite scores e.g ReadBench + foreach (var category in DriverBenchmarkCategory.AllCategories) { - jsonWriter.WriteStartArray(); - - // write composite scores e.g ReadBench - foreach (var category in DriverBenchmarkCategory.AllCategories) - { - WriteScoreToResults(jsonWriter, category, CalculateCompositeScore(benchmarkResults, category)); - } - - // write individual benchmarks results - foreach (var benchmark in benchmarkResults) - { - WriteScoreToResults(jsonWriter, benchmark.Name, benchmark.Score); - } + WriteScoreToResults(jsonWriter, category, CalculateCompositeScore(benchmarkResults, category)); + } - jsonWriter.WriteEndArray(); + // write individual benchmarks results + foreach (var benchmark in benchmarkResults) + { + WriteScoreToResults(jsonWriter, benchmark.Name, benchmark.Score); } - return new[] { resultsPath }; + jsonWriter.WriteEndArray(); } - private static void WriteScoreToResults(JsonWriter jsonWriter, string name, double score) - { - jsonWriter.WriteStartDocument(); - jsonWriter.WriteStartDocument("info"); - jsonWriter.WriteString("test_name", name); - jsonWriter.WriteEndDocument(); - - jsonWriter.WriteStartArray("metrics"); - jsonWriter.WriteStartDocument(); - jsonWriter.WriteString("name", "megabytes_per_second"); - jsonWriter.WriteDouble("value", score); - jsonWriter.WriteEndDocument(); - jsonWriter.WriteEndArray(); + return new[] { resultsPath }; + } - jsonWriter.WriteEndDocument(); - } + private static void WriteScoreToResults(JsonWriter jsonWriter, string name, double score) + { + jsonWriter.WriteStartDocument(); + jsonWriter.WriteStartDocument("info"); + jsonWriter.WriteString("test_name", name); + jsonWriter.WriteEndDocument(); + + jsonWriter.WriteStartArray("metrics"); + jsonWriter.WriteStartDocument(); + jsonWriter.WriteString("name", "megabytes_per_second"); + jsonWriter.WriteDouble("value", score); + jsonWriter.WriteEndDocument(); + jsonWriter.WriteEndArray(); + + jsonWriter.WriteEndDocument(); } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/Exporters/LocalExporter.cs b/benchmarks/MongoDB.Driver.Benchmarks/Exporters/LocalExporter.cs index e3ced36cbd9..21d742cdf4c 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/Exporters/LocalExporter.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/Exporters/LocalExporter.cs @@ -21,52 +21,51 @@ using BenchmarkDotNet.Reports; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.Exporters +namespace MongoDB.Benchmarks.Exporters; + +public sealed class LocalExporter : IExporter { - public sealed class LocalExporter : IExporter + public string Name => GetType().Name; + + public void ExportToLog(Summary summary, ILogger logger) { - public string Name => GetType().Name; + } - public void ExportToLog(Summary summary, ILogger logger) - { - } + public IEnumerable ExportToFiles(Summary summary, ILogger consoleLogger) + { + var exportedFiles = new List(); - public IEnumerable ExportToFiles(Summary summary, ILogger consoleLogger) + var benchmarksGroupedByRuntime = summary.Reports.GroupBy(b => b.GetRuntimeInfo()).ToArray(); + foreach (var benchmarkGroup in benchmarksGroupedByRuntime) { - var exportedFiles = new List(); - - var benchmarksGroupedByRuntime = summary.Reports.GroupBy(b => b.GetRuntimeInfo()).ToArray(); - foreach (var benchmarkGroup in benchmarksGroupedByRuntime) - { - var runtime = benchmarkGroup.Key; - var filename = $"local-report({runtime}).txt"; - var path = Path.Combine(summary.ResultsDirectoryPath, filename); - - using var writer = new StreamWriter(path, false); - var benchmarkResults = benchmarkGroup.Select(report => new BenchmarkResult(report)).ToArray(); + var runtime = benchmarkGroup.Key; + var filename = $"local-report({runtime}).txt"; + var path = Path.Combine(summary.ResultsDirectoryPath, filename); - writer.WriteLine("Scores Summary: "); - foreach (var category in DriverBenchmarkCategory.AllCategories) - { - WriteScore(writer, category, CalculateCompositeScore(benchmarkResults, category)); - } + using var writer = new StreamWriter(path, false); + var benchmarkResults = benchmarkGroup.Select(report => new BenchmarkResult(report)).ToArray(); - foreach (var benchmark in benchmarkResults) - { - WriteScore(writer, benchmark.Name, benchmark.Score); - } + writer.WriteLine("Scores Summary: "); + foreach (var category in DriverBenchmarkCategory.AllCategories) + { + WriteScore(writer, category, CalculateCompositeScore(benchmarkResults, category)); + } - exportedFiles.Add(path); + foreach (var benchmark in benchmarkResults) + { + WriteScore(writer, benchmark.Name, benchmark.Score); } - return exportedFiles; + exportedFiles.Add(path); } - private static void WriteScore(StreamWriter writer, string benchName, double score) - { - writer.WriteLine(score != 0 - ? $"Executed {benchName}, score: {score:F3} MB/s" - : $"Skipped {benchName}"); - } + return exportedFiles; + } + + private static void WriteScore(StreamWriter writer, string benchName, double score) + { + writer.WriteLine(score != 0 + ? $"Executed {benchName}, score: {score:F3} MB/s" + : $"Skipped {benchName}"); } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/LibmongocryptBindingBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/LibmongocryptBindingBenchmark.cs index 79c9d39f1d7..56c41e89ebd 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/LibmongocryptBindingBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/LibmongocryptBindingBenchmark.cs @@ -22,96 +22,95 @@ using MongoDB.Driver; using MongoDB.Driver.Encryption; -namespace MongoDB.Benchmarks +namespace MongoDB.Benchmarks; + +public class LibmongocryptBindingBenchmark { - public class LibmongocryptBindingBenchmark - { - private const int RepeatCount = 10; - private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk"; + private const int RepeatCount = 10; + private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk"; - private byte[] _encryptedValuesDocumentBytes; - private IMongoClient _disposableKeyVaultClient; - private IAutoEncryptionLibMongoCryptController _libMongoCryptController; + private byte[] _encryptedValuesDocumentBytes; + private IMongoClient _disposableKeyVaultClient; + private IAutoEncryptionLibMongoCryptController _libMongoCryptController; - [Params(1)] - public int ThreadsCount { get; set; } + [Params(1)] + public int ThreadsCount { get; set; } - [GlobalSetup] - public void Setup() - { - MongoClientSettings.Extensions.AddAutoEncryption(); + [GlobalSetup] + public void Setup() + { + MongoClientSettings.Extensions.AddAutoEncryption(); - var localMasterKey = Convert.FromBase64String(LocalMasterKey); + var localMasterKey = Convert.FromBase64String(LocalMasterKey); - var kmsProviders = new Dictionary>(); - var localKey = new Dictionary { { "key", localMasterKey } }; - kmsProviders.Add("local", localKey); + var kmsProviders = new Dictionary>(); + var localKey = new Dictionary { { "key", localMasterKey } }; + kmsProviders.Add("local", localKey); - var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault"); - var autoEncryptionOptions = new AutoEncryptionOptions( - keyVaultNamespace: keyVaultNamespace, - kmsProviders: kmsProviders, - bypassAutoEncryption: true); + var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault"); + var autoEncryptionOptions = new AutoEncryptionOptions( + keyVaultNamespace: keyVaultNamespace, + kmsProviders: kmsProviders, + bypassAutoEncryption: true); - var clientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost"); - clientSettings.AutoEncryptionOptions = autoEncryptionOptions; - clientSettings.ClusterSource = DisposingClusterSource.Instance; + var clientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost"); + clientSettings.AutoEncryptionOptions = autoEncryptionOptions; + clientSettings.ClusterSource = DisposingClusterSource.Instance; - _disposableKeyVaultClient = new MongoClient(clientSettings); + _disposableKeyVaultClient = new MongoClient(clientSettings); - var keyVaultDatabase = _disposableKeyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName); - keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName); - _disposableKeyVaultClient.DropDatabase("crypt-test"); + var keyVaultDatabase = _disposableKeyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName); + keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName); + _disposableKeyVaultClient.DropDatabase("crypt-test"); - var clientEncryptionSettings = new ClientEncryptionOptions( - _disposableKeyVaultClient, - keyVaultNamespace, - kmsProviders); + var clientEncryptionSettings = new ClientEncryptionOptions( + _disposableKeyVaultClient, + keyVaultNamespace, + kmsProviders); - var encryptedValuesDocument = new BsonDocument(); - using (var clientEncryption = new ClientEncryption(clientEncryptionSettings)) + var encryptedValuesDocument = new BsonDocument(); + using (var clientEncryption = new ClientEncryption(clientEncryptionSettings)) + { + var dataKeyId = clientEncryption.CreateDataKey( + "local", + new DataKeyOptions(), + CancellationToken.None); + + var encryptOptions = new EncryptOptions( + EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic.ToString(), + keyId: dataKeyId); + + for (int i = 0; i < 1500; i++) { - var dataKeyId = clientEncryption.CreateDataKey( - "local", - new DataKeyOptions(), - CancellationToken.None); - - var encryptOptions = new EncryptOptions( - EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic.ToString(), - keyId: dataKeyId); - - for (int i = 0; i < 1500; i++) - { - var toEncryptString = $"value {(i + 1):D4}"; - var encryptedString = - clientEncryption.Encrypt(toEncryptString, encryptOptions, CancellationToken.None); - encryptedValuesDocument.Add(new BsonElement($"key{(i + 1):D4}", encryptedString)); - } + var toEncryptString = $"value {(i + 1):D4}"; + var encryptedString = + clientEncryption.Encrypt(toEncryptString, encryptOptions, CancellationToken.None); + encryptedValuesDocument.Add(new BsonElement($"key{(i + 1):D4}", encryptedString)); } - _encryptedValuesDocumentBytes = encryptedValuesDocument.ToBson(); - - // Create libmongocrypt binding that will be used for decryption - _libMongoCryptController = - MongoClientSettings.Extensions.AutoEncryptionProvider.CreateAutoCryptClientController(_disposableKeyVaultClient, autoEncryptionOptions); } + _encryptedValuesDocumentBytes = encryptedValuesDocument.ToBson(); - [Benchmark] - public void BulkDecryptionUsingBinding() + // Create libmongocrypt binding that will be used for decryption + _libMongoCryptController = + MongoClientSettings.Extensions.AutoEncryptionProvider.CreateAutoCryptClientController(_disposableKeyVaultClient, autoEncryptionOptions); + } + + [Benchmark] + public void BulkDecryptionUsingBinding() + { + ThreadingUtilities.ExecuteOnNewThreads(ThreadsCount, _ => { - ThreadingUtilities.ExecuteOnNewThreads(ThreadsCount, _ => + for (int i = 0; i < RepeatCount; i++) { - for (int i = 0; i < RepeatCount; i++) - { - _libMongoCryptController.DecryptFields(_encryptedValuesDocumentBytes, CancellationToken.None); - } - }, 20000); - } + _libMongoCryptController.DecryptFields(_encryptedValuesDocumentBytes, CancellationToken.None); + } + }, 20000); + } - [GlobalCleanup] - public void Cleanup() - { - _libMongoCryptController.Dispose(); - _disposableKeyVaultClient.Dispose(); - } + [GlobalCleanup] + public void Cleanup() + { + _libMongoCryptController.Dispose(); + _disposableKeyVaultClient.Dispose(); } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/BulkWriteMixedOpsBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/BulkWriteMixedOpsBenchmark.cs index d7de47e2e13..dfc406ff5dc 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/BulkWriteMixedOpsBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/BulkWriteMixedOpsBenchmark.cs @@ -17,78 +17,102 @@ using System.Linq; using BenchmarkDotNet.Attributes; using MongoDB.Bson; +using MongoDB.Bson.Serialization; using MongoDB.Driver; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.MultiDoc +namespace MongoDB.Benchmarks.MultiDoc; + +[IterationCount(15)] +[BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class BulkWriteMixedOpsBenchmark { - [IterationCount(15)] - [BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class BulkWriteMixedOpsBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private readonly List _clientBulkWriteMixedOpsModels = []; - private readonly List> _collectionBulkWriteMixedOpsModels = []; + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoCollection _collectionPoco; + private IMongoDatabase _database; + private readonly List _clientBulkWriteMixedOpsModels = []; + private readonly List _clientBulkWriteMixedOpsPocoModels = []; + private readonly List> _collectionBulkWriteMixedOpsModels = []; + private readonly List> _collectionBulkWriteMixedOpsPocoModels = []; - private static readonly string[] __collectionNamespaces = Enumerable.Range(0, 10) - .Select(i => $"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}_{i}") - .ToArray(); + private static readonly string[] __collectionNamespaces = Enumerable.Range(0, 10) + .Select(i => $"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}_{i}") + .ToArray(); - [Params(5_500_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(5_500_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); - [GlobalSetup] - public void Setup() + var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); + for (var i = 0; i < 10000; i++) { - _client = MongoConfiguration.CreateClient(); + var collectionName = __collectionNamespaces[i % __collectionNamespaces.Length]; - var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); - for (var i = 0; i < 10000; i++) - { - var collectionName = __collectionNamespaces[i % __collectionNamespaces.Length]; + _clientBulkWriteMixedOpsModels.Add(new BulkWriteInsertOneModel(collectionName, smallDocument.DeepClone().AsBsonDocument)); + _clientBulkWriteMixedOpsModels.Add(new BulkWriteReplaceOneModel(collectionName, FilterDefinition.Empty, smallDocument.DeepClone().AsBsonDocument)); + _clientBulkWriteMixedOpsModels.Add(new BulkWriteDeleteOneModel(collectionName, FilterDefinition.Empty)); - _clientBulkWriteMixedOpsModels.Add(new BulkWriteInsertOneModel(collectionName, smallDocument.DeepClone().AsBsonDocument)); - _clientBulkWriteMixedOpsModels.Add(new BulkWriteReplaceOneModel(collectionName, FilterDefinition.Empty, smallDocument.DeepClone().AsBsonDocument)); - _clientBulkWriteMixedOpsModels.Add(new BulkWriteDeleteOneModel(collectionName, FilterDefinition.Empty)); + _collectionBulkWriteMixedOpsModels.Add(new InsertOneModel(smallDocument.DeepClone().AsBsonDocument)); + _collectionBulkWriteMixedOpsModels.Add(new ReplaceOneModel(FilterDefinition.Empty, smallDocument.DeepClone().AsBsonDocument)); + _collectionBulkWriteMixedOpsModels.Add(new DeleteOneModel(FilterDefinition.Empty)); - _collectionBulkWriteMixedOpsModels.Add(new InsertOneModel(smallDocument.DeepClone().AsBsonDocument)); - _collectionBulkWriteMixedOpsModels.Add(new ReplaceOneModel(FilterDefinition.Empty, smallDocument.DeepClone().AsBsonDocument)); - _collectionBulkWriteMixedOpsModels.Add(new DeleteOneModel(FilterDefinition.Empty)); - } + _clientBulkWriteMixedOpsPocoModels.Add(new BulkWriteInsertOneModel(collectionName, BsonSerializer.Deserialize(smallDocument))); + _clientBulkWriteMixedOpsPocoModels.Add(new BulkWriteReplaceOneModel(collectionName, FilterDefinition.Empty, BsonSerializer.Deserialize(smallDocument))); + _clientBulkWriteMixedOpsPocoModels.Add(new BulkWriteDeleteOneModel(collectionName, FilterDefinition.Empty)); + + _collectionBulkWriteMixedOpsPocoModels.Add(new InsertOneModel(BsonSerializer.Deserialize(smallDocument))); + _collectionBulkWriteMixedOpsPocoModels.Add(new ReplaceOneModel(FilterDefinition.Empty, BsonSerializer.Deserialize(smallDocument))); + _collectionBulkWriteMixedOpsPocoModels.Add(new DeleteOneModel(FilterDefinition.Empty)); } + } + + [IterationSetup] + public void BeforeTask() + { + _client.DropDatabase(MongoConfiguration.PerfTestDatabaseName); - [IterationSetup] - public void BeforeTask() + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + foreach (var collectionName in __collectionNamespaces) { - _client.DropDatabase(MongoConfiguration.PerfTestDatabaseName); + _database.CreateCollection(collectionName.Split('.')[1]); + } - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - foreach (var collectionName in __collectionNamespaces) - { - _database.CreateCollection(collectionName.Split('.')[1]); - } + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + _collectionPoco = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + } - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - } + [Benchmark] + public void SmallDocCollectionBulkWriteMixedOpsBenchmark() + { + _collection.BulkWrite(_collectionBulkWriteMixedOpsModels, new()); + } - [Benchmark] - public void SmallDocCollectionBulkWriteMixedOpsBenchmark() - { - _collection.BulkWrite(_collectionBulkWriteMixedOpsModels, new()); - } + [Benchmark] + public void SmallDocCollectionBulkWriteMixedOpsPocoBenchmark() + { + _collectionPoco.BulkWrite(_collectionBulkWriteMixedOpsPocoModels, new()); + } - [Benchmark] - public void SmallDocClientBulkWriteMixedOpsBenchmark() - { - _client.BulkWrite(_clientBulkWriteMixedOpsModels, new()); - } + [Benchmark] + public void SmallDocClientBulkWriteMixedOpsBenchmark() + { + _client.BulkWrite(_clientBulkWriteMixedOpsModels, new()); + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [Benchmark] + public void SmallDocClientBulkWriteMixedPocoOpsBenchmark() + { + _client.BulkWrite(_clientBulkWriteMixedOpsPocoModels, new()); + } + + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/FindManyBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/FindManyBenchmark.cs index 0b72d4270b1..83f4c388e22 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/FindManyBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/FindManyBenchmark.cs @@ -17,48 +17,56 @@ using BenchmarkDotNet.Attributes; using MongoDB.Bson; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.MultiDoc +namespace MongoDB.Benchmarks.MultiDoc; + +[IterationTime(2000)] +[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] +public class FindManyBenchmark { - [IterationTime(2000)] - [BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] - public class FindManyBenchmark + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoCollection _collectionPoco; + private BsonDocument _tweetDocument; + + [Params(16_220_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + + [GlobalSetup] + public void Setup() { - private IMongoClient _client; - private IMongoCollection _collection; - private BsonDocument _tweetDocument; + _client = MongoConfiguration.CreateClient(); + _tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json"); + var db = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - [Params(16_220_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + _collection = db.GetCollection(MongoConfiguration.PerfTestCollectionName); + _collectionPoco = db.GetCollection(MongoConfiguration.PerfTestCollectionName); - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json"); - _collection = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName).GetCollection(MongoConfiguration.PerfTestCollectionName); + PopulateCollection(); + } - PopulateCollection(); - } + [Benchmark] + public void FindManyAndEmptyCursor() + { + _ = _collection.Find(Builders.Filter.Empty).ToList(); + } - [Benchmark] - public void FindManyAndEmptyCursor() - { - _collection.Find(Builders.Filter.Empty).ToList(); - } + [Benchmark] + public void FindManyAndEmptyCursorPoco() + { + _ = _collectionPoco.Find(Builders.Filter.Empty).ToList(); + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); + } - private void PopulateCollection() - { - var documents = Enumerable.Range(0, 10000).Select(_ => _tweetDocument.DeepClone().AsBsonDocument); - _collection.InsertMany(documents); - } + private void PopulateCollection() + { + var documents = Enumerable.Range(0, 10000).Select(_ => _tweetDocument.DeepClone().AsBsonDocument); + _collection.InsertMany(documents); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSDownloadBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSDownloadBenchmark.cs index 2fd79673571..2f2df0a5bc6 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSDownloadBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSDownloadBenchmark.cs @@ -20,37 +20,36 @@ using MongoDB.Driver.GridFS; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.MultiDoc +namespace MongoDB.Benchmarks.MultiDoc; + +[IterationTime(3000)] +[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] +public class GridFsDownloadBenchmark { - [IterationTime(3000)] - [BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] - public class GridFsDownloadBenchmark - { - private IMongoClient _client; - private ObjectId _fileId; - private GridFSBucket _gridFsBucket; + private IMongoClient _client; + private ObjectId _fileId; + private GridFSBucket _gridFsBucket; - [Params(52_428_800)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(52_428_800)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); - _fileId = _gridFsBucket.UploadFromStream("gridfstest", File.OpenRead($"{DataFolderPath}single_and_multi_document/gridfs_large.bin")); - } + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); + _fileId = _gridFsBucket.UploadFromStream("gridfstest", File.OpenRead($"{DataFolderPath}single_and_multi_document/gridfs_large.bin")); + } - [Benchmark] - public void GridFsDownload() - { - _gridFsBucket.DownloadAsBytes(_fileId); - } + [Benchmark] + public void GridFsDownload() + { + _gridFsBucket.DownloadAsBytes(_fileId); + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSUploadBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSUploadBenchmark.cs index f5fc8edb775..f31d40e3394 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSUploadBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSUploadBenchmark.cs @@ -19,44 +19,43 @@ using MongoDB.Driver.GridFS; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.MultiDoc +namespace MongoDB.Benchmarks.MultiDoc; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class GridFsUploadBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class GridFsUploadBenchmark + private IMongoClient _client; + private byte[] _fileBytes; + private GridFSBucket _gridFsBucket; + + [Params(52_428_800)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _fileBytes = File.ReadAllBytes($"{DataFolderPath}single_and_multi_document/gridfs_large.bin"); + _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); + } + + [IterationSetup] + public void BeforeTask() + { + _gridFsBucket.Drop(); + _gridFsBucket.UploadFromBytes("smallfile", new byte[1]); + } + + [Benchmark] + public void GridFsUpload() + { + _gridFsBucket.UploadFromBytes("gridfstest", _fileBytes); + } + + [GlobalCleanup] + public void Teardown() { - private IMongoClient _client; - private byte[] _fileBytes; - private GridFSBucket _gridFsBucket; - - [Params(52_428_800)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _fileBytes = File.ReadAllBytes($"{DataFolderPath}single_and_multi_document/gridfs_large.bin"); - _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); - } - - [IterationSetup] - public void BeforeTask() - { - _gridFsBucket.Drop(); - _gridFsBucket.UploadFromBytes("smallfile", new byte[1]); - } - - [Benchmark] - public void GridFsUpload() - { - _gridFsBucket.UploadFromBytes("gridfstest", _fileBytes); - } - - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + _client.Dispose(); } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/LargeDocBulkInsertBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/LargeDocBulkInsertBenchmark.cs index 32576821996..a52a707f253 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/LargeDocBulkInsertBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/LargeDocBulkInsertBenchmark.cs @@ -13,73 +13,71 @@ * limitations under the License. */ -using System.Collections.Generic; using System.Linq; using BenchmarkDotNet.Attributes; using MongoDB.Bson; using MongoDB.Driver; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.MultiDoc +namespace MongoDB.Benchmarks.MultiDoc; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class LargeDocBulkInsertBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class LargeDocBulkInsertBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private BsonDocument[] _largeDocuments; - private InsertOneModel[] _collectionBulkWriteInsertModels; - private BulkWriteInsertOneModel[] _clientBulkWriteInsertModels; + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoDatabase _database; + private BsonDocument[] _largeDocuments; + private InsertOneModel[] _collectionBulkWriteInsertModels; + private BulkWriteInsertOneModel[] _clientBulkWriteInsertModels; - private static readonly CollectionNamespace __collectionNamespace = - CollectionNamespace.FromFullName($"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}"); + private static readonly CollectionNamespace __collectionNamespace = + CollectionNamespace.FromFullName($"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}"); - [Params(27_310_890)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(27_310_890)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - var largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json"); - _largeDocuments = Enumerable.Range(0, 10).Select(_ => largeDocument.DeepClone().AsBsonDocument).ToArray(); - _collectionBulkWriteInsertModels = _largeDocuments.Select(x => new InsertOneModel(x.DeepClone().AsBsonDocument)).ToArray(); - _clientBulkWriteInsertModels = _largeDocuments.Select(x => new BulkWriteInsertOneModel(__collectionNamespace, x.DeepClone().AsBsonDocument)).ToArray(); - } + var largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json"); + _largeDocuments = Enumerable.Range(0, 10).Select(_ => largeDocument.DeepClone().AsBsonDocument).ToArray(); + _collectionBulkWriteInsertModels = _largeDocuments.Select(x => new InsertOneModel(x.DeepClone().AsBsonDocument)).ToArray(); + _clientBulkWriteInsertModels = _largeDocuments.Select(x => new BulkWriteInsertOneModel(__collectionNamespace, x.DeepClone().AsBsonDocument)).ToArray(); + } - [IterationSetup] - public void BeforeTask() - { - _database.DropCollection(MongoConfiguration.PerfTestCollectionName); - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - } + [IterationSetup] + public void BeforeTask() + { + _database.DropCollection(MongoConfiguration.PerfTestCollectionName); + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + } - [Benchmark] - public void InsertManyLargeBenchmark() - { - _collection.InsertMany(_largeDocuments, new()); - } + [Benchmark] + public void InsertManyLargeBenchmark() + { + _collection.InsertMany(_largeDocuments, new()); + } - [Benchmark] - public void LargeDocCollectionBulkWriteInsertBenchmark() - { - _collection.BulkWrite(_collectionBulkWriteInsertModels, new()); - } + [Benchmark] + public void LargeDocCollectionBulkWriteInsertBenchmark() + { + _collection.BulkWrite(_collectionBulkWriteInsertModels, new()); + } - [Benchmark] - public void LargeDocClientBulkWriteInsertBenchmark() - { - _client.BulkWrite(_clientBulkWriteInsertModels, new()); - } + [Benchmark] + public void LargeDocClientBulkWriteInsertBenchmark() + { + _client.BulkWrite(_clientBulkWriteInsertModels, new()); + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/MultiDocBenchmarkDataTypes.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/MultiDocBenchmarkDataTypes.cs new file mode 100644 index 00000000000..4fca0bad3da --- /dev/null +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/MultiDocBenchmarkDataTypes.cs @@ -0,0 +1,119 @@ +/* Copyright 2010-present MongoDB Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace MongoDB.Benchmarks.MultiDoc; + +public sealed class SmallDocPoco +{ + [BsonElement("3MXe8Wi7")] + public string _3MXe8Wi7 { get; set; } + [BsonElement("5C8GSDC5")] + public int _5C8GSDC5 { get; set; } + + public string oggaoR4O { get; set; } + public string Vxri7mmI { get; set; } + public string IQ8K4ZMG { get; set; } + public string WzI8s1W0 { get; set; } + public string E5Aj2zB3 { get; set; } + public string PHPSSV51 { get; set; } + public int CxSCo4jD { get; set; } + public int fC63DsLR { get; set; } + public int l6e0U4bR { get; set; } + public int TLRpkltp { get; set; } + public int Ph9CZN5L { get; set; } +} + +public sealed class Tweet +{ + [BsonId] + public ObjectId Id { get; set; } + + public long id { get; set; } + + public string text { get; set; } + public long? in_reply_to_status_id { get; set; } + public int? retweet_count { get; set; } + public object contributors { get; set; } + public string created_at { get; set; } + public object geo { get; set; } + public string source { get; set; } + public object coordinates { get; set; } + public string in_reply_to_screen_name { get; set; } + public bool truncated { get; set; } + public TweetEntities entities { get; set; } + public bool retweeted { get; set; } + public object place { get; set; } + public User user { get; set; } + public bool favorited { get; set; } + public long? in_reply_to_user_id { get; set; } +} + +public sealed class TweetEntities +{ + public UserMention[] user_mentions { get; set; } + public string[] urls { get; set; } + public string[] hashtags { get; set; } +} + +[BsonNoId] +public sealed class UserMention +{ + public long id { get; set; } + + public int[] indices { get; set; } + public string screen_name { get; set; } + public string name { get; set; } +} + +[BsonNoId] +public sealed class User +{ + public long id { get; set; } + + public int friends_count { get; set; } + public string profile_sidebar_fill_color { get; set; } + public string location { get; set; } + public bool verified { get; set; } + public object follow_request_sent { get; set; } + public int favourites_count { get; set; } + public string profile_sidebar_border_color { get; set; } + public string profile_image_url { get; set; } + public bool geo_enabled { get; set; } + public string created_at { get; set; } + public string description { get; set; } + public string time_zone { get; set; } + public string url { get; set; } + public string screen_name { get; set; } + public object notifications { get; set; } + public string profile_background_color { get; set; } + public int listed_count { get; set; } + public string lang { get; set; } + public string profile_background_image_url { get; set; } + public int statuses_count { get; set; } + public object following { get; set; } + public string profile_text_color { get; set; } + public bool @protected { get; set; } + public bool show_all_inline_media { get; set; } + public bool profile_background_tile { get; set; } + public string name { get; set; } + public bool contributors_enabled { get; set; } + public string profile_link_color { get; set; } + public int followers_count { get; set; } + public bool profile_use_background_image { get; set; } + public int utc_offset { get; set; } +} diff --git a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/SmallDocBulkInsertBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/SmallDocBulkInsertBenchmark.cs index a932541575f..70c7140a028 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/SmallDocBulkInsertBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/SmallDocBulkInsertBenchmark.cs @@ -16,69 +16,96 @@ using System.Linq; using BenchmarkDotNet.Attributes; using MongoDB.Bson; +using MongoDB.Bson.Serialization; using MongoDB.Driver; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.MultiDoc +namespace MongoDB.Benchmarks.MultiDoc; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class SmallDocBulkInsertBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class SmallDocBulkInsertBenchmark + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoCollection _collectionPocos; + private IMongoDatabase _database; + private BsonDocument[] _smallDocuments; + private SmallDocPoco[] _smallPocos; + private InsertOneModel[] _collectionBulkWriteInsertModels; + private InsertOneModel[] _collectionBulkWriteInsertModelsPoco; + private BulkWriteInsertOneModel[] _clientBulkWriteInsertModels; + private BulkWriteInsertOneModel[] _clientBulkWriteInsertModelsPoco; + + private static readonly CollectionNamespace __collectionNamespace = + CollectionNamespace.FromFullName($"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}"); + + [Params(2_750_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + + var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); + _smallDocuments = Enumerable.Range(0, 10000).Select(_ => smallDocument.DeepClone().AsBsonDocument).ToArray(); + _smallPocos = _smallDocuments.Select(d => BsonSerializer.Deserialize(d)).ToArray(); + + _collectionBulkWriteInsertModels = _smallDocuments.Select(x => new InsertOneModel(x.DeepClone().AsBsonDocument)).ToArray(); + _collectionBulkWriteInsertModelsPoco = _smallPocos.Select(x => new InsertOneModel(x)).ToArray(); + _clientBulkWriteInsertModels = _smallDocuments.Select(x => new BulkWriteInsertOneModel(__collectionNamespace, x.DeepClone().AsBsonDocument)).ToArray(); + _clientBulkWriteInsertModelsPoco = _smallPocos.Select(x => new BulkWriteInsertOneModel(__collectionNamespace, x)).ToArray(); + } + + [IterationSetup] + public void BeforeTask() + { + _database.DropCollection(MongoConfiguration.PerfTestCollectionName); + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + _collectionPocos = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + } + + [Benchmark] + public void InsertManySmallBenchmark() + { + _collection.InsertMany(_smallDocuments, new()); + } + + [Benchmark] + public void InsertManySmallBenchmarkPoco() + { + _collectionPocos.InsertMany(_smallPocos, new()); + } + + [Benchmark] + public void SmallDocCollectionBulkWriteInsertBenchmark() + { + _collection.BulkWrite(_collectionBulkWriteInsertModels, new()); + } + + [Benchmark] + public void SmallDocCollectionBulkWriteInsertBenchmarkPoco() + { + _collectionPocos.BulkWrite(_collectionBulkWriteInsertModelsPoco, new()); + } + + [Benchmark] + public void SmallDocClientBulkWriteInsertBenchmark() + { + _client.BulkWrite(_clientBulkWriteInsertModels, new()); + } + + [Benchmark] + public void SmallDocClientBulkWriteInsertBenchmarkPoco() + { + _client.BulkWrite(_clientBulkWriteInsertModelsPoco, new()); + } + + [GlobalCleanup] + public void Teardown() { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private BsonDocument[] _smallDocuments; - private InsertOneModel[] _collectionBulkWriteInsertModels; - private BulkWriteInsertOneModel[] _clientBulkWriteInsertModels; - - private static readonly CollectionNamespace __collectionNamespace = - CollectionNamespace.FromFullName($"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}"); - - [Params(2_750_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - - var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); - _smallDocuments = Enumerable.Range(0, 10000).Select(_ => smallDocument.DeepClone().AsBsonDocument).ToArray(); - _collectionBulkWriteInsertModels = _smallDocuments.Select(x => new InsertOneModel(x.DeepClone().AsBsonDocument)).ToArray(); - _clientBulkWriteInsertModels = _smallDocuments.Select(x => new BulkWriteInsertOneModel(__collectionNamespace, x.DeepClone().AsBsonDocument)).ToArray(); - } - - [IterationSetup] - public void BeforeTask() - { - _database.DropCollection(MongoConfiguration.PerfTestCollectionName); - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - } - - [Benchmark] - public void InsertManySmallBenchmark() - { - _collection.InsertMany(_smallDocuments, new()); - } - - [Benchmark] - public void SmallDocCollectionBulkWriteInsertBenchmark() - { - _collection.BulkWrite(_collectionBulkWriteInsertModels, new()); - } - - [Benchmark] - public void SmallDocClientBulkWriteInsertBenchmark() - { - _client.BulkWrite(_clientBulkWriteInsertModels, new()); - } - - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + _client.Dispose(); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileDownloadBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileDownloadBenchmark.cs index 1f1149786b7..0afa498ec88 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileDownloadBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileDownloadBenchmark.cs @@ -21,74 +21,73 @@ using MongoDB.Driver.GridFS; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.ParallelBench +namespace MongoDB.Benchmarks.ParallelBench; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] +public class GridFSMultiFileDownloadBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] - public class GridFSMultiFileDownloadBenchmark - { - private IMongoClient _client; - private GridFSBucket _gridFsBucket; - private string _tmpDirectoryPath; - private ConcurrentQueue<(string, int)> _filesToDownload; + private IMongoClient _client; + private GridFSBucket _gridFsBucket; + private string _tmpDirectoryPath; + private ConcurrentQueue<(string, int)> _filesToDownload; - [Params(262_144_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(262_144_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); - _gridFsBucket.Drop(); - _tmpDirectoryPath = $"{DataFolderPath}parallel/tmpGridFS"; - _filesToDownload = new ConcurrentQueue<(string, int)>(); + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); + _gridFsBucket.Drop(); + _tmpDirectoryPath = $"{DataFolderPath}parallel/tmpGridFS"; + _filesToDownload = new ConcurrentQueue<(string, int)>(); - PopulateDatabase(); - } + PopulateDatabase(); + } - [IterationSetup] - public void BeforeTask() - { - CreateEmptyDirectory(_tmpDirectoryPath); - AddFilesToQueue(_filesToDownload, _tmpDirectoryPath, "file", 50); - } + [IterationSetup] + public void BeforeTask() + { + CreateEmptyDirectory(_tmpDirectoryPath); + AddFilesToQueue(_filesToDownload, _tmpDirectoryPath, "file", 50); + } - [Benchmark] - public void GridFsMultiDownload() + [Benchmark] + public void GridFsMultiDownload() + { + ThreadingUtilities.ExecuteOnNewThreads(16, _ => { - ThreadingUtilities.ExecuteOnNewThreads(16, _ => + while (_filesToDownload.TryDequeue(out var fileToDownloadInfo)) { - while (_filesToDownload.TryDequeue(out var fileToDownloadInfo)) - { - var filename = $"file{fileToDownloadInfo.Item2:D2}.txt"; - var resourcePath = fileToDownloadInfo.Item1; + var filename = $"file{fileToDownloadInfo.Item2:D2}.txt"; + var resourcePath = fileToDownloadInfo.Item1; - using (var file = File.Create(resourcePath)) - { - _gridFsBucket.DownloadToStreamByName(filename, file); - } + using (var file = File.Create(resourcePath)) + { + _gridFsBucket.DownloadToStreamByName(filename, file); } - }); - } + } + }); + } - [GlobalCleanup] - public void Teardown() - { - Directory.Delete(_tmpDirectoryPath, true); - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + Directory.Delete(_tmpDirectoryPath, true); + _client.Dispose(); + } - private void PopulateDatabase() + private void PopulateDatabase() + { + for (int i = 0; i < 50; i++) { - for (int i = 0; i < 50; i++) - { - var filename = $"file{i:D2}.txt"; - var resourcePath = $"{DataFolderPath}parallel/gridfs_multi/{filename}"; + var filename = $"file{i:D2}.txt"; + var resourcePath = $"{DataFolderPath}parallel/gridfs_multi/{filename}"; - using var file = File.Open(resourcePath, FileMode.Open); - _gridFsBucket.UploadFromStream(filename, file); - } + using var file = File.Open(resourcePath, FileMode.Open); + _gridFsBucket.UploadFromStream(filename, file); } } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileUploadBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileUploadBenchmark.cs index ab7627263e5..af0caceb9b8 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileUploadBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileUploadBenchmark.cs @@ -21,56 +21,55 @@ using MongoDB.Driver.GridFS; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.ParallelBench +namespace MongoDB.Benchmarks.ParallelBench; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class GridFSMultiFileUploadBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class GridFSMultiFileUploadBenchmark - { - private IMongoClient _client; - private GridFSBucket _gridFsBucket; - private ConcurrentQueue<(string, int)> _filesToUpload; + private IMongoClient _client; + private GridFSBucket _gridFsBucket; + private ConcurrentQueue<(string, int)> _filesToUpload; - [Params(262_144_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(262_144_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); - _filesToUpload = new ConcurrentQueue<(string, int)>(); - } + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName)); + _filesToUpload = new ConcurrentQueue<(string, int)>(); + } - [IterationSetup] - public void BeforeTask() - { - _gridFsBucket.Drop(); - _gridFsBucket.UploadFromBytes("smallfile", new byte[1]); + [IterationSetup] + public void BeforeTask() + { + _gridFsBucket.Drop(); + _gridFsBucket.UploadFromBytes("smallfile", new byte[1]); - AddFilesToQueue(_filesToUpload, $"{DataFolderPath}parallel/gridfs_multi", "file", 50); - } + AddFilesToQueue(_filesToUpload, $"{DataFolderPath}parallel/gridfs_multi", "file", 50); + } - [Benchmark] - public void GridFsMultiUpload() + [Benchmark] + public void GridFsMultiUpload() + { + ThreadingUtilities.ExecuteOnNewThreads(16, _ => { - ThreadingUtilities.ExecuteOnNewThreads(16, _ => + while (_filesToUpload.TryDequeue(out var filesToUploadInfo)) { - while (_filesToUpload.TryDequeue(out var filesToUploadInfo)) - { - var filename = $"file{filesToUploadInfo.Item2:D2}.txt"; - var resourcePath = filesToUploadInfo.Item1; + var filename = $"file{filesToUploadInfo.Item2:D2}.txt"; + var resourcePath = filesToUploadInfo.Item1; - using var file = File.Open(resourcePath, FileMode.Open); - _gridFsBucket.UploadFromStream(filename, file); - } - }); - } + using var file = File.Open(resourcePath, FileMode.Open); + _gridFsBucket.UploadFromStream(filename, file); + } + }); + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } -} +} \ No newline at end of file diff --git a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileExportBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileExportBenchmark.cs index a20e79d1ec6..2142ef1e123 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileExportBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileExportBenchmark.cs @@ -20,80 +20,78 @@ using MongoDB.Bson; using MongoDB.Bson.TestHelpers; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.ParallelBench +namespace MongoDB.Benchmarks.ParallelBench; + +[WarmupCount(3)] +[IterationCount(5)] +[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] +public class MultiFileExportBenchmark { - [WarmupCount(3)] - [IterationCount(5)] - [BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] - public class MultiFileExportBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private string _tmpDirectoryPath; - private ConcurrentQueue<(string, int)> _filesToDownload; + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoDatabase _database; + private string _tmpDirectoryPath; + private ConcurrentQueue<(string, int)> _filesToDownload; - [Params(565_000_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(565_000_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - _tmpDirectoryPath = $"{DataFolderPath}parallel/tmpLDJSON"; - _filesToDownload = new ConcurrentQueue<(string, int)>(); + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + _tmpDirectoryPath = $"{DataFolderPath}parallel/tmpLDJSON"; + _filesToDownload = new ConcurrentQueue<(string, int)>(); - PopulateCollection(); - } + PopulateCollection(); + } - [IterationSetup] - public void BeforeTask() - { - CreateEmptyDirectory(_tmpDirectoryPath); - AddFilesToQueue(_filesToDownload, _tmpDirectoryPath, "ldjson", 100); - } + [IterationSetup] + public void BeforeTask() + { + CreateEmptyDirectory(_tmpDirectoryPath); + AddFilesToQueue(_filesToDownload, _tmpDirectoryPath, "ldjson", 100); + } - [Benchmark] - public void MultiFileExport() + [Benchmark] + public void MultiFileExport() + { + ThreadingUtilities.ExecuteOnNewThreads(16, _ => { - ThreadingUtilities.ExecuteOnNewThreads(16, _ => + while (_filesToDownload.TryDequeue(out var fileToDownloadInfo)) { - while (_filesToDownload.TryDequeue(out var fileToDownloadInfo)) - { - var (filePath, fileNumber) = fileToDownloadInfo; - var documents = _collection.Find(Builders.Filter.Empty).Skip(fileNumber * 5000).Limit(5000).ToList(); + var (filePath, fileNumber) = fileToDownloadInfo; + var documents = _collection.Find(Builders.Filter.Empty).Skip(fileNumber * 5000).Limit(5000).ToList(); - using (var streamWriter = File.CreateText(filePath)) + using (var streamWriter = File.CreateText(filePath)) + { + foreach (var document in documents) { - foreach (var document in documents) - { - streamWriter.WriteLine(document.ToJson()); - } + streamWriter.WriteLine(document.ToJson()); } } - }, 100_000); - } + } + }, 100_000); + } - [GlobalCleanup] - public void Teardown() - { - Directory.Delete(_tmpDirectoryPath, true); - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + Directory.Delete(_tmpDirectoryPath, true); + _client.Dispose(); + } - private void PopulateCollection() + private void PopulateCollection() + { + for (int i = 0; i < 100; i++) { - for (int i = 0; i < 100; i++) - { - var resourcePath = $"{DataFolderPath}parallel/ldjson_multi/ldjson{i:D3}.txt"; - var documents = File.ReadLines(resourcePath).Select(BsonDocument.Parse).ToArray(); - _collection.InsertMany(documents); - } + var resourcePath = $"{DataFolderPath}parallel/ldjson_multi/ldjson{i:D3}.txt"; + var documents = File.ReadLines(resourcePath).Select(BsonDocument.Parse).ToArray(); + _collection.InsertMany(documents); } } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileImportBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileImportBenchmark.cs index 09be9cbae57..85970d4b5f0 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileImportBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileImportBenchmark.cs @@ -20,59 +20,57 @@ using MongoDB.Bson; using MongoDB.Bson.TestHelpers; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.ParallelBench +namespace MongoDB.Benchmarks.ParallelBench; + +[WarmupCount(2)] +[IterationCount(4)] +[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class MultiFileImportBenchmark { - [WarmupCount(2)] - [IterationCount(4)] - [BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class MultiFileImportBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private ConcurrentQueue<(string, int)> _filesToUpload; + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoDatabase _database; + private ConcurrentQueue<(string, int)> _filesToUpload; - [Params(565_000_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(565_000_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - _filesToUpload = new ConcurrentQueue<(string, int)>(); - } + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + _filesToUpload = new ConcurrentQueue<(string, int)>(); + } - [IterationSetup] - public void BeforeTask() - { - _database.DropCollection(MongoConfiguration.PerfTestCollectionName); - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + [IterationSetup] + public void BeforeTask() + { + _database.DropCollection(MongoConfiguration.PerfTestCollectionName); + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - AddFilesToQueue(_filesToUpload, $"{DataFolderPath}parallel/ldjson_multi", "ldjson", 100); - } + AddFilesToQueue(_filesToUpload, $"{DataFolderPath}parallel/ldjson_multi", "ldjson", 100); + } - [Benchmark] - public void MultiFileImport() + [Benchmark] + public void MultiFileImport() + { + ThreadingUtilities.ExecuteOnNewThreads(16, _ => { - ThreadingUtilities.ExecuteOnNewThreads(16, _ => + while (_filesToUpload.TryDequeue(out var filesToUploadInfo)) { - while (_filesToUpload.TryDequeue(out var filesToUploadInfo)) - { - var resourcePath = filesToUploadInfo.Item1; - var documents = File.ReadLines(resourcePath).Select(BsonDocument.Parse).ToArray(); - _collection.InsertMany(documents); - } - }, 100_000); - } + var resourcePath = filesToUploadInfo.Item1; + var documents = File.ReadLines(resourcePath).Select(BsonDocument.Parse).ToArray(); + _collection.InsertMany(documents); + } + }, 100_000); + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/README.md b/benchmarks/MongoDB.Driver.Benchmarks/README.md index 899a9df0ae6..47c02d4b809 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/README.md +++ b/benchmarks/MongoDB.Driver.Benchmarks/README.md @@ -27,7 +27,7 @@ To export the benchmark results into the format expected for evergreen, run the specify the name of the exported file for evergreen using the `--output|-o` option. By default, the exported file is named `evergreen-results.json`. As the benchmarks run, various useful info generated by BenchmarkDotNet is output to the console. -Note: A `BenchmarkDotNet.Artifacts` folder containing all output files (logs, exported files etc) is created after running the benchmarks. +Note: A `BenchmarkDotNet.Artifacts` folder containing all output files (logs, exported files, etc) is created after running the benchmarks. To see the list of available options that can be passed to the benchmark runner, run `dotnet run -c Release -- --help`. diff --git a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/FindOneBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/FindOneBenchmark.cs index b050a57407c..34f269b7623 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/FindOneBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/FindOneBenchmark.cs @@ -15,54 +15,70 @@ using System.Linq; using BenchmarkDotNet.Attributes; +using MongoDB.Benchmarks.MultiDoc; using MongoDB.Bson; +using MongoDB.Bson.Serialization; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.SingleDoc +namespace MongoDB.Benchmarks.SingleDoc; + +[IterationTime(3000)] +[BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] +public class FindOneBenchmark { - [IterationTime(3000)] - [BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)] - public class FindOneBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private BsonDocument _tweetDocument; + private const int Iterations = 10_000; - [Params(16_220_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoCollection _collectionPoco; + private BsonDocument _tweetDocument; + private Tweet _tweetDocumentPoco; - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _collection = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName).GetCollection(MongoConfiguration.PerfTestCollectionName); - _tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json"); + [Params(16_220_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - PopulateCollection(); - } + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + var db = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + _collection = db.GetCollection(MongoConfiguration.PerfTestCollectionName); + _collectionPoco = db.GetCollection(MongoConfiguration.PerfTestCollectionName); + _tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json"); + _tweetDocumentPoco = BsonSerializer.Deserialize(_tweetDocument); - [Benchmark] - public void FindOne() - { - for (int i = 0; i < 10000; i++) - { - _collection.Find(new BsonDocument("_id", i)).First(); - } - } + PopulateCollection(); + } - [GlobalCleanup] - public void Teardown() + [Benchmark] + public void FindOne() + { + for (int i = 0; i < Iterations; i++) { - _client.Dispose(); + _collection.Find(new BsonDocument("_id", i)).First(); } + } - private void PopulateCollection() + [Benchmark] + public void FindOnePoco() + { + for (int i = 0; i < Iterations; i++) { - var documents = Enumerable.Range(0, 10000) - .Select(i => _tweetDocument.DeepClone().AsBsonDocument.Add("_id", i)); - _collection.InsertMany(documents); + _collection.Find(new BsonDocument("_id", i)).First(); } } + + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); + } + + private void PopulateCollection() + { + var documents = Enumerable.Range(0, Iterations) + .Select(i => _tweetDocument.DeepClone().AsBsonDocument.Add("_id", i)); + _collection.InsertMany(documents); + } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneLargeBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneLargeBenchmark.cs index b02c83ed02e..15a59a7ed28 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneLargeBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneLargeBenchmark.cs @@ -16,52 +16,50 @@ using BenchmarkDotNet.Attributes; using MongoDB.Bson; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.SingleDoc +namespace MongoDB.Benchmarks.SingleDoc; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class InsertOneLargeBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class InsertOneLargeBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private BsonDocument _largeDocument; + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoDatabase _database; + private BsonDocument _largeDocument; - [Params(27_310_890)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(27_310_890)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - _largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json"); - } + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + _largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json"); + } - [IterationSetup] - public void BeforeTask() - { - _database.DropCollection(MongoConfiguration.PerfTestCollectionName); - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - } + [IterationSetup] + public void BeforeTask() + { + _database.DropCollection(MongoConfiguration.PerfTestCollectionName); + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + } - [Benchmark] - public void InsertOneLarge() + [Benchmark] + public void InsertOneLarge() + { + for (int i = 0; i < 10; i++) { - for (int i = 0; i < 10; i++) - { - _largeDocument.Remove("_id"); - _collection.InsertOne(_largeDocument); - } + _largeDocument.Remove("_id"); + _collection.InsertOne(_largeDocument); } + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneSmallBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneSmallBenchmark.cs index d7d1738282e..bb38552d2bf 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneSmallBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/InsertOneSmallBenchmark.cs @@ -14,54 +14,69 @@ */ using BenchmarkDotNet.Attributes; +using MongoDB.Benchmarks.MultiDoc; using MongoDB.Bson; +using MongoDB.Bson.Serialization; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; using static MongoDB.Benchmarks.BenchmarkHelper; -namespace MongoDB.Benchmarks.SingleDoc +namespace MongoDB.Benchmarks.SingleDoc; + +[IterationCount(100)] +[BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] +public class InsertOneSmallBenchmark { - [IterationCount(100)] - [BenchmarkCategory(DriverBenchmarkCategory.SingleBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] - public class InsertOneSmallBenchmark - { - private IMongoClient _client; - private IMongoCollection _collection; - private IMongoDatabase _database; - private BsonDocument _smallDocument; + private const int Iterations = 10_000; - [Params(2_750_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + private IMongoClient _client; + private IMongoCollection _collection; + private IMongoCollection _collectionPoco; + private IMongoDatabase _database; + private BsonDocument _smallDocument; + private SmallDocPoco _smallDocumentPoco; - [GlobalSetup] - public void Setup() - { - _client = MongoConfiguration.CreateClient(); - _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); - _smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); - } + [Params(2_750_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [IterationSetup] - public void BeforeTask() - { - _database.DropCollection(MongoConfiguration.PerfTestCollectionName); - _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); - } + [GlobalSetup] + public void Setup() + { + _client = MongoConfiguration.CreateClient(); + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); + _smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); + _smallDocumentPoco = BsonSerializer.Deserialize(_smallDocument); + } - [Benchmark] - public void InsertOneSmall() + [IterationSetup] + public void BeforeTask() + { + _database.DropCollection(MongoConfiguration.PerfTestCollectionName); + _collection = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + _collectionPoco = _database.GetCollection(MongoConfiguration.PerfTestCollectionName); + } + + [Benchmark] + public void InsertOneSmall() + { + for (int i = 0; i < Iterations; i++) { - for (int i = 0; i < 10000; i++) - { - _smallDocument.Remove("_id"); - _collection.InsertOne(_smallDocument); - } + _smallDocument.Remove("_id"); + _collection.InsertOne(_smallDocument); } + } - [GlobalCleanup] - public void Teardown() + [Benchmark] + public void InsertOneSmallPoco() + { + for (int i = 0; i < Iterations; i++) { - _client.Dispose(); + _collectionPoco.InsertOne(_smallDocumentPoco); } } + + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); + } } diff --git a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/RunCommandBenchmark.cs b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/RunCommandBenchmark.cs index 24cd0219f74..61c50537336 100644 --- a/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/RunCommandBenchmark.cs +++ b/benchmarks/MongoDB.Driver.Benchmarks/SingleDoc/RunCommandBenchmark.cs @@ -16,40 +16,38 @@ using BenchmarkDotNet.Attributes; using MongoDB.Bson; using MongoDB.Driver; -using MongoDB.Driver.TestHelpers; -namespace MongoDB.Benchmarks.SingleDoc +namespace MongoDB.Benchmarks.SingleDoc; + +[IterationTime(3000)] +[BenchmarkCategory("RunBench")] +public class RunCommandBenchmark { - [IterationTime(3000)] - [BenchmarkCategory("RunBench")] - public class RunCommandBenchmark - { - private IMongoClient _client; - private IMongoDatabase _database; + private IMongoClient _client; + private IMongoDatabase _database; - [Params(130_000)] - public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs + [Params(130_000)] + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs - [GlobalSetup] - public void Setup() - { - _client = BenchmarkHelper.MongoConfiguration.CreateClient(); - _database = _client.GetDatabase("admin"); - } + [GlobalSetup] + public void Setup() + { + _client = BenchmarkHelper.MongoConfiguration.CreateClient(); + _database = _client.GetDatabase("admin"); + } - [Benchmark] - public void RunCommand() + [Benchmark] + public void RunCommand() + { + for (int i = 0; i < 10000; i++) { - for (int i = 0; i < 10000; i++) - { - _database.RunCommand(new BsonDocument("hello", true)); - } + _database.RunCommand(new BsonDocument("hello", true)); } + } - [GlobalCleanup] - public void Teardown() - { - _client.Dispose(); - } + [GlobalCleanup] + public void Teardown() + { + _client.Dispose(); } }