2016-06-15 24 views
0

我试图找到在我的应用程序中需要占用太多CPU资源的原因,以及为什么我需要很长的等待时间,所以我开始在应用程序中记录一些东西。当我登录我的应用程序每一个部分我就遇到了这个:流星服务器方法运行时间

var updateTime = process.hrtime(); 

Nightclubs.update({_id: nightclubId}, {$push: { guests: { 
    value: value, 
    currentGuestAmount: currentGuestAmount+value, 
    date: thisEntryDate.toDate(), 
    gender: gender, 
    age: age, 
    guard: guardId 
}}}) 

var updateDiff = process.hrtime(updateTime); 

的方法,后来我登录这个时候是这样的:

console.log('update benchmark took %d nanoseconds', updateTime[0] * 1e9 + updateTime[1]); 

这导致这样的:

update benchmark took 1084353904561267 nanoseconds 

是的..这是1.8周....这真的很奇怪,因为该方法总共需要916589992纳秒或0.91秒(仍然有点太长)

有没有人对此有任何线索?

PS,对细节我插入什么数据了一下:

guests: { type: Array, defaultValue: [] }, 
'guests.$': { type: Object }, 
'guests.$.value': { type: Number }, 
'guests.$.currentGuestAmount': { type: Number }, 
'guests.$.date': { type: Date }, 
'guests.$.age': { type: Number }, 
'guests.$.gender': { type: String }, 
'guests.$.guard': { type: String }, 
+2

您可能需要使用'console.time'和'console.timeEnd' https://developer.mozilla.org/zh-CN/docs/Web/API/console#Timers – Ser

+0

@Ser是的,那个给了一个更好的答案:15ms!我想我会继续那个! –

回答

1

最后,使用console.timeconsole.timeEnd代替process.hrtime()

console.time("Update nightclub"); 

Nightclubs.update({_id: nightclubId}, {$push: { guests: { 
    value: value, 
    currentGuestAmount: currentGuestAmount+value, 
    date: thisEntryDate.toDate(), 
    gender: gender, 
    age: age, 
    guard: guardId 
}}}) 

console.timeEnd("Update nightclub"); 
相关问题