mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Add a 'timeAsyncMethod' helper
This commit is contained in:
parent
caeac717fc
commit
3cb0ab2784
2 changed files with 33 additions and 1 deletions
|
@ -10,7 +10,7 @@ destructors = []
|
|||
|
||||
require "./uv_threadpool_size"
|
||||
|
||||
module.exports =
|
||||
module.exports = Metrics =
|
||||
initialize: (_name) ->
|
||||
name = _name
|
||||
|
||||
|
@ -48,6 +48,8 @@ module.exports =
|
|||
event_loop: require "./event_loop"
|
||||
memory: require "./memory"
|
||||
|
||||
timeAsyncMethod: require('./timeAsyncMethod')
|
||||
|
||||
close: () ->
|
||||
for func in destructors
|
||||
func()
|
||||
|
|
30
libraries/metrics/timeAsyncMethod.coffee
Normal file
30
libraries/metrics/timeAsyncMethod.coffee
Normal file
|
@ -0,0 +1,30 @@
|
|||
|
||||
module.exports = (obj, methodName, key, logger) ->
|
||||
metrics = require('./metrics')
|
||||
|
||||
if typeof obj[methodName] != 'function'
|
||||
throw new Error("[Metrics] expected object property #{methodName} to be a function")
|
||||
|
||||
realMethod = obj[methodName]
|
||||
key = "methods.#{key}"
|
||||
|
||||
obj[methodName] = (originalArgs...) ->
|
||||
|
||||
[firstArgs..., callback] = originalArgs
|
||||
if !callback? || typeof callback != 'function'
|
||||
throw new Error(
|
||||
"[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
|
||||
)
|
||||
|
||||
timer = new Metrics.Timer(key)
|
||||
start = new Date()
|
||||
|
||||
realMethod.call this, firstArgs, (callbackArgs...) ->
|
||||
timer.done()
|
||||
elapsedTime = new Date() - start
|
||||
if logger?
|
||||
logger.log
|
||||
key: key
|
||||
time: elapsedTime
|
||||
"[Metrics] timed async method call"
|
||||
callback.apply this, callbackArgs
|
Loading…
Reference in a new issue