在 JMeter 中执行 Mongodb 聚合
Perform Mongodb Aggregation in JMeter
我正在尝试 运行 使用 mongo-java-驱动程序 3.8 在 JMeter 中进行简单聚合。我是 JMeter 的新手,使用 Mongo 和 Java。我使用本教程作为起点:
https://www.blazemeter.com/blog/mongodb-performance-testing-with-jmeter/
我修改了查询文档部分的代码,以便在 JSR223 采样器中使用,如下所示:
import org.bson.Document;
import org.bson.types.ObjectId;
import com.mongodb.client.model.Aggregates;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document result = collection.aggregate(Arrays.asList(Aggregates.sample(1)));
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);
}
我在“查看结果”树中响应采样器结果时收到以下错误:
Response code: 500
Response message: Exception: org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object 'com.mongodb.client.internal.AggregateIterableImpl@3c7a0022' with class
'com.mongodb.client.internal.AggregateIterableImpl' to class 'org.bson.Document'
请注意错误的部分:无法投射对象。
方法MongoCollection#aggregate
returns AggregateIterable
。
将 Document
更改为 AggregateIterable<Document>
将解决此问题。
正如 chuckskull 所说,请阅读文档:
https://mongodb.github.io/mongo-java-driver/3.8/driver/tutorials/aggregation/
您不需要块,也不要在聚合语句末尾使用 forEach(printBlock);;而是使用 first() 就像上面提到的用于查找语句的教程一样。
如果您是新手(像我一样),在了解其工作原理时,只需使用文档中建议的餐厅数据即可。
这是一个工作示例:
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Filters;
import org.bson.Document;
try {
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("restaurants");
Document result = collection.aggregate(Arrays.asList(Aggregates.sample(1))).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);
}
Collection.aggregate() 函数调用 returns AggregateIterable which cannot be cast to the document directly, you can use Groovy head() method 其中 returns 来自 Iterable 实例的第一个值,如:
Document result = collection.aggregate(Arrays.asList(Aggregates.sample(1))).head()
有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It
我正在尝试 运行 使用 mongo-java-驱动程序 3.8 在 JMeter 中进行简单聚合。我是 JMeter 的新手,使用 Mongo 和 Java。我使用本教程作为起点:
https://www.blazemeter.com/blog/mongodb-performance-testing-with-jmeter/
我修改了查询文档部分的代码,以便在 JSR223 采样器中使用,如下所示:
import org.bson.Document;
import org.bson.types.ObjectId;
import com.mongodb.client.model.Aggregates;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document result = collection.aggregate(Arrays.asList(Aggregates.sample(1)));
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);
}
我在“查看结果”树中响应采样器结果时收到以下错误:
Response code: 500
Response message: Exception: org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object 'com.mongodb.client.internal.AggregateIterableImpl@3c7a0022' with class
'com.mongodb.client.internal.AggregateIterableImpl' to class 'org.bson.Document'
请注意错误的部分:无法投射对象。
方法MongoCollection#aggregate
returns AggregateIterable
。
将 Document
更改为 AggregateIterable<Document>
将解决此问题。
正如 chuckskull 所说,请阅读文档: https://mongodb.github.io/mongo-java-driver/3.8/driver/tutorials/aggregation/
您不需要块,也不要在聚合语句末尾使用 forEach(printBlock);;而是使用 first() 就像上面提到的用于查找语句的教程一样。
如果您是新手(像我一样),在了解其工作原理时,只需使用文档中建议的餐厅数据即可。
这是一个工作示例:
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Filters;
import org.bson.Document;
try {
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("restaurants");
Document result = collection.aggregate(Arrays.asList(Aggregates.sample(1))).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);
}
Collection.aggregate() 函数调用 returns AggregateIterable which cannot be cast to the document directly, you can use Groovy head() method 其中 returns 来自 Iterable 实例的第一个值,如:
Document result = collection.aggregate(Arrays.asList(Aggregates.sample(1))).head()
有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It