在JMeter中使用Groovy来查询Mongodb

时间:2018-02-04 15:03:19

标签: mongodb groovy jmeter

我是新手,这可能是一个非常基本的问题,但我没有找到适合我的好样本。在JMeter 3.3中,我设置了JSR223 Sampler来查询mongo db中的几个参数。我成功连接到mongo并对集合进行了计数,现在我需要通过_id进行查询。我后来需要计算数组中的元素数量。到目前为止,甚至发现id没有用。 工作部分:

import com.gmongo.*;
import com.mongodb.*;
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
import com.mongodb.DBCollection;                    // import DBCollection class

DB db = MongoDBHolder.getDBFromSource("mongo", "Users");
DBCollection collection = db.getCollection("DownloadTracking"); // get "zips" collection
long count = collection.getCount();
String result = String.valueOf(count);              // convert long to String
SampleResult.setResponseData(result.getBytes());

现在没有工作样本:

log.info db.collection.findOne()

2018-02-04 16:36:19,826 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: collection for class: com.mongodb.DBApiLayer

实际上我需要得到这个对象的响应:

BasicDBObject query = new BasicDBObject("EntityOwner.UserId", ObjectId("544e0e6dcbda7101b0148739")); // create DBObject holding the query
DBObject result1 = collection.findOne(query);  // execute the query and store outcome into "result" DBObject
SampleResult.setResponseData(result1.toString().getBytes()); // set JSR223 sampler response to "result"

错误:

2018-02-04 16:55:24,197 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.ObjectId() is applicable for argument types: (java.lang.String) values: [544e0e6dcbda7101b0148739]
Possible solutions: inject(groovy.lang.Closure)
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.ObjectId() is applicable for argument types: (java.lang.String) values: [544e0e6dcbda7101b0148739]
Possible solutions: inject(groovy.lang.Closure)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-all-2.4.12.jar:2.4.12]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_152]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:222) ~[ApacheJMeter_core.jar:3.3 r1808647]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:3.3 r1808647]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:498) [ApacheJMeter_core.jar:3.3 r1808647]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:424) [ApacheJMeter_core.jar:3.3 r1808647]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:255) [ApacheJMeter_core.jar:3.3 r1808647]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_152]

我尝试将对象放在JSON格式中,例如:.findOne({ "EntityOwner.UserId": ObjectId("544e0e6dcbda7101b0148739")})。它没用。

1 个答案:

答案 0 :(得分:3)

尝试修改您的查询,如下所示:

def query = new BasicDBObject()
query.put('EntityOwner.UserId', new org.bson.types.ObjectId('544e0e6dcbda7101b0148739'))
def result1 = collection.findOne(query)

参考文献: