JMeter MongoDB Connection for Test Plan
This Apache JMeter guide will show performance testers how they can access MongoDB databases or Amazon DocumentDB clusters from their test plans using JSR223 Sampler. JSR223 Sampler enables JMeter users to use custom scripts to connect various data platforms using JDBC drivers and execute database commands like to insert data, read data to perform database operations like read and write for measuring the performance of target database solutions.
The first thing you can do to start your MongoDB connection task using Apache JMeter is to download most recent MongoDB JDBC driver which will enable us to connect to the MongoDB database.
Although JMeter is shipped with a default MongoDB JDBC driver, I will suggest to modify it with most recent database driver or in case of Amazon DocumentDB, using the appropriate native driver instead of using a compatible database JDBC driver.
Let's download JDBC driver for MongoDB database
You can download from https://repo1.maven.org/maven2/org/mongodb/mongodb-jdbc/
I downloaded mongodb-jdbc-2.1.1-all.jar to test MongoDB connection using JSR223 Sampler within my JMeter test plan.
JMeter keeps the JDBC drivers to use for various database connections within the lib folder under its installation directory.
Remove the default MongoDB driver shipped with Jmeter which is located under the lib folder of JMeter
Copy the new MongoDB JDBC driver .jar file into lib folder of JMeter
You need to restart Jmeter after you add new and remove previous MongoDB JDBC driver files so that Jmeter can use the latest files within its lib folder for a successful database connection.
Now in JMeter application, create a new Test Plan
Add a Thread Group
Under thread group add JSR223 Sampler
JSR223 aka JSR-223 stands for Java Specification Request #223 and contains Scripting for the Java Platform framework specification for embedding scripts in Java source code.
As you can guess using JSR223 sampler developers can provide scripting source code that will help us reach our test plan.
Finally, add listeners like "View Results Tree" and "View Results in Table" to see outcome of execution of the test scripts.
You can select groovy as the script language. Groovy is used frequently as scripting language in JMeter where customized test plans are required for performance measures.
When you are ready with test plan structure, copy groovy code shared below into the script area on JSR223 Sampler.
Shared groovy code first builds a MongoClient object.
Then points to the target database and collection.
After this point, you will see two insert commands to the related collection. Just a info note if collection term is new to you, collection in NoSQL databases is similar to a table in a relational database.
First insert command will add given static values to the MongoDB collection.
Second insert command will add randomly created string values as JSON documents to the same collection.
You can also define variables for database connection string in your test plan, and modify the code to use them
If you have already installed MongoDB Compass or another MongoDB tool, you can connect and validate that the sample data is generated from Jmeter successfully
Compass is a graphical user interface aka GUI tool to manage your MongoDB database operations. If you don't have MongoDB Compass installed on your computer, you can download MongoDB Compass by following the URL. This MongoDB DBMS tool will enable developers and administrators to connect to MongoDB databases, manage them and execute queries in short.
I hope this JMeter guide will help testers who want to connect MongoDB databases and execute their test plans for measuring database or application performance or for creating work load on MongoDB.