-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.rb
124 lines (98 loc) · 2.07 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
require 'benchmark/ips'
require 'kwattr'
require_relative 'spec/examples'
class ROneAttr
attr_reader :foo
def initialize(foo:)
@foo = foo
end
end
class ROneAttrDefault
DEFAULT_FOO = 42
attr_reader :foo
def initialize(foo: DEFAULT_FOO)
@foo = foo
end
end
class ROneAttrOnePositional
attr_reader :foo, :bar
def initialize(bar, foo:)
@foo = foo
@bar = bar
end
end
class RTwoAttrs
attr_reader :foo, :bar
def initialize(foo:, bar:)
@foo = foo
@bar = bar
end
end
class RTwoAttrsDefaults
DEFAULT_FOO = 42
DEFAULT_BAR = 21
attr_reader :foo, :bar
def initialize(foo: DEFAULT_FOO, bar: DEFAULT_BAR)
@foo = foo
@bar = bar
end
end
class RTwoAttrsOneDefault
DEFAULT_BAR = 21
attr_reader :foo, :bar
def initialize(foo:, bar: DEFAULT_BAR)
@foo = foo
@bar = bar
end
end
class RTwoAttrsOnePos
attr_reader :foo, :bar, :baz
def initialize(baz, foo:, bar:)
@foo = foo
@bar = bar
@baz = baz
end
end
class RTwoAttrsInheritsOneAttr < ROneAttr
attr_reader :bar
def initialize(foo:, bar:)
super(foo: foo)
@bar = bar
end
end
module ROneAttrModule
attr_reader :foo
def initialize(foo:)
@foo = foo
end
end
class ROneAttrViaModule
include ROneAttrModule
end
Benchmark.ips do |b|
b.warmup = ENV['CONTINUOUS_INTEGRATION'] ? 1 : 0.1
b.time = ENV['CONTINUOUS_INTEGRATION'] ? 5 : 1
b.report "KWAttr classes" do
OneAttr.new foo: 42
OneAttrDefault.new
OneAttrOnePositional.new 42, foo: 21
TwoAttrs.new foo: 42, bar: 21
TwoAttrsDefaults.new
TwoAttrsOneDefault.new foo: 42
TwoAttrsOnePos.new 42, foo: 21, bar: 14
TwoAttrsInheritsOneAttr.new foo: 42, bar: 21
OneAttrViaModule.new foo: 42
end
b.report "Plain classes" do
ROneAttr.new foo: 42
ROneAttrDefault.new
ROneAttrOnePositional.new 42, foo: 21
RTwoAttrs.new foo: 42, bar: 21
RTwoAttrsDefaults.new
RTwoAttrsOneDefault.new foo: 42
RTwoAttrsOnePos.new 42, foo: 21, bar: 14
RTwoAttrsInheritsOneAttr.new foo: 42, bar: 21
ROneAttrViaModule.new foo: 42
end
b.compare!
end