Module: Coradoc::PerformanceRegression

Defined in:
lib/coradoc/performance_regression.rb

Defined Under Namespace

Classes: BenchmarkResult, ComparisonResult

Constant Summary collapse

THRESHOLDS =
{
  markdown_parse_small: 2.0,
  markdown_parse_medium: 3.0,
  asciidoc_parse: 5.0,
  html_serialize: 5.0,
  md_to_html: 10.0,
  adoc_to_html: 10.0,
  core_model_creation: 1.0
}.freeze

Class Method Summary collapse

Class Method Details

.compare_with_baseline(baseline_path, iterations: 3) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/coradoc/performance_regression.rb', line 75

def compare_with_baseline(baseline_path, iterations: 3)
  return [] unless File.exist?(baseline_path)

  baseline_data = JSON.parse(File.read(baseline_path))
  baseline_map = baseline_data.each_with_object({}) { |d, m| m[d['name']] = d['duration'] }

  results = run_all(iterations:)
  results.map do |r|
    ComparisonResult.new(name: r.name, duration: r.duration, baseline: baseline_map[r.name])
  end
end


64
65
66
67
68
69
70
71
72
73
# File 'lib/coradoc/performance_regression.rb', line 64

def print_results(summary_or_results)
  if summary_or_results.is_a?(Hash)
    summary = summary_or_results
    summary[:results].each { |r| puts r.format_line }
    puts
    puts "#{summary[:total] - summary[:failed_count]}/#{summary[:total]} passed"
  else
    Array(summary_or_results).each { |r| puts r.format_line }
  end
end

.run_all(iterations: 3) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/coradoc/performance_regression.rb', line 48

def run_all(iterations: 3)
  benchmarks = build_benchmarks
  benchmarks.map do |name, threshold, block|
    times = []
    iterations.times { times << Benchmark.realtime { block.call } }
    avg = times.sum / times.size
    BenchmarkResult.new(name:, duration: avg, iterations:, threshold:)
  end
end

.run_all_with_summary(iterations: 3) ⇒ Object



58
59
60
61
62
# File 'lib/coradoc/performance_regression.rb', line 58

def run_all_with_summary(iterations: 3)
  results = run_all(iterations:)
  failed = results.count { |r| !r.passed? }
  { results:, failed_count: failed, total: results.size }
end