Class: DiffBench::Runner
- Inherits:
-
Object
- Object
- DiffBench::Runner
- Defined in:
- lib/diffbench.rb
Constant Summary collapse
- COLORS =
{:red => 31, :green => 32, :yellow => 33}
Class Method Summary collapse
Instance Method Summary collapse
- #improvement_percentage(before_patch, after_patch) ⇒ Object
-
#initialize(*args) ⇒ Runner
constructor
A new instance of Runner.
- #output_improvement(before, after) ⇒ Object
- #print_results(results) ⇒ Object
- #run ⇒ Object
- #run_current_head ⇒ Object
- #run_revisions ⇒ Object
Constructor Details
#initialize(*args) ⇒ Runner
Returns a new instance of Runner.
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 |
# File 'lib/diffbench.rb', line 14 def initialize(*args) parser = OptionParser.new do |opts| opts. = "Usage: diffbench [options] file\n\nWhen working tree is dirty default is run benchmark againts dirty tree and clean tree.\nWhen working tree is clean default is run benchmark against current head and previous commit.\n" opts.on("-r", '--revision [REVISIONS]', 'Specify revisions to run benchmark (comma separated). Example: master,f9a845,v3.1.4') do |value| if tree_dirty? raise Error, "Working tree is dirty." end @revisions = value.split(",") end opts.on("-b", '--before [COMMAND]', 'Specify command to run before each benchmark run. e.g. bundle install') do |value| @before_command = value end opts.on_tail('-h', '--help', 'Show this help') do output opts exit end end parser.parse!(args) @file = args.first or raise Error, 'File not specified' end |
Class Method Details
.color(text, color_string) ⇒ Object
136 137 138 139 |
# File 'lib/diffbench.rb', line 136 def self.color(text, color_string) code = COLORS[color_string] self.color_enabled? ? "\e[#{code}m#{text}\e[0m" : text end |
.color_enabled? ⇒ Boolean
141 142 143 |
# File 'lib/diffbench.rb', line 141 def self.color_enabled? true end |
Instance Method Details
#improvement_percentage(before_patch, after_patch) ⇒ Object
132 133 134 |
# File 'lib/diffbench.rb', line 132 def improvement_percentage(before_patch, after_patch) (((before_patch.real - after_patch.real).to_f / before_patch.real) * 100).round end |
#output_improvement(before, after) ⇒ Object
126 127 128 129 130 |
# File 'lib/diffbench.rb', line 126 def output_improvement(before, after) improvement = improvement_percentage(before, after) color_string = result_color(improvement) output self.class.color("Improvement: #{improvement}%", color_string).strip end |
#print_results(results) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/diffbench.rb', line 68 def print_results(results) output "" #TODO set caption the right way caption = "Before patch: ".gsub(/./, " ") + Benchmark::Tms::CAPTION output caption tests = results.values.first.keys tests.each do |test| output(("-" * (caption.size - test.size)) + test) results.each do |revision, benchmark| output "#{revision}: #{benchmark[test].format}" end if results.size == 2 output_improvement(*results.values.map {|b| b[test]}) end output "" end end |
#run ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/diffbench.rb', line 42 def run if @revisions run_revisions else run_current_head end end |
#run_current_head ⇒ Object
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 |
# File 'lib/diffbench.rb', line 86 def run_current_head output "Running benchmark with current working tree" first_run = run_file if tree_dirty? output "Stashing changes" git_run "stash" output "Running benchmark with clean working tree" begin second_run = run_file ensure output "Applying stashed changes back" git_run "stash pop" end elsif branch = current_head output "Checkout HEAD^" git_run "checkout 'HEAD^'" output "Running benchmark with HEAD^" begin second_run = run_file ensure output "Checkout to previous HEAD again" git_run "checkout #{branch}" end else raise Error, "No current branch." end output "" caption = "Before patch: ".gsub(/./, " ") + Benchmark::Tms::CAPTION output caption first_run.keys.each do |test| output(("-" * (caption.size - test.size)) + test) before_patch = second_run[test] after_patch = first_run[test] output "After patch: #{after_patch.format}" output "Before patch: #{before_patch.format}" output_improvement(before_patch, after_patch) output "" end end |
#run_revisions ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/diffbench.rb', line 50 def run_revisions branch = current_head results = begin @revisions.inject({}) do |result, revision| output "Checkout to #{revision}" output "Run benchmark with #{revision}" git_run("checkout '#{revision}'") result[revision] = run_file result end ensure output "Checkout to #{branch}" git_run("checkout '#{branch}'") end print_results(results) end |