-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.rb
208 lines (180 loc) · 5 KB
/
benchmark.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
$: << 'lib'
require 'bound'
require 'benchmark'
TestBoundary = Bound.required(
:foo,
:bar => [Bound.required(:abc)],
:baz => Bound.required(:gonzo)
)
StructBoundary = Struct.new(:foo, :bar, :baz)
BarStructBoundary = Struct.new(:abc)
BazStructBoundary = Struct.new(:gonzo)
StaticBoundClass = Class.new do
def self.initialize_unvalidated
class_eval <<-EOR
def initialize(target, overwrite = nil)
@t, @o = target, overwrite
end
EOR
end
def self.define_delegate(attr, prefix = '')
class_eval <<-EOR
def #{prefix}#{attr}
return @o[:#{attr}] if @o && @o.key?(:#{attr})
@t.kind_of?(Hash)? @t[:#{attr}] : @t.#{attr}
end
EOR
end
def self.define_nested_delegate(attr, nested_class)
define_delegate attr, 'get_'
if nested_class.kind_of? Array
nested_class = nested_class.first
class_eval <<-EOR
def #{attr}
@#{attr} ||= get_#{attr}.map{|t| #{nested_class}.new t}
end
private :get_#{attr}
EOR
else
class_eval <<-EOR
def #{attr}
@#{attr} ||= #{nested_class}.new(get_#{attr})
end
private :get_#{attr}
EOR
end
end
end
BarStaticBoundary = Class.new(StaticBoundClass) do
initialize_unvalidated
define_delegate :abc
end
BazStaticBoundary = Class.new(StaticBoundClass) do
initialize_unvalidated
define_delegate :gonzo
end
StaticBoundary = Class.new(StaticBoundClass) do
initialize_unvalidated
define_delegate :foo
define_nested_delegate :bar, [BarStaticBoundary]
define_nested_delegate :baz, BazStaticBoundary
end
def assert_correctness(bound)
raise('foo is wrong') unless bound.foo == 'NOPE'
raise('bar size is wrong') unless bound.bar.size == 2
raise('bar[0] is wrong') unless bound.bar[0].abc == 'TRUE'
raise('bar[1] is wrong') unless bound.bar[1].abc == 'FALSE'
raise('baz.gonzo is wrong') unless bound.baz.gonzo == 22
end
def bench(key, &block)
profiled do
result = nil
time = Benchmark.realtime { result = block.call }
puts "Benchmarking '#{key}' --> #{time}ms"
result
end
end
def profiled
if ENV['PROFILE']
require 'hotch'
Hotch { yield }
else
yield
end
end
Provider = Class.new do
attr_accessor :foo, :bar, :baz
BarProvider = Class.new do
attr_accessor :abc
end
BazProvider = Class.new do
attr_accessor :gonzo
end
end
provider_objects = 100_000.times.map do |i|
Provider.new.tap do |p|
p.foo = 'YES'
p.bar = [
BarProvider.new.tap do |brp|
brp.abc = 'TRUE'
end,
BarProvider.new.tap do |brp|
brp.abc = 'FALSE'
end
]
p.baz = BazProvider.new.tap do |bzp|
bzp.gonzo = 22
end
end
end
provider_hashes = 100_000.times.map do |i|
{
:foo => 'YES',
:bar => [{:abc => 'TRUE'}, {:abc => 'FALSE'}],
:baz => {:gonzo => 22}
}
end
overwrite = {:foo => 'NOPE'}
bench ' bound w/ objt' do
provider_objects.each do |provider|
result = TestBoundary.new(provider, overwrite)
assert_correctness result
end
end
bench ' bound w/ hash' do
provider_hashes.each do |provider|
result = TestBoundary.new(provider, overwrite)
assert_correctness result
end
end
Bound.disable_validation
bench 'bound noval w/ objt' do
provider_objects.each do |provider|
result = TestBoundary.new(provider, overwrite)
assert_correctness result
end
end
bench 'bound noval w/ hash' do
provider_hashes.each do |provider|
result = TestBoundary.new(provider, overwrite)
assert_correctness result
end
end
bench 'staticbound w/ objt' do
provider_objects.each do |provider|
result = StaticBoundary.new(provider, overwrite)
assert_correctness result
end
end
bench 'staticbound w/ hash' do
provider_hashes.each do |provider|
result = StaticBoundary.new(provider, overwrite)
assert_correctness result
end
end
bench 'structbound w/ objt' do
provider_objects.each do |provider|
result = StructBoundary.new(
overwrite[:foo],
[
BarStructBoundary.new(provider.bar[0].abc),
BarStructBoundary.new(provider.bar[1].abc),
],
BazStructBoundary.new(provider.baz.gonzo)
)
assert_correctness result
end
end
bench 'structbound w/ hash' do
provider_hashes.map do |provider|
result = StructBoundary.new(
overwrite[:foo],
[
BarStructBoundary.new(provider[:bar][0][:abc]),
BarStructBoundary.new(provider[:bar][1][:abc]),
],
BazStructBoundary.new(provider[:baz][:gonzo])
)
assert_correctness result
end
end