Created
May 28, 2020 15:31
-
-
Save electron0zero/3c9e0153e67591556b89e5366bafa7d1 to your computer and use it in GitHub Desktop.
Perf - low overhead, dead simple way to time you code, useful when you want to time things in prod
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
# frozen_string_literal: true | |
# time some code with name, and dump it in logfile | |
# can be used to find how much time is going in each part | |
# | |
# set perf markers in the code | |
# pros: | |
# - low overhead, gives high level picture | |
# cons: | |
# - needs marking | |
# | |
# start = Perf.start('precomp') | |
# # <code we want to time> | |
# Perf.end(start) | |
# | |
# this can be part of code, and common parts can be instrumented using this | |
# TODO: move away from logger to sending data to ELK or graphite(in async way) so we can | |
# plot it as stack chart | |
class Perf | |
LOGGER = Logger.new('log/perf_time.log') | |
def self.start(name) | |
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
return { name: name, start_time: start_time } | |
end | |
# @param [Hash] return of start call | |
def self.end(start) | |
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
LOGGER.info("tid: #{Thread.current.object_id}, "\ | |
"name: #{start[:name]}, "\ | |
"duration: #{end_time - start[:start_time]}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment