JSR223(Groovy)无法访问Jmeter中的MongoDB(3.0.3)(2.13)

时间:2015-09-02 13:32:48

标签: mongodb authentication groovy jmeter jsr223

我的JSR 223 Groovy脚本在尝试从JMeter 2.3启用身份验证时访问MonDB 3.0.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");

这是输出:

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

在MongoDB中启用身份验证之前,我没有遇到任何问题。 任何帮助将不胜感激。

修改:这是我原始测试计划的一个片段:

<?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>

2 个答案:

答案 0 :(得分:2)

最后,我通过在我的Apache-JMeter lib目录中用'mongo-java-driver-2.13.2.jar'替换'mongo-java-driver-2.11.3.jar'找到了解决方案。可能是Mongo 3身份验证存在一些问题。

我还修改了代码以便工作:

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");
...

现在一切正常。

答案 1 :(得分:0)

您没有定义MongoDB连接或在其字段中出错:

  • MongoDB来源:应命名为“ mongoDS

请参阅:

此外,您不应该在Groovy脚本中使用$ {}来引用JMeter变量,而是使用:

  

vars [“mongodb_database”]而非$ {mongodb_database}