@@ -29,7 +29,9 @@ public class SmallDocBulkInsertBenchmark
29
29
private IMongoClient _client ;
30
30
private IMongoCollection < BsonDocument > _collection ;
31
31
private IMongoDatabase _database ;
32
- private IEnumerable < BsonDocument > _smallDocuments ;
32
+ private List < BsonDocument > _smallDocuments ;
33
+ private List < InsertOneModel < BsonDocument > > _collectionBulkWriteInsertModels ;
34
+ private List < BulkWriteInsertOneModel < BsonDocument > > _clientBulkWriteInsertModels ;
33
35
34
36
private static readonly CollectionNamespace __collectionNamespace =
35
37
CollectionNamespace . FromFullName ( $ "{ MongoConfiguration . PerfTestDatabaseName } .{ MongoConfiguration . PerfTestCollectionName } ") ;
@@ -44,7 +46,9 @@ public void Setup()
44
46
_database = _client . GetDatabase ( MongoConfiguration . PerfTestDatabaseName ) ;
45
47
46
48
var smallDocument = ReadExtendedJson ( "single_and_multi_document/small_doc.json" ) ;
47
- _smallDocuments = Enumerable . Range ( 0 , 10000 ) . Select ( _ => smallDocument . DeepClone ( ) . AsBsonDocument ) ;
49
+ _smallDocuments = Enumerable . Range ( 0 , 10000 ) . Select ( _ => smallDocument . DeepClone ( ) . AsBsonDocument ) . ToList ( ) ;
50
+ _collectionBulkWriteInsertModels = _smallDocuments . Select ( x => new InsertOneModel < BsonDocument > ( x . DeepClone ( ) . AsBsonDocument ) ) . ToList ( ) ;
51
+ _clientBulkWriteInsertModels = _smallDocuments . Select ( x => new BulkWriteInsertOneModel < BsonDocument > ( __collectionNamespace , x . DeepClone ( ) . AsBsonDocument ) ) . ToList ( ) ;
48
52
}
49
53
50
54
[ IterationSetup ]
@@ -61,10 +65,15 @@ public void InsertManySmallBenchmark()
61
65
}
62
66
63
67
[ Benchmark ]
64
- public void SmallDocClientBulkInsertBenchmark ( )
68
+ public void SmallDocCollectionBulkWriteInsertBenchmark ( )
65
69
{
66
- var models = _smallDocuments . Select ( x => new BulkWriteInsertOneModel < BsonDocument > ( __collectionNamespace , x ) ) . ToList ( ) ;
67
- _client . BulkWrite ( models , new ClientBulkWriteOptions ( ) ) ;
70
+ _collection . BulkWrite ( _collectionBulkWriteInsertModels , new BulkWriteOptions ( ) ) ;
71
+ }
72
+
73
+ [ Benchmark ]
74
+ public void SmallDocClientBulkWriteInsertBenchmark ( )
75
+ {
76
+ _client . BulkWrite ( _clientBulkWriteInsertModels , new ClientBulkWriteOptions ( ) ) ;
68
77
}
69
78
70
79
[ GlobalCleanup ]
0 commit comments