overleaf/services/document-updater/app/coffee/Profiler.coffee

35 lines
846 B
CoffeeScript
Raw Normal View History

2017-05-18 06:00:07 -04:00
Settings = require('settings-sharelatex')
logger = require('logger-sharelatex')
deltaMs = (ta, tb) ->
2017-05-18 10:02:54 -04:00
nanoSeconds = (ta[0]-tb[0])*1e9 + (ta[1]-tb[1])
milliSeconds = Math.floor(nanoSeconds*1e-6)
return milliSeconds
2017-05-18 06:00:07 -04:00
module.exports = class Profiler
2017-05-18 10:02:08 -04:00
LOG_CUTOFF_TIME: 1000
2017-05-18 06:00:07 -04:00
constructor: (@name, @args) ->
@t0 = @t = process.hrtime()
@start = new Date()
2017-05-18 06:00:07 -04:00
@updateTimes = []
log: (label) ->
t1 = process.hrtime()
dtMilliSec = deltaMs(t1, @t)
@t = t1
@updateTimes.push [label, dtMilliSec] # timings in ms
return @ # make it chainable
end: (message) ->
totalTime = deltaMs(@t, @t0)
if totalTime > @LOG_CUTOFF_TIME # log anything greater than cutoff
args = {}
for k,v of @args
args[k] = v
args.updateTimes = @updateTimes
args.start = @start
args.end = new Date()
logger.log args, @name
return totalTime