Skip to content

Instantly share code, notes, and snippets.

@electron0zero
Created May 28, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electron0zero/3c9e0153e67591556b89e5366bafa7d1 to your computer and use it in GitHub Desktop.
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
# 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