diff options
Diffstat (limited to 'tools/profile')
-rwxr-xr-x | tools/profile | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/tools/profile b/tools/profile index 62daa2f0bd..a35dd18b77 100755 --- a/tools/profile +++ b/tools/profile @@ -6,9 +6,11 @@ ENV['RAILS_ENV'] ||= 'development' module CodeTools class Profiler + Error = Class.new(StandardError) attr_reader :path, :mode def initialize(path, mode=nil) + assert_ruby_file_exists(path) @path, @mode = path, mode require 'benchmark' end @@ -60,6 +62,16 @@ module CodeTools puts "%8.1f ms %d KB RSS" % [elapsed * 1000, after_rss - before_rss] end + private + + def assert_ruby_file_exists(path) + fail Error.new("No such file") unless File.exists?(path) + fail Error.new("#{path} is a directory") if File.directory?(path) + ruby_extension = File.extname(path) == '.rb' + ruby_executable = File.open(path, 'rb') {|f| f.readline } =~ [/\A#!.*ruby/] + fail Error.new("Not a ruby file") unless ruby_extension or ruby_executable + end + module RequireProfiler private def require(file, *args) RequireProfiler.profile(file) { super } end |