Profiler.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS206: Consider reworking classes to avoid initClass
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. let Profiler
  12. const Settings = require('@overleaf/settings')
  13. const logger = require('logger-sharelatex')
  14. const deltaMs = function (ta, tb) {
  15. const nanoSeconds = (ta[0] - tb[0]) * 1e9 + (ta[1] - tb[1])
  16. const milliSeconds = Math.floor(nanoSeconds * 1e-6)
  17. return milliSeconds
  18. }
  19. module.exports = Profiler = (function () {
  20. Profiler = class Profiler {
  21. static initClass() {
  22. this.prototype.LOG_CUTOFF_TIME = 1000
  23. }
  24. constructor(name, args) {
  25. this.name = name
  26. this.args = args
  27. this.t0 = this.t = process.hrtime()
  28. this.start = new Date()
  29. this.updateTimes = []
  30. }
  31. log(label) {
  32. const t1 = process.hrtime()
  33. const dtMilliSec = deltaMs(t1, this.t)
  34. this.t = t1
  35. this.updateTimes.push([label, dtMilliSec]) // timings in ms
  36. return this // make it chainable
  37. }
  38. end(message) {
  39. const totalTime = deltaMs(this.t, this.t0)
  40. if (totalTime > this.LOG_CUTOFF_TIME) {
  41. // log anything greater than cutoff
  42. const args = {}
  43. for (const k in this.args) {
  44. const v = this.args[k]
  45. args[k] = v
  46. }
  47. args.updateTimes = this.updateTimes
  48. args.start = this.start
  49. args.end = new Date()
  50. logger.log(args, this.name)
  51. }
  52. return totalTime
  53. }
  54. }
  55. Profiler.initClass()
  56. return Profiler
  57. })()