1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
ENV['RAILS_ENV'] ||= 'production'
require File.expand_path('../../../load_paths', __FILE__)
require 'action_pack'
require 'action_controller'
require 'action_view'
require 'active_model'
require 'benchmark'
MyHash = Class.new(Hash)
Hash.class_eval do
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class Runner
def initialize(app, output)
@app, @output = app, output
end
def puts(*)
super if @output
end
def call(env)
env['n'].to_i.times { @app.call(env) }
@app.call(env).tap { |response| report(env, response) }
end
def report(env, response)
return unless ENV["DEBUG"]
out = env['rack.errors']
out.puts response[0], response[1].to_yaml, '---'
response[2].each { |part| out.puts part }
out.puts '---'
end
def self.puts(*)
super if @output
end
def self.print(*)
super if @output
end
def self.app_and_env_for(action, n)
env = Rack::MockRequest.env_for("/")
env.merge!('n' => n, 'rack.input' => StringIO.new(''), 'rack.errors' => $stdout)
app = lambda { |env| BasePostController.action(action).call(env) }
return app, env
end
$ran = []
def self.run(action, n, output = true)
print "."
STDOUT.flush
@output = output
label = action.to_s
app, env = app_and_env_for(action, n)
t = Benchmark.realtime { new(app, output).call(env) }
$ran << [label, (t * 1000).to_i.to_s] if output
end
def self.done
puts
header, content = "", ""
$ran.each do |k,v|
size = [k.size, v.size].max + 1
header << format("%#{size}s", k)
content << format("%#{size}s", v)
end
puts header
puts content
end
end
ActionController::Base.logger = nil
ActionController::Base.config.compile_methods!
ActionView::Resolver.caching = ENV["RAILS_ENV"] == "production"
class BasePostController < ActionController::Base
append_view_path "#{File.dirname(__FILE__)}/views"
def overhead
self.response_body = ''
end
def index
render :text => ''
end
$OBJECT = {:name => "Hello my name is omg", :address => "333 omg"}
def partial
render :partial => "/collection", :object => $OBJECT
end
def partial_10
render :partial => "/ten_partials"
end
def partial_100
render :partial => "/hundred_partials"
end
$COLLECTION1 = []
10.times do |i|
$COLLECTION1 << { :name => "Hello my name is omg", :address => "333 omg" }
end
def coll_10
render :partial => "/collection", :collection => $COLLECTION1
end
$COLLECTION2 = []
100.times do |i|
$COLLECTION2 << { :name => "Hello my name is omg", :address => "333 omg" }
end
def coll_100
render :partial => "/collection", :collection => $COLLECTION2
end
def uniq_100
render :partial => $COLLECTION2
end
$COLLECTION3 = []
50.times do |i|
$COLLECTION3 << {:name => "Hello my name is omg", :address => "333 omg"}
$COLLECTION3 << MyHash.new(:name => "Hello my name is omg", :address => "333 omg")
end
def diff_100
render :partial => $COLLECTION3
end
def template_1
render :template => "template"
end
module Foo
def omg
"omg"
end
end
helper Foo
end
N = (ENV['N'] || 1000).to_i
# ActionController::Base.use_accept_header = false
def run_all!(times, verbose)
Runner.run(:overhead, times, verbose)
Runner.run(:index, times, verbose)
Runner.run(:template_1, times, verbose)
Runner.run(:partial, times, verbose)
Runner.run(:partial_10, times, verbose)
Runner.run(:coll_10, times, verbose)
Runner.run(:partial_100, times, verbose)
Runner.run(:coll_100, times, verbose)
Runner.run(:uniq_100, times, verbose)
Runner.run(:diff_100, times, verbose)
end
if ENV["PROFILE"]
Runner.run(ENV["PROFILE"].to_sym, 1, false)
require "ruby-prof"
RubyProf.start
Runner.run(ENV["PROFILE"].to_sym, N, true)
result = RubyProf.stop
printer = RubyProf::CallStackPrinter.new(result)
printer.print(File.open("output.html", "w"))
else
run_all!(1, false)
(ENV["M"] || 1).to_i.times do
$ran = []
run_all!(N, true)
Runner.done
end
end
|