Skip to content
Browse files

Fail profiler fast when input is not a ruby file

[ci skip]
  • Loading branch information...
1 parent 54a4065 commit 3cbeb8d8eaf217f8eb38f5ea26f766f5cd978ff3 @bf4 bf4 committed Jul 25, 2014
Showing with 12 additions and 0 deletions.
  1. +12 −0 tools/profile
View
12 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

0 comments on commit 3cbeb8d

Please sign in to comment.
Something went wrong with that request. Please try again.