aboutsummaryrefslogtreecommitdiffstats
path: root/guides/bug_report_templates/benchmark.rb
blob: 4a8ce787c774cdfa44c4ca2db7e8362638f4a306 (plain) (blame)
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
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails"
  gem "benchmark-ips"
end

require "active_support"
require "active_support/core_ext/object/blank"

# Your patch goes here.
class String
  def fast_blank?
    true
  end
end

# Enumerate some representative scenarios here.
#
# It is very easy to make an optimization that improves performance for a
# specific scenario you care about but regresses on other common cases.
# Therefore, you should test your change against a list of representative
# scenarios. Ideally, they should be based on real-world scenarios extracted
# from production applications.
SCENARIOS = {
  "Empty"            => "",
  "Single Space"     => " ",
  "Two Spaces"       => "  ",
  "Mixed Whitspaces" => " \t\r\n",
  "Very Long String" => " " * 100
}

SCENARIOS.each_pair do |name, value|
  puts
  puts " #{name} ".center(80, "=")
  puts

  Benchmark.ips do |x|
    x.report("blank?")      { value.blank? }
    x.report("fast_blank?") { value.fast_blank? }
    x.compare!
  end
end