我在服务器上使用mlab沙箱,并且在后端使用node.js express。问题在于从mlab到客户端的数据检索时间。基于mongoDB上的executionStats,有0个executionTimemills,但是当我尝试通过邮递员获取数据时,平均时间为500毫秒,大小为600B。我试图追踪延迟的来源。并将其放在后端的promise上,基本上,测试是在查询触发(pp_query)之前发布当前时间,并将其与返回的promise (pp_res)的时间进行比较。 em>。我使用了异步功能并等待。即使MongoDb上的执行时间为0,也有这种延迟是正常的吗?如果是,对于600B大小的数据,500ms是否太长?谢谢
这是我的异步函数:
onResponse()
这些是方法:
async function suggestedPrice(itemCode,customerId,qty){
var priceCatega = priceCateg(itemCode);
var aitemHis = itemHistory_c(itemCode,customerId);
var apriceCat = priceCategoryHistory_c(customerId, await priceCatega)
var itemHis = await aitemHis;
var priceCat = await apriceCat;
return {itemHis,priceCat};
}
路由器
const itemHistory_c = (itemCode,customerId) =>{
var pp_query = speedTest(); //states the current timestamp
var x = deploySalesOrders.find({itemCode,customerId})
x.select('date discount price offer');
x.limit(1);
x.sort({date:-1})
//x.explain("executionStats");
return x.exec()
.then((itemHis) => {
var pp_res = speedTest();
// return itemHis;
var g;
if(!isEmpty(itemHis)){
var date_IH = itemHis[0].date;
var disc_IH = itemHis[0].discount;
var unitPrice_IH = itemHis[0].price;
var offer = itemHis[0].offer;
var origin = "itemHis";
g = {date : date_IH,unitPrice : unitPrice_IH,disc : disc_IH,offer,origin}
}
var post_res= speedTest();
g = {...itemHis,pp_query,pp_res,post_res}
return g;
})
}
const priceCategoryHistory_c = (customerId,priceCategory) =>{
var x;
if(priceCategory != ''){
var pp_query = speedTest();
var y = deploySalesOrders.find({$and:[{priceCategory},{customerId}]})
y.select('date discount price offer itemCode controlNo_DSI')
y.sort("-date")
y.limit(1)
return y.exec()
.then((priceCatHis) =>{
var pp_res = speedTest();
if(!isEmpty(priceCatHis)){
var date_IH = priceCatHis[0].date;
var disc_IH = priceCatHis[0].discount;
var unitPrice_IH = priceCatHis[0].price;
var offer = priceCatHis[0].offer;
var itemCode = priceCatHis[0].itemCode;
var controlNo_DSI = priceCatHis[0].controlNo_DSI
var origin = "priceCat";
x = {date : date_IH,unitPrice : unitPrice_IH,disc : disc_IH, priceCategory,offer,itemCode,controlNo_DSI,origin}//,purchase}
}
var post_res= speedTest();
x = {...x,pp_query,pp_res,post_res}
return x;
// return priceCatHis
})
}
}
const priceCateg = (itemCode) =>{
var x = ItemTrial.find({itemCode})
x.select('price.priceCategory')
x.sort('-date')
x.limit(1)
return x.exec()
.then((price) =>{
if (price.length > 0){
return price[0].price.priceCategory;
}
else{
return '';
}
});
}
邮递员结果
router.use('/suggestions',(req,res)=>{
try{
suggestedPrice(req.query.itemCode, req.query.customerId,req.query.totalQty)
.then((a) => {
res.status(200).send(a)
})
}
catch(err){
res.json({err});
}
})