我在Jmeter中编写了以下脚本,用于在MongoDB中插入文档。我的脚本执行成功,但是没有执行所需的任务,即未在数据库中插入记录。我想念什么?谁能帮忙
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoCollection;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
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");
//String json = ('{"EmployeeOID":12345,"EmployeeName":"Test Automation through Jmeter","Employee_Type_OID":4,"Rank":0,"Rating":0,"Score":0,"Supervisor_OID":56789,"TRGEmpID":"012345T"}');
//DBObject insertEmployee = (DBObject)JSON.parse(json);
BasicDBObject insertEmployee = new BasicDBObject();
insertEmployee.put("EmployeeOID",12345);
insertEmployee.put("EmployeeName", "Test Automation through Jmeter");
insertEmployee.put("Employee_Type_OID",4);
insertEmployee.put("Rank",0);
insertEmployee.put("Rating",0);
insertEmployee.put("Score",0);
insertEmployee.put("Supervisor_OID",56789);
insertEmployee.put("CompanyID","012345T");
collection.insert(insertEmployee);
DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
System.out.println(cursorDoc.next());
}
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}
答案 0 :(得分:0)
您的脚本中包含return
keyword,在此阶段执行结束。关键字return
下的行将永远不会执行,因此您将无法看到插入的对象,因为代码无法运行。
删除此行:
return "Connected to " + vars.get("collectionName");
并且您的脚本应按预期开始工作(或至少开始失败)