Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2016 14:15
Show Gist options
  • Save anonymous/58f61bab4b95279eaa1d1970da8d6920 to your computer and use it in GitHub Desktop.
Save anonymous/58f61bab4b95279eaa1d1970da8d6920 to your computer and use it in GitHub Desktop.
/*
* Copyright 2016 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.
*
*/
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.RawBsonDocument;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
public class HELP2113 {
public static void main(String[] args) throws Exception {
String databaseName = args[1];
String collectionName = args[2];
MongoClient client = new MongoClient(new MongoClientURI(args[0]));
MongoDatabase database = client.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(collectionName);
load(collection);
System.out.println("Warming up...");
for (int i = 0; i < 5; i++) {
dump(collection);
System.out.print('.');
}
System.out.println();
System.out.println("Starting...");
final int runs = 20;
long totalElapsedMillis = 0;
for (int i = 0; i < runs; i++) {
long elapsedMillis = dump(collection);
totalElapsedMillis += elapsedMillis;
System.out.println(elapsedMillis + " ms");
}
System.out.println();
System.out.println("Average: " + totalElapsedMillis / runs + " ms");
System.out.println("All done");
}
private static void load(final MongoCollection<Document> collection) {
long count = collection.count();
if (count != 0) {
System.out.println("Skipping loading phase as collection already contains " + count + " documents");
return;
}
System.out.println("Starting loading phase");
Random random = new Random();
byte[] bytes = new byte[1000];
List<Document> documents = new ArrayList<>(1024);
for (int i = 0; i < 1000 * 1000; i++) {
random.nextBytes(bytes);
documents.add(new Document("_id", i).append("bin", bytes));
if (documents.size() == 1000) {
collection.insertMany(documents);
documents.clear();
}
}
System.out.println("Loaded documents");
}
private static long dump(final MongoCollection<Document> documentCollection) throws IOException, InterruptedException {
File tempFile = File.createTempFile("HELP2113", ".bson", new File("/tmp"));
tempFile.deleteOnExit();
OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile), 1024 * 32);
try {
MongoCollection<RawBsonDocument> collection = documentCollection.withDocumentClass(RawBsonDocument.class);
long start;
BlockingQueue<RawBsonDocument> blockingQueue = new ArrayBlockingQueue<RawBsonDocument>(12000);
AtomicBoolean done = new AtomicBoolean(false);
start = System.nanoTime();
Thread t = new Thread(() -> {
try {
RawBsonDocument cur = blockingQueue.poll(1, MILLISECONDS);
while (cur != null || !done.get()) {
if (cur != null) {
os.write(cur.getByteBuffer().array());
}
cur = blockingQueue.poll(1, MILLISECONDS);
}
} catch (Throwable e) {
e.printStackTrace();
}
});
t.start();
collection.find().batchSize(Integer.MAX_VALUE).forEach((Consumer<RawBsonDocument>) cur -> {
try {
blockingQueue.put(cur);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
done.set(true);
t.join();
return TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS);
} finally {
os.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment