如何使用Jmeter,MongoDb配置登录服务器

时间:2016-10-15 20:14:44

标签: mongodb jmeter jmeter-plugins

我正在使用MongoDb源配置,我想点击服务器10.X.X.X上的数据库。此服务器需要身份验证如何使用MongoDb Config从Jmeter进行身份验证。

3 个答案:

答案 0 :(得分:1)

您可以使用MongoClient Class来完成。将以下jar文件“mongo-java-driver-2.13.2.jar”放在Jmeter库文件夹中,并在JSR223 sampler中编写以下代码行,选择java作为语言

      import com.mongodb.*;
      import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
      import com.mongodb.DBObject;
      import com.mongodb.BasicDBList;
      import com.mongodb.BasicDBObject;
      import com.mongodb.DBCollection;
      import org.bson.types.ObjectId;
      import java.util.*;
      import java.util.Arrays;
      import java.util.List;

      MongoClient mongoClient = new MongoClient(new ServerAddress("server 
      address:port"));
      DB db = mongoClient.getDB("database name");  
      db.authenticate("uname","password".toCharArray());
      DBCollection coll = db.getCollection("collection name");

答案 1 :(得分:0)

根据How to Load Test MongoDB with JMeter指南,它应该是:

DB db = MongoDBHolder.getDBFromSource("mongodb_source_name", "database_name", "username", "password");

因此,您不必将任何内容放入MongoDB源配置中,凭据应在JSR223 Test Elements中设置

获得MongoDB测试计划"骨架"您可以使用相关的JMeter模板:

File -> Templates -> MongoDB Load Test -> Create

MongoDB Load Test JMeter

答案 2 :(得分:0)

在这里尝试这些:

  1. link下载并安装mongo-java-driver-3.10.2.jar库。
  2. 沿着路径放置替换旧库的库 “ jmeter \ lib”。
  3. 使用将Groovy设置为语言的JSR223采样器

全文代码:

import com.mongodb.*

MongoCredential coreCredential = MongoCredential.createCredential("${userDB}", "${adminDB}", "${password}".toCharArray());
//we register connection options <user>, <database>, <password>. Admin Database is the main database. By logging in, you can read and edit all other databases.

MongoClient coreMongoClient = new MongoClient(new ServerAddress("${server_mongoDB}", ${port_mongoDB}), Arrays.asList(coreCredential));
//we register the address and port of connection of the MongoDB server

DB coreDB = coreMongoClient.getDB("${mongoDb}");
//write the name of the database to which you are connecting

DBCollection coll = coreDB.getCollection("${collection}");
//write the name of the collection that you want to get

BasicDBObject query = new BasicDBObject();
//we create object of future request

query.put(${key}, ${value});
//we create request. Search field format (key, value)

BasicDBObject s = coll.find(query).toArray();
//we carry out request

SampleResult.setResponseData(s.toString(),"UTF-8");
//output in response data so that you can use assert



//query option to select all collection entries in the response window
//SampleResult.setResponseData(coll.find().toArray().toString(),"UTF-8");