SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, tutorials, tools and downloads for SQL Database Programmers, Developers, Windows and Office Users

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.

download MongoDB JDBC driver to use with JMeter

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

MongoDB JDBC driver in JMeter lib folder

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.

JMeter test plan structure for MongoDB database connection

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.

Groovy scripting language for JMeter JSR223 Sampler custom code development

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.

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;
import java.security.SecureRandom;

// MongoDB database connection details
def mongoHost = "localhost"
def mongoPort = 27017
def databaseName = "cdaas"
def collectionName = "customer"

// Combine numbers and letters character sets for building random strings
def rndChars = numbers + letters
def random = new SecureRandom()
def numbers = "1234567890"
def letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

try {
   def settings = MongoClientSettings.builder()
      .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress(mongoHost, mongoPort))))

   def mongoClient = MongoClients.create(settings)

   def database = mongoClient.getDatabase(databaseName)
   def collection = database.getCollection(collectionName)

   // Insert statically build document to target collection
   def document = new Document("9999", "kodyaz")

   // Insert dynamically build documents with random string to target collection
   def counter = 10001
   50.times {
      // build a random string containing 20 characters
      def randomString = (1..20).collect { rndChars.charAt(random.nextInt(rndChars.length())) }.join()
      document = new Document(counter.toString(), randomString)

} catch (Exception e) {
   println "Error: $e"
} finally {
   if (mongoClient != null) {
      // Close any open database connection
Groovy script code to connect MongoDB database in Apache JMeter Test Plan

You can also define variables for database connection string in your test plan, and modify the code to use them

user defined variables in JMeter

def mongoHost = vars.getObject("mongoHost")
def mongoPort = Integer.parseInt( vars.getObject("mongoPort") )
def databaseName = vars.getObject("databaseName")
def collectionName = vars.getObject("collectionName")
Replace static database connection details with JMeter user defined variables

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

MongoDB Compass GUI tool

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.

Copyright © 2004 - 2024 Eralper YILMAZ. All rights reserved.