Class: DiffBench::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/diffbench.rb

Constant Summary collapse

COLORS =
{red: 31, green: 32}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, *args) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
18
19
# File 'lib/diffbench.rb', line 14

def initialize(file, *args)
  @file = file
  unless @file
    raise Error, "File not specified"
  end
end

Class Method Details

.color(text, color_string) ⇒ Object



69
70
71
72
# File 'lib/diffbench.rb', line 69

def self.color(text, color_string)
  code = COLORS[color_string]
  self.color_enabled? ? "\e[#{code}m#{text}\e[0m" : text
end

.color_enabled?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/diffbench.rb', line 74

def self.color_enabled?
  true
end

Instance Method Details

#improvement_percentage(before_patch, after_patch) ⇒ Object



65
66
67
# File 'lib/diffbench.rb', line 65

def improvement_percentage(before_patch, after_patch)
  (((before_patch.real - after_patch.real).to_f / before_patch.real) * 100).round
end

#runObject



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
# File 'lib/diffbench.rb', line 21

def run
  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]
    improvement = improvement_percentage(before_patch, after_patch)
    color_string = result_color(improvement)
    output "After patch:  #{after_patch.format}"
    output "Before patch: #{before_patch.format}"
    if color_string
      output self.class.color("Improvement: #{improvement}%", color_string).strip
    end
    output ""
  end
end