Profiler.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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('@overleaf/logger')
  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 = 15 * 1000
  23. this.prototype.LOG_SYNC_CUTOFF_TIME = 1000
  24. }
  25. constructor(name, args) {
  26. this.name = name
  27. this.args = args
  28. this.t0 = this.t = process.hrtime()
  29. this.start = new Date()
  30. this.updateTimes = []
  31. this.totalSyncTime = 0
  32. }
  33. log(label, options = {}) {
  34. const t1 = process.hrtime()
  35. const dtMilliSec = deltaMs(t1, this.t)
  36. this.t = t1
  37. this.totalSyncTime += options.sync ? dtMilliSec : 0
  38. this.updateTimes.push([label, dtMilliSec]) // timings in ms
  39. return this // make it chainable
  40. }
  41. end(message) {
  42. const totalTime = deltaMs(this.t, this.t0)
  43. const exceedsCutoff = totalTime > this.LOG_CUTOFF_TIME
  44. const exceedsSyncCutoff = this.totalSyncTime > this.LOG_SYNC_CUTOFF_TIME
  45. if (exceedsCutoff || exceedsSyncCutoff) {
  46. // log anything greater than cutoffs
  47. const args = {}
  48. for (const k in this.args) {
  49. const v = this.args[k]
  50. args[k] = v
  51. }
  52. args.updateTimes = this.updateTimes
  53. args.start = this.start
  54. args.end = new Date()
  55. args.status = { exceedsCutoff, exceedsSyncCutoff }
  56. logger.warn(args, this.name)
  57. }
  58. return totalTime
  59. }
  60. }
  61. Profiler.initClass()
  62. return Profiler
  63. })()