0
votes

I want to perform load test on my mongodb database.So I added mongodb driver to bin/ext folder and restarted jmeter. I selected JSR223 Sampler and selected groovy as my language and added connection code as below

import com.mongodb.*

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);
}

mongoHost,mongoPort,databaseName,collectionName are configured in user defined variables(Test Plan). When I run sampler getting error

Thread Name:myThread Group 1-1
Sample Start:2020-01-27 15:57:35 IST
Load time:39
Connect Time:0
Latency:0
Size in bytes:0
Sent bytes:0
Headers size in bytes:0
Body size in bytes:0
Sample Count:1
Error Count:1
Data type ("text"|"bin"|""):text
Response code:500
Response message:javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script3.groovy: 6: unable to resolve class com.mongodb.ServerAddress
 @ line 6, column 1.
   import com.mongodb.ServerAddress;
   ^

Script3.groovy: 5: unable to resolve class com.mongodb.MongoClientSettings
 @ line 5, column 1.
   import com.mongodb.MongoClientSettings;
   ^

Script3.groovy: 10: unable to resolve class org.bson.Document
 @ line 10, column 1.
   import org.bson.Document;
   ^

3 errors



SampleResult fields:
ContentType: 
DataEncoding: null
1

1 Answers

2
votes

You need to add the following libraries to JMeter Classpath:

  1. mongo-java-driver
  2. bson

The libraries version must match (or at least be compatible) with your MongoDB version, it can be obtained by running db.version() query in the MongoDB shell

JMeter restart will be required to pick up the libraries

More information: MongoDB Performance Testing with JMeter