zoukankan      html  css  js  c++  java
  • JMeter压测MongoDB

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

     

  • 相关阅读:
    repeater 结合checkbox批量删除
    (转)用JS判断ckeditor3.6版本编辑器内容为空的方法
    把数据库中的null作为条件查询应该用is
    注意 reader["yjID"] == DBNull.Value而不是null
    (转)第三方登录(QQ登录)开发流程详解
    (转)TortoiseSVN使用简介
    dropdownlist 二级联动
    关于服务器防火墙和discuz论坛的问题
    (转)Discuz!NT图文安装教程
    maven 基础
  • 原文地址:https://www.cnblogs.com/yjlch1016/p/12250782.html
Copyright © 2011-2022 走看看