问题
I have a problem in my JSR 223 Groovy script when it tries to access MonDB 3.0.3 with authentication enabled from JMeter 2.3 :
import com.mongodb.DB;
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
DB db = MongoDBHolder.getDBFromSource("mongoDS", "${mongodb_database}", "${mongodb_user}", "${mongodb_password}");
DBCollection coll = db.getCollection("users");
BasicDBObject query = new BasicDBObject("userId",vars.get("userId"));
DBCollection coll = db.getCollection("users");
This is the output:
2015/09/02 16:45:05 ERROR - meter.protocol.java.sampler.JSR223Sampler:
Problem in JSR223 script Sample MongoDB, message:javax.script.ScriptException:
java.lang.NullPointerException javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:326)
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:44)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:200)
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:70)
at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at org.apache.jmeter.protocol.mongodb.config.MongoDBHolder.getDBFromSource(MongoDBHolder.java:51)
at org.apache.jmeter.protocol.mongodb.config.MongoDBHolder$getDBFromSource.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at Script8.run(Script8.groovy:13)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:323)
... 7 more
I did not have issue before enabling the authentication in MongoDB. Any help would be appreciated.
Edit: this is an snippet from my original Test Plan:
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="functional-test" enabled="true">
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="mongodb_server" elementType="Argument">
<stringProp name="Argument.name">mongodb_server</stringProp>
<stringProp name="Argument.value">"MONGODB_SERVER_IP"</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="mongodb_user" elementType="Argument">
<stringProp name="Argument.name">mongodb_user</stringProp>
<stringProp name="Argument.value">"MY_MONGO_USER"</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="mongodb_password" elementType="Argument">
<stringProp name="Argument.name">mongodb_password</stringProp>
<stringProp name="Argument.value">"MY_PASSWORD"</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="mongodb_database" elementType="Argument">
<stringProp name="Argument.name">mongodb_database</stringProp>
<stringProp name="Argument.value">"MY_DB"</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
<hashTree/>
<MongoSourceElement guiclass="TestBeanGUI" testclass="MongoSourceElement" testname="MongoDB Source Config" enabled="true">
<boolProp name="autoConnectRetry">false</boolProp>
<intProp name="connectTimeout">0</intProp>
<stringProp name="connection">${mongodb_server}</stringProp>
<intProp name="connectionsPerHost">10</intProp>
<boolProp name="continueOnInsertError">false</boolProp>
<boolProp name="fsync">false</boolProp>
<longProp name="maxAutoConnectRetryTime">0</longProp>
<intProp name="maxWaitTime">120000</intProp>
<boolProp name="safe">false</boolProp>
<boolProp name="socketKeepAlive">false</boolProp>
<intProp name="socketTimeout">0</intProp>
<stringProp name="source">myMongoDBSource</stringProp>
<intProp name="threadsAllowedToBlockForConnectionMultiplier">5</intProp>
<boolProp name="waitForJournaling">false</boolProp>
<intProp name="writeOperationNumberOfServers">0</intProp>
<intProp name="writeOperationTimeout">0</intProp>
</MongoSourceElement>
<JSR223Sampler guiclass="TestBeanGUI" testclass="JSR223Sampler" testname="Sample MongoDB" enabled="true">
<stringProp name="cacheKey">pol</stringProp>
<stringProp name="filename"></stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="script">
import com.mongodb.DB;
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
DB db = MongoDBHolder.getDBFromSource("mongoDS", "${mongodb_database}", "${mongodb_user}", "${mongodb_password}");
DBCollection coll = db.getCollection("users");
BasicDBObject query = new BasicDBObject("userId",vars.get("userId"));
DBCollection coll = db.getCollection("users");
</stringProp>
<stringProp name="scriptLanguage">groovy</stringProp>
</JSR223Sampler>
</jmeterTestPlan>
回答1:
Finally I found the solution by replacing the 'mongo-java-driver-2.11.3.jar' with 'mongo-java-driver-2.13.2.jar' in my Apache-JMeter lib directory. May be it has some issues with Mongo 3 authentication.
Also I had modified the code in order to work:
import com.mongodb.*
import com.mongodb.BasicDBObject
MongoCredential coreCredential = MongoCredential.createCredential("${mongodb_user}", "${mongodb_database}", "${mongodb_password}".toCharArray());
MongoClient coreMongoClient = new MongoClient(new ServerAddress("${mongodb_server}", 27017), Arrays.asList(coreCredential));
DB coreDB = coreMongoClient.getDB("${mongodb_database}");
DBCollection coll = coreDB.getCollection("users");
...
now everything work well.
回答2:
You didn't define the MongoDB connection or made a mistake in its field:
- MongoDB Source : which should be named "mongoDS"
See:
- http://jmeter.apache.org/usermanual/component_reference.html#MongoDB_Source_Config
Besides, you shouldn't be using ${} in Groovy script to reference JMeter variables, use instead:
vars["mongodb_database"] instead of ${mongodb_database}
来源:https://stackoverflow.com/questions/32354645/jsr223-groovy-cannot-access-mongodb3-0-3-in-jmeter2-13