Class: TheMechanic2::BenchmarkResult

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

Overview

Model for formatting and exporting benchmark results Provides JSON and Markdown export capabilities

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ BenchmarkResult

Returns a new instance of BenchmarkResult.



9
10
11
12
13
14
15
# File 'lib/the_mechanic_2/benchmark_result.rb', line 9

def initialize(data)
  @code_a_metrics = data[:code_a_metrics]
  @code_b_metrics = data[:code_b_metrics]
  @winner = data[:winner]
  @performance_ratio = data[:performance_ratio]
  @summary = data[:summary]
end

Instance Attribute Details

#code_a_metricsObject (readonly)

Returns the value of attribute code_a_metrics.



7
8
9
# File 'lib/the_mechanic_2/benchmark_result.rb', line 7

def code_a_metrics
  @code_a_metrics
end

#code_b_metricsObject (readonly)

Returns the value of attribute code_b_metrics.



7
8
9
# File 'lib/the_mechanic_2/benchmark_result.rb', line 7

def code_b_metrics
  @code_b_metrics
end

#performance_ratioObject (readonly)

Returns the value of attribute performance_ratio.



7
8
9
# File 'lib/the_mechanic_2/benchmark_result.rb', line 7

def performance_ratio
  @performance_ratio
end

#summaryObject (readonly)

Returns the value of attribute summary.



7
8
9
# File 'lib/the_mechanic_2/benchmark_result.rb', line 7

def summary
  @summary
end

#winnerObject (readonly)

Returns the value of attribute winner.



7
8
9
# File 'lib/the_mechanic_2/benchmark_result.rb', line 7

def winner
  @winner
end

Instance Method Details

#to_hHash

Returns a hash representation of the results

Returns:

  • (Hash)

    results as a hash



62
63
64
65
66
67
68
69
70
# File 'lib/the_mechanic_2/benchmark_result.rb', line 62

def to_h
  {
    code_a_metrics: @code_a_metrics,
    code_b_metrics: @code_b_metrics,
    winner: @winner,
    performance_ratio: @performance_ratio,
    summary: @summary
  }
end

#to_json(*_args) ⇒ String

Exports results as JSON

Returns:

  • (String)

    JSON representation of results



19
20
21
22
23
24
25
26
27
28
# File 'lib/the_mechanic_2/benchmark_result.rb', line 19

def to_json(*_args)
  {
    code_a_metrics: @code_a_metrics,
    code_b_metrics: @code_b_metrics,
    winner: @winner,
    performance_ratio: @performance_ratio,
    summary: @summary,
    timestamp: Time.now.iso8601
  }.to_json
end

#to_markdownString

Exports results as Markdown

Returns:

  • (String)

    Markdown formatted results



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
# File 'lib/the_mechanic_2/benchmark_result.rb', line 32

def to_markdown
  "    # Ruby Benchmark Results\n    \n    **Winner:** \#{winner_text}\n    \n    **Summary:** \#{@summary}\n    \n    ## Performance Metrics\n    \n    | Metric | Code A | Code B |\n    |--------|--------|--------|\n    | IPS (iterations/sec) | \#{format_number(@code_a_metrics[:ips])} | \#{format_number(@code_b_metrics[:ips])} |\n    | Standard Deviation | \#{format_number(@code_a_metrics[:stddev])} | \#{format_number(@code_b_metrics[:stddev])} |\n    | Objects Allocated | \#{format_number(@code_a_metrics[:objects])} | \#{format_number(@code_b_metrics[:objects])} |\n    | Memory (MB) | \#{format_number(@code_a_metrics[:memory_mb])} | \#{format_number(@code_b_metrics[:memory_mb])} |\n    | Execution Time (sec) | \#{format_number(@code_a_metrics[:execution_time])} | \#{format_number(@code_b_metrics[:execution_time])} |\n    \n    ## Analysis\n    \n    \#{analysis_text}\n    \n    ---\n    \n    *Generated at \#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}*\n  MARKDOWN\nend\n"