Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 208 lines (171 sloc) 4.647 kb
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
1 ENV['RAILS_ENV'] ||= 'production'
2 ENV['NO_RELOAD'] ||= '1'
3
ee677a7 @josevalim Updates to work on Rails master.
josevalim authored
4 require 'rubygems'
5 require 'ruby-prof'
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
6
ee677a7 @josevalim Updates to work on Rails master.
josevalim authored
7 $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../.."
8 require File.expand_path("../../../load_paths", __FILE__)
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
9
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
10 require 'action_pack'
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
11 require 'action_controller'
12 require 'action_view'
13 require 'benchmark'
14
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
15 MyHash = Class.new(Hash)
16
17 if ActionPack::VERSION::MAJOR > 2
18 require 'active_model'
19
20 Hash.class_eval do
21 extend ActiveModel::Naming
22 include ActiveModel::Conversion
23 end
24 end
25
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
26 class Runner
27 def initialize(app, output)
28 @app, @output = app, output
29 end
30
31 def puts(*)
32 super if @output
33 end
34
35 def call(env)
36 env['n'].to_i.times { @app.call(env) }
37 @app.call(env).tap { |response| report(env, response) }
38 end
39
40 def report(env, response)
41 return unless ENV["DEBUG"]
42 out = env['rack.errors']
43 out.puts response[0], response[1].to_yaml, '---'
44 response[2].each { |part| out.puts part }
45 out.puts '---'
46 end
47
48 def self.puts(*)
49 super if @output
50 end
51
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
52 def self.print(*)
53 super if @output
54 end
55
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
56 if ActionPack::VERSION::MAJOR < 3
57 def self.app_and_env_for(action, n)
58 env = Rack::MockRequest.env_for("/?action=#{action}")
59 env.merge!('n' => n, 'rack.input' => StringIO.new(''), 'rack.errors' => $stdout)
60 app = BasePostController
61 return app, env
62 end
63 else
64 def self.app_and_env_for(action, n)
65 env = Rack::MockRequest.env_for("/")
66 env.merge!('n' => n, 'rack.input' => StringIO.new(''), 'rack.errors' => $stdout)
ee677a7 @josevalim Updates to work on Rails master.
josevalim authored
67 app = lambda { |env| BasePostController.action(action).call(env) }
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
68 return app, env
69 end
70 end
71
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
72 $ran = []
73
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
74 def self.run(action, n, output = true)
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
75 print "."
76 STDOUT.flush
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
77 @output = output
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
78 label = action.to_s
79 app, env = app_and_env_for(action, n)
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
80 t = Benchmark.realtime { new(app, output).call(env) }
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
81 $ran << [label, (t * 1000).to_i.to_s] if output
82 end
83
84 def self.done
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
85 puts
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
86 header, content = "", ""
87 $ran.each do |k,v|
88 size = [k.size, v.size].max + 1
89 header << format("%#{size}s", k)
90 content << format("%#{size}s", v)
91 end
92 puts header
93 puts content
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
94 end
95 end
96
97 module ActionController::Rails2Compatibility
98 instance_methods.each do |name|
99 remove_method name
100 end
101 end
102
103 class BasePostController < ActionController::Base
104 append_view_path "#{File.dirname(__FILE__)}/views"
105
106 if ActionPack::VERSION::MAJOR > 2
107 def overhead
108 self.response_body = ''
109 end
110 end
111
112 def index
113 render :text => ''
114 end
115
116 $OBJECT = {:name => "Hello my name is omg", :address => "333 omg"}
117
118 def partial
119 render :partial => "/collection", :object => $OBJECT
120 end
121
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
122 def partial_10
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
123 render :partial => "/ten_partials"
124 end
125
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
126 def partial_100
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
127 render :partial => "/hundred_partials"
128 end
129
130 $COLLECTION1 = []
131 10.times do |i|
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
132 $COLLECTION1 << { :name => "Hello my name is omg", :address => "333 omg" }
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
133 end
134
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
135 def coll_10
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
136 render :partial => "/collection", :collection => $COLLECTION1
137 end
138
139 $COLLECTION2 = []
140 100.times do |i|
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
141 $COLLECTION2 << { :name => "Hello my name is omg", :address => "333 omg" }
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
142 end
143
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
144 def coll_100
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
145 render :partial => "/collection", :collection => $COLLECTION2
146 end
147
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
148 def uniq_100
149 render :partial => $COLLECTION2
150 end
151
152 $COLLECTION3 = []
153 50.times do |i|
154 $COLLECTION3 << {:name => "Hello my name is omg", :address => "333 omg"}
155 $COLLECTION3 << MyHash.new(:name => "Hello my name is omg", :address => "333 omg")
156 end
157
158 def diff_100
159 render :partial => $COLLECTION3
160 end
161
162 def template_1
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
163 render :template => "template"
164 end
a954e06 Updated to improve reusable output and updated for Rails 3 current.
Yehuda Katz authored
165
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
166 module Foo
167 def omg
168 "omg"
169 end
170 end
171 helper Foo
172 end
173
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
174 N = (ENV['N'] || 1000).to_i
77cece3 @josevalim Remove unused config.
josevalim authored
175 # ActionController::Base.use_accept_header = false
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
176
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
177 def run_all!(times, verbose)
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
178 if ActionPack::VERSION::MAJOR > 2
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
179 Runner.run(:overhead, times, verbose)
180 end
181 Runner.run(:index, times, verbose)
182 Runner.run(:template_1, times, verbose)
183 Runner.run(:partial, times, verbose)
184 Runner.run(:partial_10, times, verbose)
185 Runner.run(:coll_10, times, verbose)
186 Runner.run(:partial_100, times, verbose)
187 Runner.run(:coll_100, times, verbose)
188 Runner.run(:uniq_100, times, verbose)
189 Runner.run(:diff_100, times, verbose)
190 end
191
192 unless ENV["PROFILE"]
193 run_all!(1, false)
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
194
195 (ENV["M"] || 1).to_i.times do
ca61e90 @josevalim View names refactoring and added uniq_100 and diff_100.
josevalim authored
196 $ran = []
197 run_all!(N, true)
198 Runner.done
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
199 end
200 else
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
201 Runner.run(ENV["PROFILE"].to_sym, 1, false)
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
202 require "ruby-prof"
203 RubyProf.start
2cd2bb6 @wycats Make minimal.rb work on Rails 2.3 and edge
authored
204 Runner.run(ENV["PROFILE"].to_sym, N, true)
4e1327f @wycats Initial commit of some reusable Rails benchmarks
authored
205 result = RubyProf.stop
206 printer = RubyProf::CallStackPrinter.new(result)
207 printer.print(File.open("output.html", "w"))
208 end
Something went wrong with that request. Please try again.