|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using BenchmarkDotNet.Attributes; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Driver; |
| 21 | +using static MongoDB.Benchmarks.BenchmarkHelper; |
| 22 | + |
| 23 | +namespace MongoDB.Benchmarks.MultiDoc |
| 24 | +{ |
| 25 | + [IterationCount(15)] |
| 26 | + [BenchmarkCategory(DriverBenchmarkCategory.BulkWriteBench, DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)] |
| 27 | + public class BulkWriteMixedOpsBenchmark |
| 28 | + { |
| 29 | + private IMongoClient _client; |
| 30 | + private IMongoCollection<BsonDocument> _collection; |
| 31 | + private IMongoDatabase _database; |
| 32 | + private readonly List<BulkWriteModel> _clientBulkWriteMixedOpsModels = []; |
| 33 | + private readonly List<WriteModel<BsonDocument>> _collectionBulkWriteMixedOpsModels = []; |
| 34 | + |
| 35 | + private static readonly string[] __collectionNamespaces = Enumerable.Range(0, 10) |
| 36 | + .Select(i => $"{MongoConfiguration.PerfTestDatabaseName}.{MongoConfiguration.PerfTestCollectionName}_{i}") |
| 37 | + .ToArray(); |
| 38 | + |
| 39 | + [Params(5_500_000)] |
| 40 | + public int BenchmarkDataSetSize { get; set; } // used in BenchmarkResult.cs |
| 41 | + |
| 42 | + [GlobalSetup] |
| 43 | + public void Setup() |
| 44 | + { |
| 45 | + _client = MongoConfiguration.CreateClient(); |
| 46 | + |
| 47 | + var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json"); |
| 48 | + for (var i = 0; i < 10000; i++) |
| 49 | + { |
| 50 | + var collectionName = __collectionNamespaces[i % __collectionNamespaces.Length]; |
| 51 | + |
| 52 | + _clientBulkWriteMixedOpsModels.Add(new BulkWriteInsertOneModel<BsonDocument>(collectionName, smallDocument.DeepClone().AsBsonDocument)); |
| 53 | + _clientBulkWriteMixedOpsModels.Add(new BulkWriteReplaceOneModel<BsonDocument>(collectionName, FilterDefinition<BsonDocument>.Empty, smallDocument.DeepClone().AsBsonDocument)); |
| 54 | + _clientBulkWriteMixedOpsModels.Add(new BulkWriteDeleteOneModel<BsonDocument>(collectionName, FilterDefinition<BsonDocument>.Empty)); |
| 55 | + |
| 56 | + _collectionBulkWriteMixedOpsModels.Add(new InsertOneModel<BsonDocument>(smallDocument.DeepClone().AsBsonDocument)); |
| 57 | + _collectionBulkWriteMixedOpsModels.Add(new ReplaceOneModel<BsonDocument>(FilterDefinition<BsonDocument>.Empty, smallDocument.DeepClone().AsBsonDocument)); |
| 58 | + _collectionBulkWriteMixedOpsModels.Add(new DeleteOneModel<BsonDocument>(FilterDefinition<BsonDocument>.Empty)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [IterationSetup] |
| 63 | + public void BeforeTask() |
| 64 | + { |
| 65 | + _client.DropDatabase(MongoConfiguration.PerfTestDatabaseName); |
| 66 | + |
| 67 | + _database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName); |
| 68 | + foreach (var collectionName in __collectionNamespaces) |
| 69 | + { |
| 70 | + _database.CreateCollection(collectionName.Split('.')[1]); |
| 71 | + } |
| 72 | + |
| 73 | + _collection = _database.GetCollection<BsonDocument>(MongoConfiguration.PerfTestCollectionName); |
| 74 | + } |
| 75 | + |
| 76 | + [Benchmark] |
| 77 | + public void SmallDocCollectionBulkWriteMixedOpsBenchmark() |
| 78 | + { |
| 79 | + _collection.BulkWrite(_collectionBulkWriteMixedOpsModels, new()); |
| 80 | + } |
| 81 | + |
| 82 | + [Benchmark] |
| 83 | + public void SmallDocClientBulkWriteMixedOpsBenchmark() |
| 84 | + { |
| 85 | + _client.BulkWrite(_clientBulkWriteMixedOpsModels, new()); |
| 86 | + } |
| 87 | + |
| 88 | + [GlobalCleanup] |
| 89 | + public void Teardown() |
| 90 | + { |
| 91 | + _client.Dispose(); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments