JMeter 使用 Java BeanShell 将文档保存到 MongoDb:JSR223 脚本采样器中的问题

JMeter saving document to MongoDb with Java BeanShell: Problem in JSR223 script Sampler

我想通过 JMeter 在 MongoDb 中插入一次并更新几次。如果相关 MOngoDb 是 Docker 中的 运行。我在连接期间遇到问题,但似乎我正在尝试错误地连接到 MongoDb。

我确信 MongoDb 已启动并且 运行 因为我可以使用以下参数成功连接并从 Spring 添加数据:

spring:
  data:
    mongodb:
      host: localhost
      port:  27017
      database: demodb
      authentication-database: admin
      username: root
      password: rootpassword

我在 JMeter 中尝试了两种方法,但都返回错误:

第一个暂定)

使用 Lambda。我在某处读到 lambda 不适用于 Grovy 但我正在尝试使用 Java (我对 Grovy 一无所知)。我在这里添加了这个细节,因为当我在 Jmeter 中搜索面临“->”问题的人时,答案总是与 "Grovy doesn't work with Lambda" 相关。我忽略了这个答案,因为我使用的是 Java。至少,我假设我通过右键单击 Thread Group -> Sampler -> JSR 223 Sampler -> 在 Script Language Box 下正确选择 Java 我选择了 java (Bean Shell2.0b6 / Bean Shell 引擎 1.0)

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 org.bson.Document;
import java.util.Arrays;
try {
    MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings {builder -> builder.hosts(Arrays.asList(                new ServerAddress(vars.get("localhost"),vars.get("27017").toInteger())))}
        .build();

    MongoClient mongoClient = MongoClients.create(settings);
    MongoDatabase database = mongoClient.getDatabase(vars.get("demodb"));
    MongoCollection<Document> collection = database.getCollection(vars.get("transfers"));
    vars.putObject("collection", collection);

    Document document = new Document("id", "3")
    .append("origem", "Jimis")
    .append("destino", "drpc")
    .append("status", 1);

    collection.insertOne(document);

    return "Connected to " + vars.get("transfers");
}
catch (Exception e) {
    SampleResult.setSuccessful(false);
    SampleResult.setResponseCode("500");
    SampleResult.setResponseMessage("Exception: " + e);
}

通过这种方法,我得到了这个错误日志:

2020-04-03 16:11:23,519 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-04-03 16:11:23,519 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-04-03 16:11:23,520 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-04-03 16:11:23,529 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2020-04-03 16:11:23,529 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2020-04-03 16:11:23,530 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-04-03 16:11:23,530 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2020-04-03 16:11:23,531 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-04-03 16:11:23,533 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-04-03 16:11:23,534 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-03 16:11:23,542 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered ">" at line 10, column 103.
 in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 10
javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered ">" at line 10, column 103.
 in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 10
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) [ApacheJMeter_core.jar:5.2.1]
    at java.lang.Thread.run(Thread.java:834) [?:?]
2020-04-03 16:11:23,544 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2020-04-03 16:11:23,544 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2020-04-03 16:11:23,544 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-04-03 16:11:23,545 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

第二个暂定)

避开 Lambda 以防它帮助我前进。

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 org.bson.Document;

import java.util.Arrays;

try {
    MongoClient mongoClient = MongoClients.create("demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true");

    MongoDatabase database = mongoClient.getDatabase(vars.get("demodb"));
    MongoCollection<Document> collection = database.getCollection(vars.get("transfers"));
    vars.putObject("collection", collection);

    Document document = new Document("id", "3")
    .append("origem", "Jimis")
    .append("destino", "drpc")
    .append("status", 1);

    collection.insertOne(document);*

    return "connected";
}
catch (Exception e) {
    SampleResult.setSuccessful(false);
    SampleResult.setResponseCode("500");
    SampleResult.setResponseMessage("Exception: " + e);
}

日志是:

2020-04-03 16:14:18,684 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-04-03 16:14:18,685 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-04-03 16:14:18,685 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-04-03 16:14:18,746 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2020-04-03 16:14:18,746 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2020-04-03 16:14:18,746 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-04-03 16:14:18,746 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2020-04-03 16:14:18,747 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-04-03 16:14:18,747 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-04-03 16:14:18,747 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-03 16:14:18,752 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered "=" at line 16, column 46.
 in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 16
javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered "=" at line 16, column 46.
 in inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' at line number 16
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) [ApacheJMeter_core.jar:5.2.1]
    at java.lang.Thread.run(Thread.java:834) [?:?]
2020-04-03 16:14:18,753 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2020-04-03 16:14:18,753 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2020-04-03 16:14:18,753 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-04-03 16:14:18,753 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

初步看来它在抱怨在这里使用 lambda

MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings {builder -> builder.hosts(Arrays.asList(                new ServerAddress(vars.get("localhost"),vars.get("27017").toInteger())))}
            .build();

在第二个尝试中,它似乎在抱怨 equals 信号,但我认为我的理解不准确。

message: javax.script.ScriptException: In file: inline evaluation of: ``import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; i . . . '' Encountered "=" at line 16, column 46.

*** 授权问题(据我所知,我正在使用从 Spring 成功登录到 MongoDb

的相同配置
import com.mongodb.*
import com.mongodb.BasicDBObject
import org.bson.*

MongoCredential coreCredential = MongoCredential.createCredential("root", "demodb", "rootpassword".toCharArray());
MongoClient coreMongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(coreCredential));
DB coreDB = coreMongoClient.getDB("demodb");
DBCollection coll = coreDB.getCollection("transfer");

BasicDBObject document = new BasicDBObject("id", "3")
.append("origem", "Jimis")
.append("destino", "drpc")
.append("status", 1);

coreDB.getCollection('transfer').insert(document);

新错误:

-06 18:59:39,561 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-06 18:59:39,588 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}
javax.script.ScriptException: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[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.2.1]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]

PS.: 我在某处读到我应该更新 JMeter MongoDb jar 所以我做了(现在是 mongo-java-driver-2.13.2.jar)

*** 其他暂定

当我尝试时

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

DB db = MongoDBHolder.getDBFromSource("demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true", "demodb");
DBCollection collection = db.getCollection("transfer"); 
long count = collection.getCount();
String result = String.valueOf(count);              // convert long to String

//log.info("Log Resultado:" + result)); 
SampleResult.setResponseData("SampleResult Resultado: " + result,"UTF-8");

我明白了

 Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: java.lang.IllegalStateException: You didn't define variable:demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true using MongoDB Source Config (property:MongoDB Source)
javax.script.ScriptException: java.lang.IllegalStateException: You didn't define variable:demodb://root:rootpassword@localhost:27017/?authSource=admin&ssl=true using MongoDB Source Config (property:MongoDB Source)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[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]

它似乎在抱怨 MongoDB 源配置。在 Internet 上搜索我没有找到如何在 JMeter 中配置它

您不能在 groovy 中使用 lambda,但您不必在您的情况下使用

您可以使用 or use java anonymous inner class as example

 MongoClientSettings expected = MongoClientSettings.builder()
.applyToClusterSettings(new Block<ClusterSettings.Builder>() {

@Override  
 void apply(final ClusterSettings.Builder builder){
 builder.hosts([new ServerAddress('host1', 1), new ServerAddress('host2', 2)]);

Beanshell 不支持 diamond operators 因此请务必在 "Language" 下拉列表中选择 groovy

此外users are encouraged to use Groovy since JMeter 3.1 mainly because Groovy has much better performance comparing to Beanshell

因此请考虑切换到 Groovy,错误应该会消失。