Module: BenchmarkRunner::Term

Defined in:
lib/tasks/benchmark_runner.rb

Overview

Pretty terminal formatting for benchmark output

Constant Summary collapse

CLEAR =
"\e[0m"
BOLD =
"\e[1m"
DIM =
"\e[2m"
BLACK =
"\e[30m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
BLUE =
"\e[34m"
MAGENTA =
"\e[35m"
CYAN =
"\e[36m"
WHITE =
"\e[37m"
BG_RED =
"\e[41m"
BG_GREEN =
"\e[42m"
HL =

Box-drawing

""
VL =
""
TL =
""
TR =
""
BL =
""
BR =
""
TT =
""
BT =
""
LT =
""
RT =
""
CROSS =
""
SPINNER =

Spinner frames

%w[         ].freeze

Class Method Summary collapse

Class Method Details

.category(title, icon:, description:, failure_means:, compare_against: nil) ⇒ Object

Category section with description



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/tasks/benchmark_runner.rb', line 105

def self.category(title, icon:, description:, failure_means:,
compare_against: nil)
  puts
  puts "#{CYAN}#{VL}#{CLEAR}  #{BOLD}#{MAGENTA}#{icon} #{title}#{CLEAR}"
  puts

  # Description
  puts "  #{DIM}#{description}#{CLEAR}"
  puts

  # What we compare
  if compare_against
    puts "  #{CYAN}Comparing against:#{CLEAR} #{compare_against}"
    puts
  end

  # What failure means
  puts "  #{YELLOW}⚠️  Failure means:#{CLEAR} #{failure_means}"
  puts

  sep(width: 76)
  puts
end

.clear_spinnerObject



61
62
63
64
# File 'lib/tasks/benchmark_runner.rb', line 61

def self.clear_spinner
  print "\b \b"
  $stdout.flush
end

.env_info(ruby_version, platform) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/tasks/benchmark_runner.rb', line 96

def self.env_info(ruby_version, platform)
  puts
  puts "  #{DIM}Environment:#{CLEAR}"
  puts "  #{VL}  Ruby #{ruby_version} on #{platform}#{' ' * (60 - ruby_version.length - platform.length)}#{VL}"
  puts "  #{DIM}#{BL}#{HL * 76}#{BR}#{CLEAR}"
  puts
end

.fail(msg = nil) ⇒ Object



70
71
72
73
# File 'lib/tasks/benchmark_runner.rb', line 70

def self.fail(msg = nil)
  puts " #{RED}#{CLEAR}"
  puts "  #{RED}#{msg}#{CLEAR}" if msg
end

.header(title, color: CYAN) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/tasks/benchmark_runner.rb', line 87

def self.header(title, color: CYAN)
  width = 78
  line = HL * width
  puts
  puts "#{color}#{TL}#{line}#{TR}#{CLEAR}"
  puts "#{color}#{VL}#{CLEAR}  #{BOLD}#{color}#{title}#{CLEAR}#{' ' * (width - title.length - 4)}#{color}#{VL}#{CLEAR}"
  puts "#{color}#{BL}#{line}#{BR}#{CLEAR}"
end

.hint(msg) ⇒ Object



79
80
81
# File 'lib/tasks/benchmark_runner.rb', line 79

def self.hint(msg)
  puts "  #{CYAN}#{CLEAR} #{DIM}#{msg}#{CLEAR}"
end

.info(msg) ⇒ Object



75
76
77
# File 'lib/tasks/benchmark_runner.rb', line 75

def self.info(msg)
  puts "  #{DIM}#{msg}#{CLEAR}"
end

.okObject



66
67
68
# File 'lib/tasks/benchmark_runner.rb', line 66

def self.ok
  puts " #{GREEN}#{CLEAR}"
end

.sep(char: HL, width: 78) ⇒ Object



83
84
85
# File 'lib/tasks/benchmark_runner.rb', line 83

def self.sep(char: HL, width: 78)
  puts "#{DIM}#{char * width}#{CLEAR}"
end

.speedup_badge(factor, label) ⇒ Object



146
147
148
149
# File 'lib/tasks/benchmark_runner.rb', line 146

def self.speedup_badge(factor, label)
  puts "  #{GREEN}#{label}#{CLEAR}"
  puts "  #{GREEN}   #{factor.round(2)}x faster#{CLEAR}"
end

.spinObject



55
56
57
58
59
# File 'lib/tasks/benchmark_runner.rb', line 55

def self.spin
  print "\b#{SPINNER[@spinner_idx % SPINNER.length]} "
  $stdout.flush
  @spinner_idx += 1
end

.summary_card(results) ⇒ Object

Summary card using TableTennis



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
# File 'lib/tasks/benchmark_runner.rb', line 152

def self.summary_card(results)
  puts
  sep(width: 78)
  puts
  puts "  #{BOLD}#{MAGENTA}SUMMARY#{CLEAR}"
  puts

  rows = results.map do |r|
    {
      benchmark: r[:label],
      ips: r[:ips]&.round(1),
    }
  end

  return if rows.empty?

  table = TableTennis.new(rows,
                          title: "Performance Results",
                          theme: :dark,
                          headers: { benchmark: "Benchmark", ips: "IPS" })
  table.render

  puts
  puts "  #{DIM}#{results.length} benchmarks completed#{CLEAR}"
  puts
end

.table(results) ⇒ Object

Results table for a category using TableTennis



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tasks/benchmark_runner.rb', line 130

def self.table(results)
  rows = results.map do |r|
    {
      test: r[:name],
      ips: r[:ips],
      deviation: "#{r[:deviation].round(1)}%",
      status: r[:is_best] ? "BEST" : "",
    }
  end

  return if rows.empty?

  table = TableTennis.new(rows, theme: :dark)
  table.render
end