Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BulkWrite Benchmarks #1657

Merged
merged 12 commits into from
Mar 28, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016-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.
*
*/

package com.mongodb.benchmark.benchmarks;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public abstract class AbstractCollectionWriteBenchmark<T> extends AbstractWriteBenchmark<T> {

protected MongoCollection<T> collection;
protected MongoDatabase database;

private final String name;
private final Class<T> clazz;

protected AbstractCollectionWriteBenchmark(final String name,
final String resourcePath,
int numIterations,
int numDocuments,
final Class<T> clazz) {
super(name, resourcePath, numIterations, numDocuments, clazz);
this.name = name;
this.clazz = clazz;
}

@Override
public void setUp() throws Exception {
super.setUp();
database = client.getDatabase(DATABASE_NAME);
collection = database.getCollection(COLLECTION_NAME, clazz);
database.drop();
}

@Override
public void before() throws Exception {
super.before();
collection.drop();
}

@Override
public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.mongodb.benchmark.benchmarks;

import com.mongodb.MongoNamespace;
import com.mongodb.benchmark.framework.Benchmark;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
Expand All @@ -33,6 +34,8 @@ public abstract class AbstractMongoBenchmark extends Benchmark {

protected static final String DATABASE_NAME = "perftest";
protected static final String COLLECTION_NAME = "corpus";
protected static final MongoNamespace NAMESPACE = new MongoNamespace(
AbstractMongoBenchmark.DATABASE_NAME, AbstractMongoBenchmark.COLLECTION_NAME);


protected MongoClient client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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
Expand All @@ -17,62 +17,58 @@

package com.mongodb.benchmark.benchmarks;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.conversions.Bson;
import org.bson.json.JsonReader;

import java.nio.charset.StandardCharsets;

public abstract class AbstractInsertBenchmark<T> extends AbstractMongoBenchmark {

protected MongoCollection<T> collection;

public abstract class AbstractWriteBenchmark<T> extends AbstractMongoBenchmark {
protected static final Bson EMPTY_FILTER = Filters.empty();
private final String name;
private final String resourcePath;
private final Class<T> clazz;
private byte[] bytes;
protected int fileLength;
protected T document;

protected AbstractInsertBenchmark(final String name, final String resourcePath, final Class<T> clazz) {
protected int numInternalIterations;
protected int numDocuments;

protected AbstractWriteBenchmark(final String name,
final String resourcePath,
int numInternalIterations,
int numDocuments,
final Class<T> clazz) {
this.name = name;
this.resourcePath = resourcePath;
this.clazz = clazz;
this.numInternalIterations = numInternalIterations;
this.numDocuments = numDocuments;
}

@Override
public void setUp() throws Exception {
super.setUp();
MongoDatabase database = client.getDatabase(DATABASE_NAME);

collection = database.getCollection(COLLECTION_NAME, clazz);

database.drop();
bytes = readAllBytesFromRelativePath(resourcePath);

fileLength = bytes.length;

Codec<T> codec = collection.getCodecRegistry().get(clazz);

Codec<T> codec = client.getCodecRegistry().get(clazz);
document = codec.decode(new JsonReader(new String(bytes, StandardCharsets.UTF_8)), DecoderContext.builder().build());
}

@Override
public void before() throws Exception {
super.before();
collection.drop();
}

@Override
public String getName() {
return name;
}

protected T createDocument() {
Codec<T> codec = collection.getCodecRegistry().get(clazz);

Codec<T> codec = client.getCodecRegistry().get(clazz);
return codec.decode(new JsonReader(new String(bytes, StandardCharsets.UTF_8)), DecoderContext.builder().build());
}

@Override
public int getBytesPerRun() {
return fileLength * numInternalIterations * numDocuments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package com.mongodb.benchmark.benchmarks;

import com.mongodb.benchmark.benchmarks.bulk.ClientBulkWriteBenchmark;
import com.mongodb.benchmark.benchmarks.bulk.CollectionBulkWriteBenchmark;
import com.mongodb.benchmark.benchmarks.bulk.MixedClientBulkWriteBenchmark;
import com.mongodb.benchmark.benchmarks.bulk.MixedCollectionBulkWriteBenchmark;
import com.mongodb.benchmark.framework.Benchmark;
import com.mongodb.benchmark.framework.BenchmarkResult;
import com.mongodb.benchmark.framework.BenchmarkResultWriter;
Expand Down Expand Up @@ -70,17 +74,32 @@ private static void runBenchmarks()
runBenchmark(new RunCommandBenchmark<>(DOCUMENT_CODEC));
runBenchmark(new FindOneBenchmark<Document>("single_and_multi_document/tweet.json", BenchmarkSuite.DOCUMENT_CLASS));

runBenchmark(new InsertOneBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10000,
runBenchmark(new InsertOneBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10_000,
DOCUMENT_CLASS, ID_REMOVER));
runBenchmark(new InsertOneBenchmark<Document>("Large", "./single_and_multi_document/large_doc.json", 10,
DOCUMENT_CLASS, ID_REMOVER));

runBenchmark(new FindManyBenchmark<Document>("single_and_multi_document/tweet.json", BenchmarkSuite.DOCUMENT_CLASS));
runBenchmark(new InsertManyBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10000,
runBenchmark(new InsertManyBenchmark<Document>("Small", "./single_and_multi_document/small_doc.json", 10_000,
DOCUMENT_CLASS));
runBenchmark(new InsertManyBenchmark<Document>("Large", "./single_and_multi_document/large_doc.json", 10,
DOCUMENT_CLASS));

runBenchmark(new CollectionBulkWriteBenchmark<>("Small", "./single_and_multi_document/small_doc.json", 10_000,
DOCUMENT_CLASS));
runBenchmark(new CollectionBulkWriteBenchmark<>("Large", "./single_and_multi_document/large_doc.json", 10,
DOCUMENT_CLASS));

runBenchmark(new ClientBulkWriteBenchmark<>("Small", "./single_and_multi_document/small_doc.json", 10_000,
DOCUMENT_CLASS));
runBenchmark(new ClientBulkWriteBenchmark<>("Large", "./single_and_multi_document/large_doc.json", 10,
DOCUMENT_CLASS));

runBenchmark(new MixedCollectionBulkWriteBenchmark<>("./single_and_multi_document/small_doc.json", 10_000,
DOCUMENT_CLASS));
runBenchmark(new MixedClientBulkWriteBenchmark<>("./single_and_multi_document/small_doc.json", 10_000,
DOCUMENT_CLASS));

runBenchmark(new GridFSUploadBenchmark("single_and_multi_document/gridfs_large.bin"));
runBenchmark(new GridFSDownloadBenchmark("single_and_multi_document/gridfs_large.bin"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import java.util.ArrayList;
import java.util.List;

public class InsertManyBenchmark<T> extends AbstractInsertBenchmark<T> {
private final int numDocuments;
public class InsertManyBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
private final List<T> documentList;

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

Expand All @@ -48,9 +46,4 @@ public void before() throws Exception {
public void run() {
collection.insertMany(documentList);
}

@Override
public int getBytesPerRun() {
return fileLength * numDocuments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package com.mongodb.benchmark.benchmarks;

public class InsertOneBenchmark<T> extends AbstractInsertBenchmark<T> {
public class InsertOneBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
private final int numIterations;
private final IdRemover<T> idRemover;

public InsertOneBenchmark(final String name, final String resourcePath, final int numIterations, final Class<T> clazz,
final IdRemover<T> idRemover) {
super(name + " doc insertOne", resourcePath, clazz);
super(name + " doc insertOne", resourcePath, numIterations, 1, clazz);
this.numIterations = numIterations;
this.idRemover = idRemover;
}
Expand All @@ -35,10 +35,4 @@ public void run() {
collection.insertOne(document);
}
}

@Override
public int getBytesPerRun() {
return fileLength * numIterations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2016-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.
*
*/

package com.mongodb.benchmark.benchmarks.bulk;

import com.mongodb.benchmark.benchmarks.AbstractCollectionWriteBenchmark;
import com.mongodb.client.model.bulk.ClientNamespacedInsertOneModel;
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel;

import java.util.ArrayList;
import java.util.List;

public class ClientBulkWriteBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
private final List<ClientNamespacedInsertOneModel> modelList;

public ClientBulkWriteBenchmark(final String name, final String resourcePath, final int numDocuments, final Class<T> clazz) {
super(name + " doc Client BulkWrite insert", resourcePath, 1, numDocuments, clazz);
modelList = new ArrayList<>(numDocuments);
}

@Override
public void before() throws Exception {
super.before();
database.createCollection(COLLECTION_NAME);

modelList.clear();
for (int i = 0; i < numDocuments; i++) {
modelList.add(ClientNamespacedWriteModel.insertOne(NAMESPACE, createDocument()));
}
}

@Override
public void run() {
client.bulkWrite(modelList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016-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.
*
*/

package com.mongodb.benchmark.benchmarks.bulk;

import com.mongodb.benchmark.benchmarks.AbstractCollectionWriteBenchmark;
import com.mongodb.client.model.InsertOneModel;

import java.util.ArrayList;
import java.util.List;

public class CollectionBulkWriteBenchmark<T> extends AbstractCollectionWriteBenchmark<T> {
private final List<InsertOneModel<T>> modelList;

public CollectionBulkWriteBenchmark(final String name, final String resourcePath, final int numDocuments, final Class<T> clazz) {
super(name + " doc Collection BulkWrite insert", resourcePath, 1, numDocuments, clazz);
modelList = new ArrayList<>(numDocuments);
}

@Override
public void before() throws Exception {
super.before();
database.createCollection(COLLECTION_NAME);
modelList.clear();
for (int i = 0; i < numDocuments; i++) {
modelList.add(new InsertOneModel<>((createDocument())));
}
}

@Override
public void run() {
collection.bulkWrite(modelList);
}
}
Loading