Created
March 24, 2015 13:05
log_duration.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment