JMeter压测MongoDB
<h1><span style="color: rgba(255, 0, 255, 1)">1 原生方法:</span></h1><p><span style="color: rgba(255, 0, 0, 1)">vim /usr/local/jmeter/apache-jmeter-5.1.1/bin/jmeter.properties</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">编辑JMeter配置文件</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">在最后一行加入<span style="color: rgba(255, 102, 0, 1)">not_in_menu =</span></span></p>
<p><img src="https://img2020.cnblogs.com/blog/1321829/202011/1321829-20201127234632856-488262933.png"></p>
<p> 重新启动JMeter</p>
<p>在配置元件添加<span style="color: rgba(153, 51, 0, 1)"><strong>MongoDB Source Config (DEPRECATED)</strong></span></p>
<p>在取样器添加<span style="color: rgba(153, 51, 0, 1)"><strong>MongoDB Script (DEPRECATED)</strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"> <img src="https://img2020.cnblogs.com/blog/1321829/202011/1321829-20201127235308403-612998616.png"></span></p>
<p> </p>
<p><img src="https://img2020.cnblogs.com/blog/1321829/202011/1321829-20201127235328760-10009239.png"></p>
<p><strong><em><span style="text-decoration: underline"><span style="text-decoration: line-through"><span style="color: rgba(153, 51, 102, 1); text-decoration: line-through">但是此元器件存在严重的性能问题,</span></span></span></em></strong></p>
<p><strong><em><span style="text-decoration: underline"><span style="text-decoration: line-through"><span style="color: rgba(153, 51, 102, 1); text-decoration: line-through">会在访问数据库时锁定一个链接,</span></span></span></em></strong></p>
<p><strong><em><span style="text-decoration: underline"><span style="text-decoration: line-through"><span style="color: rgba(153, 51, 102, 1); text-decoration: line-through">故在3.0以后的版本里被抛弃!</span></span></span></em></strong></p>
<p> </p>
<h1><span style="color: rgba(255, 0, 255, 1)">2 JSR223取样器:</span></h1>
<h2><span style="color: rgba(255, 0, 255, 1)">2.1 下载mongo-java-driver驱动</span></h2>
<p>官方驱动:</p>
<p>https://mongodb.github.io/mongo-java-driver/</p>
<p>或者Maven:</p>
<p>https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver</p>
<pre><span style="color: rgba(128, 128, 128, 1)"><!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver --><br><span style="color: rgba(232, 191, 106, 1)"><dependency><br><span style="color: rgba(232, 191, 106, 1)"> <groupId>org.mongodb<span style="color: rgba(232, 191, 106, 1)"></groupId><br><span style="color: rgba(232, 191, 106, 1)"> <artifactId>mongo-java-driver<span style="color: rgba(232, 191, 106, 1)"></artifactId><br><span style="color: rgba(232, 191, 106, 1)"> <version>3.8.2<span style="color: rgba(232, 191, 106, 1)"></version><br><span style="color: rgba(232, 191, 106, 1)"></dependency></span></span></span></span></span></span></span></span></span></pre>
<p>把mongo-java-driver-3.8.2.jar放到/usr/local/jmeter/apache-jmeter-5.1.1/lib/ext目录下</p>
<p><span style="color: rgba(255, 0, 0, 1)">rm -rf mongo-java-driver-2.11.3.jar</span></p>
<p># 删除/usr/local/jmeter/apache-jmeter-5.1.1/lib目录<span><span>中</span><span>旧版本的mongo-java-driver jar包</span></span></p>
<p> </p>
<h2><span style="color: rgba(255, 0, 255, 1)">2.2 在MongoDB中创建jmeter_test库与blazemeter_tutorial集合</span></h2>
<p> </p>
<h2><span style="color: rgba(255, 0, 255, 1)">2.3 编写脚本,采用JSR223 Sampler</span></h2>
<p><span><span><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201234202888-1810198429.png"></span></span></p>
<table border="0">
<tbody>
<tr>
<td>名称</td>
<td>值</td>
</tr>
<tr>
<td>mongoHost</td>
<td>192.168.1.111</td>
</tr>
<tr>
<td>mongoPort</td>
<td>27017</td>
</tr>
<tr>
<td>databaseName</td>
<td>jmeter_test</td>
</tr>
<tr>
<td>collectionName</td>
<td>blazemeter_tutorial</td>
</tr>
</tbody>
</table>
<p> </p>
<p><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201234547618-660794851.png"></p>
<div class="cnblogs_Highlighter">
<pre class="brush:groovy;gutter:true;">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);
}
</pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201234654172-1830458744.png"></p>
<div class="cnblogs_Highlighter">
<pre class="brush:groovy;gutter:true;">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);
}
</pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201234756405-440576394.png"></p>
<div class="cnblogs_Highlighter">
<pre class="brush:groovy;gutter:true;">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);
}
</pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201234826945-391633342.png"></p>
<div class="cnblogs_Highlighter">
<pre class="brush:groovy;gutter:true;">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);
}
</pre>
</div>
<p> </p>
<p><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201234910113-30904402.png"></p>
<div class="cnblogs_Highlighter">
<pre class="brush:groovy;gutter:true;">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);
}
</pre>
</div>
<h1> </h1>
<p><img src="https://img2018.cnblogs.com/i-beta/1321829/202002/1321829-20200201235048219-468344476.png"></p>
<p><img src="https://common.cnblogs.com/images/loading.gif"></p>
<p> </p>
<h2><span style="color: rgba(255, 0, 255, 1)">2.4 补充说明:</span></h2>
<div class="cnblogs_Highlighter">
<pre class="brush:java;gutter:true;">MongoClient mongoClient = MongoClients.create("mongodb://mongohost:27017");
// 不带用户名与密码的链接方式
MongoClient mongoClient = MongoClients.create("mongodb://user:password@mongohost/?authSource=userdb&ssl=true");
// 带用户名与密码的链接方式
</pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/yjlch1016/p/12250782.html
頁:
[1]