How to Test a MongoDB Cloud Database?

How to Test a MongoDB Cloud Database?

When working with cloud databases, you can have more room to focus on your code since many of the backend processes are handled by the database provider. These include security monitoring, storage allocation, and creating backups of your data. MongoDB’s cloud database, named Atlas, belongs to this category. It is a fully-managed database that allows deployment in more than 90 regions. It can be integrated with major cloud platforms, including Microsoft Azure and Amazon Web Services.

This setup means that performance testing on MongoDB Atlas can be conducted completely on the cloud. Our post about ‘Insights on Cloud-Based Performance Testing’ lists the upsides of this method, such as multi-device support and stability. These benefits are tied to any internet-based dev component, including cloud databases which also have inherent advantages. As outlined in ‘What is a Cloud Database?’ by MongoDB, scalability and infrastructure reliability are among the biggest perks of cloud databases. Making the most out of these qualities means ensuring that your database runs smoothly and efficiently. And this can be achieved with the help of testing.

This article can serve as your guide on testing a MongoDB cloud database.

Prepare tools and designate your test database

Prepare your testing tools. You may use JMeter to test your MongoDB database. Another PerfMatrix post mentioned JMeter as one of the top open-source performance testing tools, and it can cover the purpose of this task. The JSR223 Sampler, a component of JMeter, will be used in the process.

You’ll also need the MongoDB Java Driver. This is a library that lets you manipulate a MongoDB instance, such as when reading and updating documents. Be sure you have the latest version. The driver file must be placed in the lib/ext folder in your JMeter folder.

Next, check your computer’s connection to MongoDB via mongo shell by opening a command window and entering “$ mongo” (without quotes). If you configured an admin account when setting up MongoDB on your server, be sure to log in as the admin user. Use “$ mongo -u admin123 -p –authenticationDatabase login123” and replace admin123 and login123 with your own account details.

Running mongo shell initializes a connection to the test database by default. However, using the default test database is not recommended as it only has a small data set. To get more reliable results from your performance test, it’s advisable to run it on an instance with a considerable volume of data.

To switch to another database, Real Python’s guide on NoSQL databases indicates that you have to enter the “use” command on mongo shell. Input “> use db123” where db123 is the name of your database.

Configure test settings

For the actual test, you need to build a JMeter script that will execute the assessment of your MongoDB cloud database. Begin with inserting the corresponding variables in the Test Plan on JMeter. These include mongoHost, mongoPort, databaseName, and collectionName.

Once that’s done, add a thread group by right-clicking on Test Plan, selecting Add, then Threads (Users), then Thread Group. Specify the series of MongoDB operations you like to test. Examples include ‘connect to database’, ‘create document’, and ‘delete document’. Note that this is a series, so if the previous step encountered an issue, the thread should be aborted. To enable this action, navigate to the “Action to be taken after a Sampler error” setting and select the “Stop Thread” option.

Allow JMeter to access MongoDB

Use JSR223 Sampler to link MongoDB and JMeter. Enter the following line on the testing tool:

importcom.mongodb.client.MongoClients;importcom.mongodb.client.MongoClient;MongoClientmongoClient=MongoClients.create();

 After the connection has been established, JMeter can then access your cloud database instances. Use the following command to execute said operation:

importcom.mongodb.client.MongoCollection;importcom.mongodb.client.MongoDatabase;importorg.bson.Document;MongoDatabasedatabase=mongoClient.getDatabase("db123");MongoCollectioncollection=database.getCollection("collection1");

Replace db123 and collection1 with the names of your MongoDB database and collection.

Build the thread code and run the test

For each MongoDB operation, you have to build a JSR223 code. The tutorial on ‘MongoDB Performance Testing With JMeter’ by DZone details the complete code for each step. For instance, the ‘connect to database’ phase uses the following lines:

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.util.Arrays;

try {
MongoClientSettings settings = MongoClientSettings.builder()
.applyToClusterSettings {builder ->
builder.hosts(Arrays.asList(new ServerAddress(vars.get("mongoHost"),vars.get("mongoPort").toInteger())))}
.build();

MongoClient mongoClient = MongoClients.create(settings);

MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
MongoCollection<Document> collection = database.getCollection(vars.get("collectionName"));

vars.putObject("collection", collection);

return "Connected to " + vars.get("collectionName");
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}

Other operations that are part of your thread, like creating or deleting a document, need their corresponding bits of code. When everything’s set, all that’s left is to run the script and save the results.

Conclusion

Testing your MongoDB cloud database requires a similar approach as with other cloud-based components. Just be sure to double-check your scripts to mitigate any errors that might come up during the process.