JMeter压测MongoDB

时间:2022-05-20 01:50:44

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、在MongoDB中创建jmeter_test库与blazemeter_tutorial集合

 

3、编写脚本,采用JSR223 Sampler

JMeter压测MongoDB

名称
mongoHost 192.168.1.111
mongoPort 27017
databaseName jmeter_test
collectionName blazemeter_tutorial

 

JMeter压测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);
}

 

JMeter压测MongoDB

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

 

JMeter压测MongoDB

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

 

JMeter压测MongoDB

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

 

JMeter压测MongoDB

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

 

JMeter压测MongoDB