File: benchmarks.rb

package info (click to toggle)
ruby-memo-wise 1.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 384 kB
  • sloc: ruby: 2,663; makefile: 4; sh: 4
file content (244 lines) | stat: -rw-r--r-- 7,494 bytes parent folder | download
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# frozen_string_literal: true

require "benchmark/ips"

require "tempfile"
require "memo_wise"

# Some gems do not yet work in Ruby 3 so we only require them if they're loaded
# in the Gemfile.
%w[memery memoist memoized memoizer ddmemoize dry-core].
  each { |gem| require gem if Gem.loaded_specs.key?(gem) }

# The VERSION constant does not get loaded above for these gems.
%w[memoized memoizer].
  each { |gem| require "#{gem}/version" if Gem.loaded_specs.key?(gem) }

# The Memoizable module from dry-core needs to be required manually
require "dry/core/memoizable" if Gem.loaded_specs.key?("dry-core")

class BenchmarkSuiteWithoutGC
  def warming(*)
    run_gc
  end

  def running(*)
    run_gc
  end

  def warmup_stats(*); end

  def add_report(*); end

  private

  def run_gc
    GC.enable
    GC.start
    GC.disable
  end
end
suite = BenchmarkSuiteWithoutGC.new

BenchmarkGem = Struct.new(:klass, :activation_code, :memoization_method) do
  def benchmark_name
    "#{klass} (#{klass::VERSION})"
  end
end

# We alphabetize this list for easier readability, but shuffle the list before
# using it to minimize the chance that our benchmarks are affected by ordering.
# NOTE: Some gems do not yet work in Ruby 3 so we only test with them if they've
# been `require`d.
BENCHMARK_GEMS = [
  BenchmarkGem.new(MemoWise, "prepend MemoWise", :memo_wise),
  (BenchmarkGem.new(DDMemoize, "DDMemoize.activate(self)", :memoize) if defined?(DDMemoize)),
  (BenchmarkGem.new(Dry::Core, "include Dry::Core::Memoizable", :memoize) if defined?(Dry::Core)),
  (BenchmarkGem.new(Memery, "include Memery", :memoize) if defined?(Memery)),
  (BenchmarkGem.new(Memoist, "extend Memoist", :memoize) if defined?(Memoist)),
  (BenchmarkGem.new(Memoized, "include Memoized", :memoize) if defined?(Memoized)),
  (BenchmarkGem.new(Memoizer, "include Memoizer", :memoize) if defined?(Memoizer))
].compact.shuffle

# Use metaprogramming to ensure that each class is created in exactly the
# the same way.
BENCHMARK_GEMS.each do |benchmark_gem|
  eval <<~HEREDOC, binding, __FILE__, __LINE__ + 1 # rubocop:disable Security/Eval
    class #{benchmark_gem.klass}Example
      #{benchmark_gem.activation_code}

      def no_args
        100
      end
      #{benchmark_gem.memoization_method} :no_args

      def one_positional_arg(a)
        100
      end
      #{benchmark_gem.memoization_method} :one_positional_arg

      def positional_args(a, b)
        100
      end
      #{benchmark_gem.memoization_method} :positional_args

      def one_keyword_arg(a:)
        100
      end
      #{benchmark_gem.memoization_method} :one_keyword_arg

      def keyword_args(a:, b:)
        100
      end
      #{benchmark_gem.memoization_method} :keyword_args

      def positional_and_keyword_args(a, b:)
        100
      end
      #{benchmark_gem.memoization_method} :positional_and_keyword_args

      def positional_and_splat_args(a, *args)
        100
      end
      #{benchmark_gem.memoization_method} :positional_and_splat_args

      def keyword_and_double_splat_args(a:, **kwargs)
        100
      end
      #{benchmark_gem.memoization_method} :keyword_and_double_splat_args

      def positional_splat_keyword_and_double_splat_args(a, *args, b:, **kwargs)
        100
      end
      #{benchmark_gem.memoization_method} :positional_splat_keyword_and_double_splat_args
    end
  HEREDOC
end

N_RESULT_DECIMAL_DIGITS = 2

# Each method within these benchmarks is initially run once to memoize the
# result value, so our benchmark only tests memoized retrieval time.
benchmark_lambdas = [
  lambda do |x, instance, benchmark_gem|
    instance.no_args

    x.report("#{benchmark_gem.benchmark_name}: ()") do
      instance.no_args
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.one_positional_arg(1)

    x.report("#{benchmark_gem.benchmark_name}: (a)") do
      instance.one_positional_arg(1)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.positional_args(1, 2)

    x.report("#{benchmark_gem.benchmark_name}: (a, b)") do
      instance.positional_args(1, 2)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.one_keyword_arg(a: 1)

    x.report("#{benchmark_gem.benchmark_name}: (a:)") do
      instance.one_keyword_arg(a: 1)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.keyword_args(a: 1, b: 2)

    x.report("#{benchmark_gem.benchmark_name}: (a:, b:)") do
      instance.keyword_args(a: 1, b: 2)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.positional_and_keyword_args(1, b: 2)

    x.report("#{benchmark_gem.benchmark_name}: (a, b:)") do
      instance.positional_and_keyword_args(1, b: 2)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.positional_and_splat_args(1, 2)

    x.report("#{benchmark_gem.benchmark_name}: (a, *args)") do
      instance.positional_and_splat_args(1, 2)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.keyword_and_double_splat_args(a: 1, b: 2)

    x.report("#{benchmark_gem.benchmark_name}: (a:, **kwargs)") do
      instance.keyword_and_double_splat_args(a: 1, b: 2)
    end
  end,
  lambda do |x, instance, benchmark_gem|
    instance.positional_splat_keyword_and_double_splat_args(1, 2, b: 3, a: 4)

    x.report("#{benchmark_gem.benchmark_name}: (a, *args, b:, **kwargs)") do
      instance.positional_splat_keyword_and_double_splat_args(1, 2, b: 3, a: 4)
    end
  end
]

# We benchmark different cases separately, to ensure that slow performance in
# one method or code path isn't hidden by fast performance in another.
benchmark_lambdas.map do |benchmark|
  json_file = Tempfile.new

  Benchmark.ips do |x|
    x.config(suite: suite)
    BENCHMARK_GEMS.each do |benchmark_gem|
      instance = Object.const_get("#{benchmark_gem.klass}Example").new

      benchmark.call(x, instance, benchmark_gem)
    end

    x.compare!
    x.json! json_file.path
  end

  JSON.parse(json_file.read)
end.each_with_index do |benchmark_json, i|
  # We print a comparison table after we run each benchmark to copy into our
  # README.md

  # MemoWise will not appear in the comparison table, but we will use it to
  # compare against other gems' benchmarks
  memo_wise = benchmark_json.find { _1["name"].include?("MemoWise") }
  benchmark_json.delete(memo_wise)

  # Sort benchmarks by gem name to alphabetize our final output table.
  benchmark_json.sort_by! { _1["name"] }

  # Print headers based on the first benchmark_json
  if i.zero?
    benchmark_headers = benchmark_json.map do |benchmark_gem|
      # Gem name is of the form:
      # "MemoWise (1.1.0): ()"
      # We use this mapping to get a header of the form
      # "`MemoWise` (1.1.0)
      gem_name_parts = benchmark_gem["name"].split
      "`#{gem_name_parts[0]}` #{gem_name_parts[1][...-1]}"
    end.join("|")
    puts "|Method arguments|#{benchmark_headers}|"
    puts "#{'|--' * (benchmark_json.size + 1)}|"
  end

  output_str = benchmark_json.map do |bgem|
    # "%.2f" % 12.345 => "12.34" (instead of "12.35")
    #   See: https://bugs.ruby-lang.org/issues/12548
    # 1.00.round(2).to_s => "1.0" (instead of "1.00")
    #
    # So to round and format correctly, we first use Float#round and then %
    "%.#{N_RESULT_DECIMAL_DIGITS}fx" %
      (memo_wise["central_tendency"] / bgem["central_tendency"]).round(N_RESULT_DECIMAL_DIGITS)
  end.join("|")

  name = memo_wise["name"].partition(": ").last
  puts "|`#{name}`#{' (none)' if name == '()'}|#{output_str}|"
end