class XSpec::Notifier::TimingsAtEnd

Renders a histogram of test durations after the entire run is complete.

Constants

DEFAULT_SPLITS

Attributes

out[R]
splits[R]
timings[R]
width[R]

Public Class Methods

new(out: $stdout, splits: DEFAULT_SPLITS, width: 20) click to toggle source
# File lib/xspec/notifiers.rb, line 93
def initialize(out:    $stdout,
               splits: DEFAULT_SPLITS,
               width:  20)

  @timings = {}
  @splits  = splits
  @width   = width
  @out     = out
end

Public Instance Methods

evaluate_finish(result) click to toggle source
# File lib/xspec/notifiers.rb, line 103
def evaluate_finish(result)
  timings[result] = result.duration
end
run_finish() click to toggle source
# File lib/xspec/notifiers.rb, line 107
def run_finish
  buckets = bucket_from_splits(timings, splits)
  max     = buckets.values.max

  out.puts "           Timings:"
  buckets.each do |(split, count)|
    label = split.infinite? ? "∞" : split

    out.puts "    %6s %-#{width}s %i" % [
      label,
      '#' * (count / max.to_f * width.to_f).ceil,
      count
    ]
  end
  out.puts

  true
end

Private Instance Methods

bucket_from_splits(timings, splits) click to toggle source
# File lib/xspec/notifiers.rb, line 130
def bucket_from_splits(timings, splits)
  initial_buckets = splits.each_with_object({}) do |b, a|
    a[b] = 0
  end

  buckets = timings.each_with_object(initial_buckets) do |(_, d), a|
    split = splits.detect {|x| d < x }
    a[split] += 1
  end

  remove_trailing_zero_counts(buckets)
end
remove_trailing_zero_counts(buckets) click to toggle source
# File lib/xspec/notifiers.rb, line 143
def remove_trailing_zero_counts(buckets)
  Hash[
    buckets
      .to_a
      .reverse
      .drop_while {|_, x| x == 0 }
      .reverse
  ]
end