aboutsummaryrefslogtreecommitdiffstats
path: root/tools/profile
diff options
context:
space:
mode:
authorBenjamin Fleischer <github@benjaminfleischer.com>2014-07-25 13:23:54 -0500
committerBenjamin Fleischer <github@benjaminfleischer.com>2014-07-25 13:41:18 -0500
commit3cbeb8d8eaf217f8eb38f5ea26f766f5cd978ff3 (patch)
tree61ffa106c3efd59531b286fecd50e5d73455ea5e /tools/profile
parent54a4065074485ece05c4fe598d16d108cb292d88 (diff)
downloadrails-3cbeb8d8eaf217f8eb38f5ea26f766f5cd978ff3.tar.gz
rails-3cbeb8d8eaf217f8eb38f5ea26f766f5cd978ff3.tar.bz2
rails-3cbeb8d8eaf217f8eb38f5ea26f766f5cd978ff3.zip
Fail profiler fast when input is not a ruby file
[ci skip]
Diffstat (limited to 'tools/profile')
-rwxr-xr-xtools/profile12
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