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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 补充说明:
1 2 3 4 5 | MongoClient mongoClient = MongoClients.create( "mongodb://mongohost:27017" ); // 不带用户名与密码的链接方式 MongoClient mongoClient = MongoClients.create( "mongodb://user:password@mongohost/?authSource=userdb&ssl=true" ); // 带用户名与密码的链接方式 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构