0
votes

I'm trying to connect to MongoDB in Jmeter by using JSR223 Sampler. Here is my code:


import com.mongodb.MongoClientURI;
import com.mongodb.MongoClient;
import org.bson.Document;


import com.mongodb.BasicDBObject;
import com.mongodb.ConnectionString;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;


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 java.util.Arrays;


import com.mongodb.Cursor;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

try {


     MongoClientURI connectionString = new MongoClientURI("mymongodb.com:27017");
     MongoClient mongoClient = MongoClients.create(connectionString);
     MongoDatabase database = mongoClient.getdatabse("mydatbase");  
     MongoCollection<Document> collection = database.getCollection("employee");

}
catch (Throwable ex) {
    log.error("Error in Beanshell", ex);
    throw ex;
}

I'm getting error:

ERROR o.a.j.p.j.s.J.JSR223 Sampler: Error in Beanshell groovy.lang.MissingMethodException: No signature of method: static com.mongodb.client.MongoClients.create() is applicable for argument types: (com.mongodb.MongoClientURI) values: [mymongodb.com:27017] Possible solutions: create(), create(com.mongodb.ConnectionString), create(com.mongodb.MongoClientSettings), create(java.lang.String), create(com.mongodb.ConnectionString, com.mongodb.MongoDriverInformation), create(com.mongodb.MongoClientSettings, com.mongodb.MongoDriverInformation) at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1518) ~[groovy-all-2.4.16.jar:2.4.16] at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1504) ~[groovy-all-2.4.16.jar:2.4.16] at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:52) ~[groovy-all-2.4.16.jar:2.4.16] at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) [groovy-all-2.4.16.jar:2.4.16] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) [groovy-all-2.4.16.jar:2.4.16] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128) [groovy-all-2.4.16.jar:2.4.16] at Script11.run(Script11.groovy:44) [script:?] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) [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.1.1 r1855137] at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.1.1 r1855137] at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) [ApacheJMeter_core.jar:5.1.1 r1855137] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) [ApacheJMeter_core.jar:5.1.1 r1855137] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137] at java.lang.Thread.run(Thread.java:834) [?:?]

Anyone has any ideas? Many Thanks!

1
Can you try creating a ConnectionString instead of a MongoClientURI, prepend the protocol mongodb:// and feed that to MongoClients.create?als
I did try that: MongoClient mongoClient = MongoClients.create("mymongodb.com:27017"); getting: ERROR o.a.j.p.j.s.J.JSR223 Sampler: Error in Beanshell java.lang.NoSuchMethodError: com.mongodb.MongoCredential.createCredential(Ljava/lang/String;Ljava/lang/String;[C)Lcom/mongodb/MongoCredential; at com.mongodb.ConnectionString.createCredentials(ConnectionString.java:718)chen layton
Hmm then try to skip creating the MongoClientURI and call the MongoClients.create with "mogodb://mymongodb.com:27017"als
That’s exactly what I did. Still getting error in my last comment. Any more ideas? Thanks.chen layton
In your previous response it looked like you didn't put the protocol mongodb:// into the connection string.als

1 Answers

0
votes

Your code relies on a diamond operator which is not supported in Beanshell.

Be aware that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider choosing groovy as the scripting language and your code should start working normally

You might also be interested in MongoDB Performance Testing with JMeter article