diff --git a/README.md b/README.md index addb037..2a47372 100755 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ java -cp ./latest-version/mongodb-performance-test.jar de.idealo.mongodb.perf.Ma This will print a description how to use the program: ``` usage: de.idealo.mongodb.perf.Main [-H] [-v] [-m ] [-o ] [-t ] [-d ] [-dropdb] [-s ] [-h ] - [-port ] [-db ] [-c ] [-url ] [-u ] [-p ] [-adb ] [-ssl] + [-port ] [-db ] [-c ] [-url ] [-u ] [-p ] [-adb ] [-ssl] [-wc ] *** mongoDB performance test (version 1.1.3)*** Please run first mode=INSERT in order to have a non-empty collection to test on. You may add option 'dropdb' in order to drop the database before inserting documents. @@ -83,6 +83,7 @@ Options: -p,--password mongoDB password -adb,--authdb mongoDB database to be authenticated against (default: value of parameter -db) -ssl,--ssl use SSL to connect to mongoDB + -wc, --writeconcern write concern for the operations (default: ACKNOWLEDGED) (options: ACKNOWLEDGED, UNACKNOWLEDGED, JOURNALED, MAJORITY) ``` ### Some examples @@ -184,6 +185,8 @@ For example, 50 threads inserted a total of 1 million of documents, so the range ## Version history +* v1.2.0 + + improvement: added `--writeconcern` option to pass [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) (ACKNOWLEDGED, UNACKNOWLEDGED, JOURNALED, MAJORITY) to the operations. * v1.1.4 + improvement: replace `logback-classic` v1.4.6 by v1.4.12 to close a potential security vulnerability * v1.1.3 diff --git a/latest-version/mongodb-performance-test.jar b/latest-version/mongodb-performance-test.jar index edee5b3..c9a42df 100644 Binary files a/latest-version/mongodb-performance-test.jar and b/latest-version/mongodb-performance-test.jar differ diff --git a/src/main/java/de/idealo/mongodb/perf/Main.java b/src/main/java/de/idealo/mongodb/perf/Main.java index 466ac56..4dabf29 100644 --- a/src/main/java/de/idealo/mongodb/perf/Main.java +++ b/src/main/java/de/idealo/mongodb/perf/Main.java @@ -1,6 +1,7 @@ package de.idealo.mongodb.perf; import com.mongodb.ServerAddress; +import com.mongodb.WriteConcern; import de.idealo.mongodb.perf.operations.*; import org.apache.commons.cli.*; import org.slf4j.Logger; @@ -40,6 +41,7 @@ public class Main { boolean dropDb = false; private final String version; private int randomFieldLength = 0; + private WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED; public Main(){ version = getClass().getPackage().getImplementationVersion(); @@ -127,7 +129,6 @@ private void validateInput(String... args){ } } - if (cmdLine.hasOption("t")) { final String[] t_arg = cmdLine.getOptionValues("t"); for (int i = 0; i < t_arg.length; i++) { @@ -166,7 +167,25 @@ private void validateInput(String... args){ } } - + if (cmdLine.hasOption("writeconcern")) { + String wcOption = cmdLine.getOptionValue("writeconcern").toUpperCase(); + switch (wcOption) { + case "ACKNOWLEDGED": + writeConcern = WriteConcern.ACKNOWLEDGED; + break; + case "UNACKNOWLEDGED": + writeConcern = WriteConcern.UNACKNOWLEDGED; + break; + case "JOURNALED": + writeConcern = WriteConcern.JOURNALED; + break; + case "MAJORITY": + writeConcern = WriteConcern.MAJORITY; + break; + default: + throw new IllegalArgumentException("Invalid WriteConcern value: " + wcOption); + } + } } catch (Exception e) { LOG.error(e.getMessage()); @@ -263,15 +282,25 @@ private static Options cliOptions() { .addOption(Option.builder("p").longOpt("password").hasArg().argName("PASSWORD").desc("mongoDB password").build()) .addOption(Option.builder("adb").longOpt("authdb").hasArg().argName("AUTH_DB").desc("mongoDB database to be authenticated against (default: value of parameter -db)").build()) .addOption(new Option("ssl", "ssl", false, "use SSL to connect to mongoDB")) - ; - + .addOption(Option.builder("wc").longOpt("writeconcern").hasArg().argName("WRITE_CONCERN") + .desc("WriteConcern for MongoDB operations (ACKNOWLEDGED, UNACKNOWLEDGED, JOURNALED, MAJORITY)") + .build()); return options; } private void executeOperations() { final ServerAddress serverAddress = new ServerAddress(host, port); - final MongoDbAccessor mongoDbAccessor = new MongoDbAccessor(user, password, authDb, ssl, url,serverAddress); + final MongoDbAccessor mongoDbAccessor = new MongoDbAccessor( + -1, // socketTimeOut + user, + password, + authDb, + ssl, + url, + writeConcern, + serverAddress + ); int run=0; CountDownLatch runModeLatch = new CountDownLatch(modes.size()); @@ -336,7 +365,6 @@ private void executeOperations() { executor.shutdown(); mongoDbAccessor.closeConnections(); } - } public static void main(String... args){ @@ -344,6 +372,4 @@ public static void main(String... args){ m.validateInput(args); m.executeOperations(); } - - -} +} \ No newline at end of file diff --git a/src/main/java/de/idealo/mongodb/perf/MongoDbAccessor.java b/src/main/java/de/idealo/mongodb/perf/MongoDbAccessor.java index b1b0188..bde24d2 100644 --- a/src/main/java/de/idealo/mongodb/perf/MongoDbAccessor.java +++ b/src/main/java/de/idealo/mongodb/perf/MongoDbAccessor.java @@ -11,9 +11,6 @@ import java.net.UnknownHostException; -/** - * Created by kay.agahd on 23.11.16. - */ public class MongoDbAccessor { private static final Logger LOG = LoggerFactory.getLogger(MongoDbAccessor.class); @@ -25,27 +22,29 @@ public class MongoDbAccessor { private final String pw; private final String authDb; private final boolean ssl; + private final WriteConcern writeConcern; private MongoClient mongo; private MongoDbAccessor() { - this(-1, null, null, null, false, null); + this(-1, null, null, null, false, null, WriteConcern.ACKNOWLEDGED); }; public MongoDbAccessor(String user, String pw, String authDb, boolean ssl, String url, ServerAddress... serverAddress) { - this(-1, user, pw, authDb, ssl, url, serverAddress); - } - - public MongoDbAccessor(int socketTimeOut, String user, String pw, String authDb, boolean ssl, String url, ServerAddress... serverAddress) { - this.serverAddress = serverAddress; - this.user = user; - this.pw = pw; - this.authDb = authDb != null && !authDb.isEmpty() ? authDb : "admin"; - this.ssl = ssl; - this.url = url; + this(-1, user, pw, authDb, ssl, url, WriteConcern.ACKNOWLEDGED, serverAddress); + } + + public MongoDbAccessor(int socketTimeOut, String user, String pw, String authDb, boolean ssl, String url, WriteConcern writeConcern, ServerAddress... serverAddress) { + this.serverAddress = serverAddress; + this.user = user; + this.pw = pw; + this.authDb = authDb != null && !authDb.isEmpty() ? authDb : "admin"; + this.ssl = ssl; + this.url = url; this.socketTimeOut = socketTimeOut; - init(); - } - + this.writeConcern = writeConcern != null ? writeConcern : WriteConcern.ACKNOWLEDGED; // Use default if null + init(); + } + public MongoDatabase getMongoDatabase(String dbName) { if (mongo == null) @@ -57,15 +56,9 @@ public MongoDatabase getMongoDatabase(String dbName) { public void init() { LOG.info(">>> init {}", serverAddress); try { - MongoClientOptions options = MongoClientOptions.builder().connectTimeout(1000 * 10). // fail fast, so we - // know this node is - // unavailable - // maxConnectionIdleTime(1000 * 60). - // maxConnectionLifeTime(1000 * 60). - // socketTimeout(socketTimeOut == -1 ? 1000 * 120 : socketTimeOut). // use - // default (no timeout) - readPreference(ReadPreference.secondaryPreferred()).connectionsPerHost(5000) - .threadsAllowedToBlockForConnectionMultiplier(10).writeConcern(WriteConcern.ACKNOWLEDGED) + MongoClientOptions options = MongoClientOptions.builder().connectTimeout(1000 * 10) // fail fast, so we know this node is unavailable + .readPreference(ReadPreference.secondaryPreferred()).connectionsPerHost(5000) + .threadsAllowedToBlockForConnectionMultiplier(10).writeConcern(writeConcern) // Use configurable WriteConcern .sslEnabled(ssl).sslInvalidHostNameAllowed(true).build(); if (url != null && !url.isEmpty()) {