我想在我的mongodb数据库上执行负载测试。所以我将mongodb驱动程序添加到bin/ext文件夹并重新启动jmeter。我选择了JSR223采样器并选择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 类路径中:
- mongo-java-driver
- 博森
库版本必须与您的MongoDB版本匹配(或至少兼容(,可以通过在MongoDB shell中运行db.version()
查询来获取
需要重新启动JMeter才能获取库
更多信息:使用JMeter进行MongoDB性能测试