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

JSR223 (Groovy) cannot access MongoDB(3.0.3) in Jmeter(2.13)

当我的 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>

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

  • MongoDB 来源:应命名为“mongoDS

参见:

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

vars["mongodb_database"] instead of ${mongodb_database}

最后,我通过将 Apache-JMeter lib 目录中的 'mongo-java-driver-2.11.3.jar' 替换为 'mongo-java-driver-2.13.2.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");
...

现在一切正常。