在我的畢業論文中,我開發了用於在客戶端和數據庫之間進行選擇/保存操作的REST API。數據將作爲JSON從傳感器發佈並存儲在MongoDB中。我們選擇了三種不同的存儲技術:Jongo驅動程序1.3.0,Java MongoDB驅動程序3.4.0和Spring的MongoRepository(帶有Jackson FasterXML)。在實現之後,我們通過JMeter開始進行負載測試。測試用例有這些參數:通過Jongo驅動程序,Java MongoDB驅動程序和MongoRepository之間的負載測試比較保存方法
線程 - 100,250,500,1000
斜坡上升期 - 10秒
循環計數 - 30
我們假設司機會更有效而不是MongoRepository,但是在1000個線程的情況下,MongoRepository每秒加載400個請求,驅動程序無法處理所有的請求。所以MongoRepository可以快速存儲。任何人都可以說爲什麼MongoRepository更有效?
編輯:
MongoRepository是這樣的:
@Repository
public interface EndPointsMongoRepository extends
MongoRepository<EndPointsDataControllerEntity, String> {
}
方法deseralize JSON到實體:
private EndPointsDataControllerEntity parseDataFromJson(String json) {
ObjectMapper mapper = new ObjectMapper();
EndPointsDataControllerEntity controller = null;
try {
controller = mapper.readValue(json, EndPointsDataControllerEntity.class);
} catch(JsonParseException ex){
LOGGER.warn(" JsonParseException with message :" + ex.getMessage());
} catch(JsonMappingException ex){
LOGGER.warn("JsonMappingException with message :" + ex.getMessage());
} catch(IOException ex){
LOGGER.warn("IOException with message :" + ex.getMessage());
}
LOGGER.info("Id controller: " + controller.getIdController());
return controller;
}
然後我只保存數據。
的MongoDB的Java驅動程序實現:
public void saveUnstructuredMeasuredData(String json) {
LOGGER.info("Data saved to database by second way: \n" + json);
com.mongodb.client.MongoCollection<Document> collection = getMongoClient().getCollection(UNSTRUCTURED_DATA);
Document objectFromJson = Document.parse(json);
objectFromJson.put(TIMESTAMP_MEASURE, createTimeMeasureAsTimestamp(objectFromJson));
collection.insertOne(objectFromJson);
}
而且Jongo:
public void saveUnstructuredMeasuredDataStraightWithShell(String json) {
LOGGER.info("Data saved to database by third way: \n" + json);
Jongo jongo = new Jongo(getMongoDB());
MongoCollection measuredData = jongo.getCollection(MEASURED_DATA);
measuredData.insert(json);
}
我明白Jongo喜歡「官方」java驅動程序的擴展,我得到的結果有一點點更好的性能。 –