Skip to content

Commit e1eb156

Browse files
authored
Add BulkWrite Benchmarks (#1657)
JAVA-5545
1 parent 68b9bf1 commit e1eb156

10 files changed

+349
-45
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2016-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.benchmark.benchmarks;
19+
20+
import com.mongodb.client.MongoCollection;
21+
import com.mongodb.client.MongoDatabase;
22+
23+
public abstract class AbstractCollectionWriteBenchmark<T> extends AbstractWriteBenchmark<T> {
24+
25+
protected MongoCollection<T> collection;
26+
protected MongoDatabase database;
27+
28+
private final String name;
29+
private final Class<T> clazz;
30+
31+
protected AbstractCollectionWriteBenchmark(final String name,
32+
final String resourcePath,
33+
int numIterations,
34+
int numDocuments,
35+
final Class<T> clazz) {
36+
super(name, resourcePath, numIterations, numDocuments, clazz);
37+
this.name = name;
38+
this.clazz = clazz;
39+
}
40+
41+
@Override
42+
public void setUp() throws Exception {
43+
super.setUp();
44+
database = client.getDatabase(DATABASE_NAME);
45+
collection = database.getCollection(COLLECTION_NAME, clazz);
46+
database.drop();
47+
}
48+
49+
@Override
50+
public void before() throws Exception {
51+
super.before();
52+
collection.drop();
53+
}
54+
55+
@Override
56+
public String getName() {
57+
return name;
58+
}
59+
}

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/AbstractMongoBenchmark.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.mongodb.benchmark.benchmarks;
1919

20+
import com.mongodb.MongoNamespace;
2021
import com.mongodb.benchmark.framework.Benchmark;
2122
import com.mongodb.client.MongoClient;
2223
import com.mongodb.client.MongoClients;
@@ -33,6 +34,8 @@ public abstract class AbstractMongoBenchmark extends Benchmark {
3334

3435
protected static final String DATABASE_NAME = "perftest";
3536
protected static final String COLLECTION_NAME = "corpus";
37+
protected static final MongoNamespace NAMESPACE = new MongoNamespace(
38+
AbstractMongoBenchmark.DATABASE_NAME, AbstractMongoBenchmark.COLLECTION_NAME);
3639

3740

3841
protected MongoClient client;

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/AbstractInsertBenchmark.java renamed to driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/AbstractWriteBenchmark.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
@@ -17,62 +17,58 @@
1717

1818
package com.mongodb.benchmark.benchmarks;
1919

20-
import com.mongodb.client.MongoCollection;
21-
import com.mongodb.client.MongoDatabase;
20+
import com.mongodb.client.model.Filters;
2221
import org.bson.codecs.Codec;
2322
import org.bson.codecs.DecoderContext;
23+
import org.bson.conversions.Bson;
2424
import org.bson.json.JsonReader;
2525

2626
import java.nio.charset.StandardCharsets;
2727

28-
public abstract class AbstractInsertBenchmark<T> extends AbstractMongoBenchmark {
29-
30-
protected MongoCollection<T> collection;
31-
28+
public abstract class AbstractWriteBenchmark<T> extends AbstractMongoBenchmark {
29+
protected static final Bson EMPTY_FILTER = Filters.empty();
3230
private final String name;
3331
private final String resourcePath;
3432
private final Class<T> clazz;
3533
private byte[] bytes;
3634
protected int fileLength;
3735
protected T document;
38-
39-
protected AbstractInsertBenchmark(final String name, final String resourcePath, final Class<T> clazz) {
36+
protected int numInternalIterations;
37+
protected int numDocuments;
38+
39+
protected AbstractWriteBenchmark(final String name,
40+
final String resourcePath,
41+
int numInternalIterations,
42+
int numDocuments,
43+
final Class<T> clazz) {
4044
this.name = name;
4145
this.resourcePath = resourcePath;
4246
this.clazz = clazz;
47+
this.numInternalIterations = numInternalIterations;
48+
this.numDocuments = numDocuments;
4349
}
4450

4551
@Override
4652
public void setUp() throws Exception {
4753
super.setUp();
48-
MongoDatabase database = client.getDatabase(DATABASE_NAME);
49-
50-
collection = database.getCollection(COLLECTION_NAME, clazz);
51-
52-
database.drop();
5354
bytes = readAllBytesFromRelativePath(resourcePath);
54-
5555
fileLength = bytes.length;
56-
57-
Codec<T> codec = collection.getCodecRegistry().get(clazz);
58-
56+
Codec<T> codec = client.getCodecRegistry().get(clazz);
5957
document = codec.decode(new JsonReader(new String(bytes, StandardCharsets.UTF_8)), DecoderContext.builder().build());
6058
}
6159

62-
@Override
63-
public void before() throws Exception {
64-
super.before();
65-
collection.drop();
66-
}
67-
6860
@Override
6961
public String getName() {
7062
return name;
7163
}
7264

7365
protected T createDocument() {
74-
Codec<T> codec = collection.getCodecRegistry().get(clazz);
75-
66+
Codec<T> codec = client.getCodecRegistry().get(clazz);
7667
return codec.decode(new JsonReader(new String(bytes, StandardCharsets.UTF_8)), DecoderContext.builder().build());
7768
}
69+
70+
@Override
71+
public int getBytesPerRun() {
72+
return fileLength * numInternalIterations * numDocuments;
73+
}
7874
}

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/BenchmarkSuite.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package com.mongodb.benchmark.benchmarks;
1919

20+
import com.mongodb.benchmark.benchmarks.bulk.ClientBulkWriteBenchmark;
21+
import com.mongodb.benchmark.benchmarks.bulk.CollectionBulkWriteBenchmark;
22+
import com.mongodb.benchmark.benchmarks.bulk.MixedClientBulkWriteBenchmark;
23+
import com.mongodb.benchmark.benchmarks.bulk.MixedCollectionBulkWriteBenchmark;
2024
import com.mongodb.benchmark.framework.Benchmark;
2125
import com.mongodb.benchmark.framework.BenchmarkResult;
2226
import com.mongodb.benchmark.framework.BenchmarkResultWriter;
@@ -70,17 +74,32 @@ private static void runBenchmarks()
7074
runBenchmark(new RunCommandBenchmark<>(DOCUMENT_CODEC));
7175
runBenchmark(new FindOneBenchmark<Document>("single_and_multi_document/tweet.json", BenchmarkSuite.DOCUMENT_CLASS));
7276

73-
runBenchmark(new InsertOneBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10000,
77+
runBenchmark(new InsertOneBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10_000,
7478
DOCUMENT_CLASS, ID_REMOVER));
7579
runBenchmark(new InsertOneBenchmark<Document>("Large", "./single_and_multi_document/large_doc.json", 10,
7680
DOCUMENT_CLASS, ID_REMOVER));
7781

7882
runBenchmark(new FindManyBenchmark<Document>("single_and_multi_document/tweet.json", BenchmarkSuite.DOCUMENT_CLASS));
79-
runBenchmark(new InsertManyBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10000,
83+
runBenchmark(new InsertManyBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10_000,
8084
DOCUMENT_CLASS));
8185
runBenchmark(new InsertManyBenchmark<Document>("Large", "./single_and_multi_document/large_doc.json", 10,
8286
DOCUMENT_CLASS));
8387

88+
runBenchmark(new CollectionBulkWriteBenchmark<>("Small", "./single_and_multi_document/small_doc.json", 10_000,
89+
DOCUMENT_CLASS));
90+
runBenchmark(new CollectionBulkWriteBenchmark<>("Large", "./single_and_multi_document/large_doc.json", 10,
91+
DOCUMENT_CLASS));
92+
93+
runBenchmark(new ClientBulkWriteBenchmark<>("Small", "./single_and_multi_document/small_doc.json", 10_000,
94+
DOCUMENT_CLASS));
95+
runBenchmark(new ClientBulkWriteBenchmark<>("Large", "./single_and_multi_document/large_doc.json", 10,
96+
DOCUMENT_CLASS));
97+
98+
runBenchmark(new MixedCollectionBulkWriteBenchmark<>("./single_and_multi_document/small_doc.json", 10_000,
99+
DOCUMENT_CLASS));
100+
runBenchmark(new MixedClientBulkWriteBenchmark<>("./single_and_multi_document/small_doc.json", 10_000,
101+
DOCUMENT_CLASS));
102+
84103
runBenchmark(new GridFSUploadBenchmark("single_and_multi_document/gridfs_large.bin"));
85104
runBenchmark(new GridFSDownloadBenchmark("single_and_multi_document/gridfs_large.bin"));
86105

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/InsertManyBenchmark.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23-
public class InsertManyBenchmark<T> extends AbstractInsertBenchmark<T> {
24-
private final int numDocuments;
23+
public class InsertManyBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
2524
private final List<T> documentList;
2625

2726
public InsertManyBenchmark(final String name, final String resourcePath, final int numDocuments, final Class<T> clazz) {
28-
super(name + " doc bulk insert", resourcePath, clazz);
29-
this.numDocuments = numDocuments;
27+
super(name + " doc bulk insert", resourcePath, 1, numDocuments, clazz);
3028
documentList = new ArrayList<>(numDocuments);
3129
}
3230

@@ -48,9 +46,4 @@ public void before() throws Exception {
4846
public void run() {
4947
collection.insertMany(documentList);
5048
}
51-
52-
@Override
53-
public int getBytesPerRun() {
54-
return fileLength * numDocuments;
55-
}
5649
}

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/InsertOneBenchmark.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
package com.mongodb.benchmark.benchmarks;
1919

20-
public class InsertOneBenchmark<T> extends AbstractInsertBenchmark<T> {
20+
public class InsertOneBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
2121
private final int numIterations;
2222
private final IdRemover<T> idRemover;
2323

2424
public InsertOneBenchmark(final String name, final String resourcePath, final int numIterations, final Class<T> clazz,
2525
final IdRemover<T> idRemover) {
26-
super(name + " doc insertOne", resourcePath, clazz);
26+
super(name + " doc insertOne", resourcePath, numIterations, 1, clazz);
2727
this.numIterations = numIterations;
2828
this.idRemover = idRemover;
2929
}
@@ -35,10 +35,4 @@ public void run() {
3535
collection.insertOne(document);
3636
}
3737
}
38-
39-
@Override
40-
public int getBytesPerRun() {
41-
return fileLength * numIterations;
42-
}
43-
4438
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2016-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.benchmark.benchmarks.bulk;
19+
20+
import com.mongodb.benchmark.benchmarks.AbstractCollectionWriteBenchmark;
21+
import com.mongodb.client.model.bulk.ClientNamespacedInsertOneModel;
22+
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class ClientBulkWriteBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
28+
private final List<ClientNamespacedInsertOneModel> modelList;
29+
30+
public ClientBulkWriteBenchmark(final String name, final String resourcePath, final int numDocuments, final Class<T> clazz) {
31+
super(name + " doc Client BulkWrite insert", resourcePath, 1, numDocuments, clazz);
32+
modelList = new ArrayList<>(numDocuments);
33+
}
34+
35+
@Override
36+
public void before() throws Exception {
37+
super.before();
38+
database.createCollection(COLLECTION_NAME);
39+
40+
modelList.clear();
41+
for (int i = 0; i < numDocuments; i++) {
42+
modelList.add(ClientNamespacedWriteModel.insertOne(NAMESPACE, createDocument()));
43+
}
44+
}
45+
46+
@Override
47+
public void run() {
48+
client.bulkWrite(modelList);
49+
}
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.benchmark.benchmarks.bulk;
19+
20+
import com.mongodb.benchmark.benchmarks.AbstractCollectionWriteBenchmark;
21+
import com.mongodb.client.model.InsertOneModel;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
public class CollectionBulkWriteBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
27+
private final List<InsertOneModel<T>> modelList;
28+
29+
public CollectionBulkWriteBenchmark(final String name, final String resourcePath, final int numDocuments, final Class<T> clazz) {
30+
super(name + " doc Collection BulkWrite insert", resourcePath, 1, numDocuments, clazz);
31+
modelList = new ArrayList<>(numDocuments);
32+
}
33+
34+
@Override
35+
public void before() throws Exception {
36+
super.before();
37+
database.createCollection(COLLECTION_NAME);
38+
modelList.clear();
39+
for (int i = 0; i < numDocuments; i++) {
40+
modelList.add(new InsertOneModel<>((createDocument())));
41+
}
42+
}
43+
44+
@Override
45+
public void run() {
46+
collection.bulkWrite(modelList);
47+
}
48+
}

0 commit comments

Comments
 (0)