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

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

JMeter压测MongoDB的更多相关文章

  1. jmeter压测app

    使用代理的方式,录制app端脚本,之后用jmeter压测就没啥好说的了 1.电脑端谷歌设置本地代理(端口号为8888) 2.jmeter设置HTTP代理服务器(端口号为8888) 3.手机端wifi设 ...

  2. JMeter压测基础(三)——Mysql数据库

    JMeter压测基础(三)——Mysql数据库 环境准备 mysql驱动 JMeter jdbc配置 JMeter jdbc请求 1.下载mysql驱动:mysql-connector-java.ja ...

  3. jmeter压测参数设定(转)

    jmeter压测参数设定 一.基本公式 线程数 = QPS * time: 注:QPS--每秒完成请求的个数:time--每个请求响应完成平均需要时间: 故QPS * time就是所有请求完成响应所需 ...

  4. jmeter压测、操作数据库、分布式linux下运行、webservice接口测试、charles抓包

    一.jmeter压测 在线程组中设置好,然后添加http请求,t添加聚合报告查看压力测试结果,如图: 一般压测时间10-15分钟,如果是稳定性测试,一般n*12小时,这些并发用户一直在请求. tps: ...

  5. jmeter压测之添加负载机

    jmeter压测基本介绍一般基准测试(基准测试时间一般为5分钟)后压测的时间是10-15分钟: 实施测试之前要拿到测试指标 例如:tps要达到多少响应时间要达到多少并发数要达到多少TPS :服务端每秒 ...

  6. jmeter 压测duobbo接口,施压客户端自己把自己压死了

    jmeter 压测duobbo接口,jmeter代码不合理,导致每执行一次请求,会调用一次消耗内存的实例化.导致越压越慢,请求发不出去.这个时候需要考虑修改代码了. 截图中,tps越来越少. 原来初始 ...

  7. jmeter压测-负载配置

    jmeter 压测 一般压测的时间是10-15分钟 TPS:服务端每秒钟处理的请求数  越大越好 响应时间 :越短越好 并发用户数  也就是多少并发 指标给你:tps要达到多少 响应时间要达到多少 并 ...

  8. JMeter压测“java.net.BindException: Address already in use: connect”解决方法

    之前在windows机上用JMeter压测,50并发下出现大量接口报“java.net.BindException: Address already in use: connect”错误. 从字面的意 ...

  9. jmeter压测学习1-window环境准备与案例

    前言 最近用jmeter做一些接口的压力测试,记录下使用过程中遇到的一些问题. 在使用window机器做并发压测的时候,发现并发数设置100的时候,会出现报错:java.net.SocketExcep ...

随机推荐

  1. textarea 标签

    textarea 标签 -- 代表HTML表单多行输入域 textarea标签是成对出现的,以<textarea>开始,以</textarea>结束 属性: Common -- ...

  2. 关于宏MACRO,我们需要知道的事

    一.先从最宏观的角度来了解宏,这里的宏观角度是指程序的运行流程: 1,提交代码后,SAS先把代码读取储存到堆栈中: 2,用文本扫描插件来扫描堆栈中的代码,从上到下,从左到右: 3,扫描到一个分号,则编 ...

  3. Spring ioc(4)---如何解决循环依赖

    前面说到对象的创建,那么在创建的过程中Spring是怎么又是如何解决循环依赖的呢.前面提到有个三级缓存.就是利用这个来解决循环依赖.打个比方说实例化A的时候,先将A创建(早期对象)放入一个池子中.这个 ...

  4. 浅谈 k8s ingress controller 选型

    大家好,先简单自我介绍下,我叫厉辉,来自腾讯云.业余时间比较喜欢开源,现在是Apache APISIX PPMC.今天我来简单给大家介绍下 K8S Ingress 控制器的选型经验,今天我讲的这些内容 ...

  5. JavaScript-装饰器模式

    装饰器模式 为对象添加新功能 不改变其原有的结构和功能 传统 UML 类图 javascript 中的装饰器 装饰类 @testDec clss Demo { } function testDec(t ...

  6. input禁止输入后,触发事件,在苹果手机的页面会滚动

    在vue中,<input type="text" readonly="readonly" @click=""/>,点击跳转页面. ...

  7. Android埋点方案的简单实现-AOP之AspectJ

    个人博客 http://www.milovetingting.cn Android埋点方案的简单实现-AOP之AspectJ AOP的定义 AOP为Aspect Oriented Programmin ...

  8. [MySQL]mysql binlog回滚数据

    1.先开启binlog log-bin = /var/log/mysql/mysql_bin #binlog日志文件,以mysql_bin开头,六个数字结尾的文件:mysql_bin.000001,并 ...

  9. java读取中文文本文件乱码问题

    今天遇到的问题是这样:用java读取一个中文文本文件,但读取到的却是乱码,之前一直没有问题,查清楚后,原来是因为今天是用的windows的记事本来编辑的文件,因编码方式是的不同而导致了乱码,解决方法就 ...

  10. MySQL基础(3) | 函数

    MySQL基础(3) | 函数 前言 MySQL只有标量值函数的概念,没有SqlServer那种表值函数. 语法 创建 create function f_add( a int, b int ) re ...