JMeter压测MongoDB

Posted 此生不换Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JMeter压测MongoDB相关的知识,希望对你有一定的参考价值。

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");
// 带用户名与密码的链接方式

 

以上是关于JMeter压测MongoDB的主要内容,如果未能解决你的问题,请参考以下文章

Jmeter压测快速体验

jmeter压测

jmeter在服务器上的压测命令

jmeter 多接口压测中出现取值 EOF,且压力机不断报文件io错误的现象

压测工具笔记(二)之JMeter

jmeter压测app