1 原生方法:
vim /usr/local/jmeter/apache-jmeter-5.1.1/bin/jmeter.properties
編輯JMeter配置文件
在最后一行加入not_in_menu =

重新啟動JMeter
在配置元件添加MongoDB Source Config (DEPRECATED)
在取樣器添加MongoDB Script (DEPRECATED)


但是此元器件存在嚴重的性能問題,
會在訪問數據庫時鎖定一個鏈接,
故在3.0以后的版本里被拋棄!
2 JSR223取樣器:
2.1 下載mongo-java-driver驅動
官方驅動:
https://mongodb.github.io/mongo-java-driver/
或者Maven:
https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.2</version>
</dependency>
把mongo-java-driver-3.8.2.jar放到/usr/local/jmeter/apache-jmeter-5.1.1/lib/ext目錄下
rm -rf mongo-java-driver-2.11.3.jar
# 刪除/usr/local/jmeter/apache-jmeter-5.1.1/lib目錄中舊版本的mongo-java-driver jar包
2.2 在MongoDB中創建jmeter_test庫與blazemeter_tutorial集合
2.3 編寫腳本,采用JSR223 Sampler

| 名稱 | 值 |
| mongoHost | 192.168.1.111 |
| mongoPort | 27017 |
| databaseName | jmeter_test |
| collectionName | blazemeter_tutorial |

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);
}

import com.mongodb.client.MongoCollection;
import org.bson.Document;
import java.util.Arrays;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document document = new Document("firstName", "Expert")
.append("lastName", "Protocolson")
.append("age", 37)
.append("occupation", "DevOps")
.append("skills", Arrays.asList("System Administration", "Linux"))
.append("adress", new Document("city", "Systemberg")
.append("street", "Data Line")
.append("house", 42));
collection.insertOne(document);
return "Document inserted";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}

import com.mongodb.client.MongoCollection;
import static com.mongodb.client.model.Filters.*;
import org.bson.Document;
import org.bson.types.ObjectId;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document result = collection.find(eq("firstName", "Expert")).first();
vars.put("exampleDocumentId", result.get("_id").toString());
return "Document with id=" + result.get("_id") + " found";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}

import com.mongodb.client.MongoCollection;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.*;
import org.bson.Document;
import org.bson.types.ObjectId;
try {
MongoCollection<Document> collection = vars.getObject("collection");
collection.updateOne(
eq("_id", new ObjectId(vars.get("exampleDocumentId"))),
combine(set("occupation", "Project Manager"), set("adress.city", "New Codeshire"), currentDate("lastModified")));
return "Document with id=" + vars.get("exampleDocumentId") + " modified";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}

import com.mongodb.client.MongoCollection;
import static com.mongodb.client.model.Filters.*;
import org.bson.Document;
try {
MongoCollection<Document> collection = vars.getObject("collection");
collection.deleteOne(eq("occupation", "Project Manager"));
return "Document deleted";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}


2.4 補充說明:
MongoClient mongoClient = MongoClients.create("mongodb://mongohost:27017");
// 不帶用戶名與密碼的鏈接方式
MongoClient mongoClient = MongoClients.create("mongodb://user:password@mongohost/?authSource=userdb&ssl=true");
// 帶用戶名與密碼的鏈接方式
