Skip to content

Instantly share code, notes, and snippets.

@nanonanomachine
Created November 18, 2018 07:16
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 nanonanomachine/f3e960a04845d767804398df1381904a to your computer and use it in GitHub Desktop.
Save nanonanomachine/f3e960a04845d767804398df1381904a to your computer and use it in GitHub Desktop.
Insert profile code
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem 'parser'
class Sample
def initialize(a)
@a = a
end
def self.method1
'Hello world'
end
def self.method2(b)
return b if b
0
end
def self.method3(b)
c = b.map do |elm|
elm + 1
end
c | [0]
end
def method4
'Hello world'
end
def method5(b)
return b if b
0
end
def method6(b)
c = b.map do |elm|
elm + 1
end
c | [0]
end
end
class Sample
def initialize(a)
__time = Time.now
@a = a
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
def self.method1
__time = Time.now
'Hello world'
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
def self.method2(b)
__time = Time.now
return b
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" } if b
0
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
def self.method3(b)
__time = Time.now
c = b.map do |elm|
elm + 1
end
c | [0]
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
def method4
__time = Time.now
'Hello world'
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
def method5(b)
__time = Time.now
return b
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" } if b
0
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
def method6(b)
__time = Time.now
c = b.map do |elm|
elm + 1
end
c | [0]
.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }
end
end
# Call bundle exec ruby-rewrite -l time_profiler.rb -m sample.rb
class TimeProfiler < Parser::TreeRewriter
def on_def(node)
insert_profile_on_def(node: node, args: node.children[1].location.expression, code: node.children[2])
super
end
def on_defs(node)
insert_profile_on_def(node: node, args: node.children[2].location.expression, code: node.children[3])
super
end
def on_return(node)
insert_after node.location.expression, "\n#{puts_time}"
super
end
private
def end_definition(node, args)
if args
args
else
node.location.name
end
end
def insert_profile_on_def(node:, args:, code:)
return unless code
insert_after end_definition(node, args), "\n#{start_time}"
unless node.children[-1].type == :return
insert_after node.children[-1].location.expression, "\n#{puts_time}"
end
end
def puts_time
'.tap { puts "#{ __method__.to_s }: #{Time.now - __time}" }'
end
def start_time
'__time = Time.now'
end
end
@nanonanomachine
Copy link
Author

nanonanomachine commented Nov 18, 2018

bundle exec ruby-rewrite -l time_profiler.rb -m sample.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment