Jmeter 获取 MongoDb 的连接错误

Jmeter Getting Connection Error with MongoDb

我想对我的 mongodb database.So 执行负载测试 我将 mongodb 驱动程序添加到 bin/ext 文件夹并重新启动了 jmeter。我选择了 JSR223 Sampler 并选择 groovy 作为我的语言并添加了如下连接代码

import com.mongodb.*

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("mongoHost"),vars.get("mongoPort").toInteger())))}
        .build();

    MongoClient mongoClient = MongoClients.create(settings);

    MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
    MongoCollection<Document> collection = database.getCollection(vars.get("collectionName"));

    vars.putObject("collection", collection);

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

mongoHost、mongoPort、databaseName、collectionName 在用户定义的变量(测试计划)中配置。 当我 运行 采样器出现错误时

Thread Name:myThread Group 1-1
Sample Start:2020-01-27 15:57:35 IST
Load time:39
Connect Time:0
Latency:0
Size in bytes:0
Sent bytes:0
Headers size in bytes:0
Body size in bytes:0
Sample Count:1
Error Count:1
Data type ("text"|"bin"|""):text
Response code:500
Response message:javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script3.groovy: 6: unable to resolve class com.mongodb.ServerAddress
 @ line 6, column 1.
   import com.mongodb.ServerAddress;
   ^

Script3.groovy: 5: unable to resolve class com.mongodb.MongoClientSettings
 @ line 5, column 1.
   import com.mongodb.MongoClientSettings;
   ^

Script3.groovy: 10: unable to resolve class org.bson.Document
 @ line 10, column 1.
   import org.bson.Document;
   ^

3 errors



SampleResult fields:
ContentType: 
DataEncoding: null

您需要将以下库添加到 JMeter Classpath

  1. mongo-java-driver
  2. bson

库版本必须与您的 MongoDB 版本匹配(或至少兼容),可以通过 运行 db.version() query 在 MongoDB shell

需要重新启动 JMeter 才能获取库

更多信息:MongoDB Performance Testing with JMeter