Profiler.coffee 846 B

12345678910111213141516171819202122232425262728293031323334
  1. Settings = require('settings-sharelatex')
  2. logger = require('logger-sharelatex')
  3. deltaMs = (ta, tb) ->
  4. nanoSeconds = (ta[0]-tb[0])*1e9 + (ta[1]-tb[1])
  5. milliSeconds = Math.floor(nanoSeconds*1e-6)
  6. return milliSeconds
  7. module.exports = class Profiler
  8. LOG_CUTOFF_TIME: 1000
  9. constructor: (@name, @args) ->
  10. @t0 = @t = process.hrtime()
  11. @start = new Date()
  12. @updateTimes = []
  13. log: (label) ->
  14. t1 = process.hrtime()
  15. dtMilliSec = deltaMs(t1, @t)
  16. @t = t1
  17. @updateTimes.push [label, dtMilliSec] # timings in ms
  18. return @ # make it chainable
  19. end: (message) ->
  20. totalTime = deltaMs(@t, @t0)
  21. if totalTime > @LOG_CUTOFF_TIME # log anything greater than cutoff
  22. args = {}
  23. for k,v of @args
  24. args[k] = v
  25. args.updateTimes = @updateTimes
  26. args.start = @start
  27. args.end = new Date()
  28. logger.log args, @name
  29. return totalTime