# Logs timing of an operation: # # * Start a clock. # * Calls the `operation`, passing in a `done` function that will: # * Stop the clock. # * Log the title of the operation with its elapsed duration and optional outcome. # If the operation calls the `done` function with no outcome, "completed" is used. # # Example: # # request = require("request") # # do_request = (url, title = "Request", callback) -> # log_duration title, (stop_clock) -> # request.get url, (error, response, body) -> # stop_clock(response?.statusCode or "500") # callback(error, body) # log_duration = (title, operation) -> start = Date.now() done = (outcome) -> outcome ||= "completed" end = Date.now() duration = (end - start) / 1000 console.log "#{title} #{outcome} in #{duration} seconds" operation(done)