I have to fetch records from mongoDB using multiple threads and pass a different offset each time with limit as 100 to fetch 100 records eachtime. The following is the code that I've used in JSR223 Sampler with Groovy as the language selected. The problem faced here is that the StringBuilder takes too much time to parse the response, because of which the response time takes a hit.

Is there a better alternative as this approach doesn't seem right?

DB db = MongoDBHolder.getDBFromSource("<sourceConfigName>", "<db_name>");

DBCollection coll = db.getCollection("collection_name");
BasicDBObject query = new BasicDBObject("1st_field", "1st_value").append("2nd_field","2nd_value");

DBCursor cursor = coll.find(query).skip(${offset}).limit(100);

StringBuilder resultBuilder = new StringBuilder();
while (cursor.hasNext()) {
 DBObject result = cursor.next();

1 Answer 1


I can make only 2 assumptions:

  1. The slow down is due to inlining ${offset} variable into the script. You should replace it with vars.get("offset"). Inlining variables disables compilation caching and scripts execute much longer. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! for more detailed explanations and scripting best practices
  2. Possible network issues (most likely). Check it as follows:

    • Perform the query on the host where MongoDB runs via Mongo Shell
    • Do the same from the host where JMeter lives
    • Measure the differences and compare them to each other and to JMeter timings

StringBuilder class is quite fast, it is absolutely OK to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.