I want to insert once and update few times in MongoDb through JMeter. In case it is relevant MOngoDb is running in Docker. I am getting issues during connection but it seems I am trying connecting to MongoDb wrongly.
I am confident that MongoDb is up and running since I can successfuly connect and add data to it from Spring with this parameters:
spring:
data:
mongodb:
host: localhost
port: 27017
database: demodb
authentication-database: admin
username: root
password: rootpassword
I have tried two approaches in JMeter and both are returning errors:
FIRST TENTATIVE)
With Lambda. I read somewhere that lambda doesn't work with Grovy but I am trying to use Java (I know nothing about Grovy). I added this detail here because when I search around people facing issues with "->" in Jmeter the answers are always related to "Grovy doesn't work with Lambda". I ignored this answer since I am using Java. At least, I assume I choose Java properly by right click Thread Group -> Sampler -> JSR 223 Sampler -> under Script Language Box I piccked up java (Bean Shell2.0b6 /Bean Shell Engine 1.0)
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("localhost"),vars.get("27017").toInteger())))}
.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase database = mongoClient.getDatabase(vars.get("demodb"));
MongoCollection<Document> collection = database.getCollection(vars.get("transfers"));
vars.putObject("collection", collection);
Document document = new Document("id", "3")
.append("origem", "Jimis")
.append("destino", "drpc")
.append("status", 1);
collection.insertOne(document);
return "Connected to " + vars.get("transfers");
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}
With this approach I get this error log:
2020-04-03 16:11:23,519 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-04-03 16:11:23,519 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-04-03 16:11:23,520 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-04-03 16:11:23,529 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2020-04-03 16:11:23,529 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2020-04-03 16:11:23,530 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-04-03 16:11:23,530 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2020-04-03 16:11:23,531 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-04-03 16:11:23,533 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-04-03 16:11:23,534 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-03 16:11:23,542 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered ">" at line 10, column 103.
in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 10
javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered ">" at line 10, column 103.
in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 10
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) [ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) [ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) [ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) [ApacheJMeter_core.jar:5.2.1]
at java.lang.Thread.run(Thread.java:834) [?:?]
2020-04-03 16:11:23,544 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2020-04-03 16:11:23,544 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2020-04-03 16:11:23,544 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-04-03 16:11:23,545 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
SECOND TENTATIVE)
Avoiding Lambda just in case it help me move forward.
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 {
MongoClient mongoClient = MongoClients.create("demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true");
MongoDatabase database = mongoClient.getDatabase(vars.get("demodb"));
MongoCollection<Document> collection = database.getCollection(vars.get("transfers"));
vars.putObject("collection", collection);
Document document = new Document("id", "3")
.append("origem", "Jimis")
.append("destino", "drpc")
.append("status", 1);
collection.insertOne(document);*
return "connected";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}
And the logs are:
2020-04-03 16:14:18,684 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-04-03 16:14:18,685 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-04-03 16:14:18,685 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-04-03 16:14:18,746 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2020-04-03 16:14:18,746 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2020-04-03 16:14:18,746 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-04-03 16:14:18,746 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2020-04-03 16:14:18,747 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-04-03 16:14:18,747 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-04-03 16:14:18,747 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-03 16:14:18,752 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered "=" at line 16, column 46.
in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 16
javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered "=" at line 16, column 46.
in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 16
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) [ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) [ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) [ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) [ApacheJMeter_core.jar:5.2.1]
at java.lang.Thread.run(Thread.java:834) [?:?]
2020-04-03 16:14:18,753 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2020-04-03 16:14:18,753 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2020-04-03 16:14:18,753 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-04-03 16:14:18,753 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
In frist tentative it seems it is complaining to use lambda here
MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings {builder -> builder.hosts(Arrays.asList( new ServerAddress(vars.get("localhost"),vars.get("27017").toInteger())))}
.build();
And in second tentative it seems it is complaining of equals signal but I don't think my comprehansion is accurate.
message: javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered "=" at line 16, column 46.
*** Authorization issue (as far as I can see I am using the same configuration successfully logged from Spring to MongoDb
import com.mongodb.*
import com.mongodb.BasicDBObject
import org.bson.*
MongoCredential coreCredential = MongoCredential.createCredential("root", "demodb", "rootpassword".toCharArray());
MongoClient coreMongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(coreCredential));
DB coreDB = coreMongoClient.getDB("demodb");
DBCollection coll = coreDB.getCollection("transfer");
BasicDBObject document = new BasicDBObject("id", "3")
.append("origem", "Jimis")
.append("destino", "drpc")
.append("status", 1);
coreDB.getCollection('transfer').insert(document);
New error:
-06 18:59:39,561 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-06 18:59:39,588 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}
javax.script.ScriptException: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
at javax.script.CompiledScript.eval(CompiledScript.java:89) ~[java.scripting:?]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:5.2.1]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]
PS.: I read somewhere I should update JMeter MongoDb jar so I did (now is mongo-java-driver-2.13.2.jar)
*** Other tentative
When I try
import com.gmongo.*;
import com.mongodb.*;
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
import com.mongodb.DBCollection;
DB db = MongoDBHolder.getDBFromSource("demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true", "demodb");
DBCollection collection = db.getCollection("transfer");
long count = collection.getCount();
String result = String.valueOf(count); // convert long to String
//log.info("Log Resultado:" + result));
SampleResult.setResponseData("SampleResult Resultado: " + result,"UTF-8");
I get
Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: java.lang.IllegalStateException: You didn't define variable:demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true using MongoDB Source Config (property:MongoDB Source)
javax.script.ScriptException: java.lang.IllegalStateException: You didn't define variable:demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true using MongoDB Source Config (property:MongoDB Source)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
It seems it is complaining about MongoDB Source Config. Well searching on Internet I didn't found how to config it in JMeter