From 624d5bb176de5212bea43a1c57a95f48676c6e9f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 14 Apr 2005 08:23:50 +0000 Subject: Added script/benchmarker to easily benchmark one or more statement a number of times from within the environment. Added script/profiler to easily profile a single statement from within the environment git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1166 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/CHANGELOG | 14 ++++++++++++++ railties/Rakefile | 2 +- railties/bin/benchmarker | 16 ++++++++++++++++ railties/bin/console_sandbox | 6 ++++++ railties/bin/console_sandbox.rb | 6 ------ railties/bin/profiler | 14 ++++++++++++++ .../generators/applications/app/app_generator.rb | 2 +- 7 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 railties/bin/benchmarker create mode 100644 railties/bin/console_sandbox delete mode 100644 railties/bin/console_sandbox.rb create mode 100644 railties/bin/profiler (limited to 'railties') diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 7a0d0ad1cc..0ff8f05b95 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,19 @@ *SVN* +* Added script/benchmarker to easily benchmark one or more statement a number of times from within the environment. Examples: + + # runs the one statement 10 times + script/benchmarker 10 'Person.expensive_method(10)' + + # pits the two statements against each other with 50 runs each + script/benchmarker 50 'Person.expensive_method(10)' 'Person.cheap_method(10)' + + +* Added script/profiler to easily profile a single statement from within the environment. Examples: + + script/profiler 'Person.expensive_method(10)' + script/profiler 'Person.expensive_method(10)' 10 # runs the statement 10 times + * Added Rake target clear_logs that'll truncate all the *.log files in log/ to zero #1079 [Lucas Carlson] * Added lazy typing for generate, such that ./script/generate cn == ./script/generate controller and the likes #1051 [k@v2studio.com] diff --git a/railties/Rakefile b/railties/Rakefile index 6b0bd8c7c1..b18d5e20ea 100644 --- a/railties/Rakefile +++ b/railties/Rakefile @@ -26,7 +26,7 @@ TEST_DIRS = %w( fixtures unit functional mocks mocks/development mocks/test ) LOG_FILES = %w( server.log development.log test.log production.log ) HTML_FILES = %w( 404.html 500.html index.html favicon.ico javascripts/prototype.js ) -BIN_FILES = %w( generate destroy breakpointer console server update runner ) +BIN_FILES = %w( generate destroy breakpointer console console_sandbox server update runner profiler benchmarker ) VENDOR_LIBS = %w( actionpack activerecord actionmailer activesupport actionwebservice railties ) diff --git a/railties/bin/benchmarker b/railties/bin/benchmarker new file mode 100644 index 0000000000..a5814d98a6 --- /dev/null +++ b/railties/bin/benchmarker @@ -0,0 +1,16 @@ +#!/usr/local/bin/ruby + +if ARGV.empty? + puts "Usage: benchmarker times 'Person.expensive_way' 'Person.another_expensive_way' ..." + exit +end + +require File.dirname(__FILE__) + '/../config/environment' +require 'benchmark' +include Benchmark + +bm(6) do |x| + ARGV[1..-1].each_with_index do |expression, idx| + x.report("##{idx + 1}") { ARGV[0].to_i.times { eval(expression) } } + end +end \ No newline at end of file diff --git a/railties/bin/console_sandbox b/railties/bin/console_sandbox new file mode 100644 index 0000000000..80f3dbc223 --- /dev/null +++ b/railties/bin/console_sandbox @@ -0,0 +1,6 @@ +ActiveRecord::Base.lock_mutex +ActiveRecord::Base.connection.begin_db_transaction +at_exit do + ActiveRecord::Base.connection.rollback_db_transaction + ActiveRecord::Base.unlock_mutex +end diff --git a/railties/bin/console_sandbox.rb b/railties/bin/console_sandbox.rb deleted file mode 100644 index 80f3dbc223..0000000000 --- a/railties/bin/console_sandbox.rb +++ /dev/null @@ -1,6 +0,0 @@ -ActiveRecord::Base.lock_mutex -ActiveRecord::Base.connection.begin_db_transaction -at_exit do - ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.unlock_mutex -end diff --git a/railties/bin/profiler b/railties/bin/profiler new file mode 100644 index 0000000000..aca84055c2 --- /dev/null +++ b/railties/bin/profiler @@ -0,0 +1,14 @@ +#!/usr/local/bin/ruby + +if ARGV.empty? + puts "Usage: profiler 'Person.expensive_method(10)' [times]" + exit +end + +require File.dirname(__FILE__) + '/../config/environment' +require "profiler" + +Profiler__::start_profile +(ARGV[1] || 1).to_i.times { eval(ARGV.first) } +Profiler__::stop_profile +Profiler__::print_profile($stdout) \ No newline at end of file diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb index 118dc0858a..765c91e406 100644 --- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb +++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb @@ -43,7 +43,7 @@ class AppGenerator < Rails::Generator::Base m.file "environments/test.rb", "config/environments/test.rb" # Scripts - %w(console console_sandbox.rb destroy generate server runner).each do |file| + %w(console console_sandbox destroy generate server runner benchmarker profiler).each do |file| m.file "bin/#{file}", "script/#{file}", script_options end if options[:gem] -- cgit v1.2.3