Class: Judges::Statistics

Inherits:
Object show all
Defined in:
lib/judges/statistics.rb

Overview

Statistics collector for judge executions.

This class collects and aggregates statistics about judge executions across multiple cycles, providing insights into performance and results.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2026 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initializeStatistics

Initialize empty statistics.



18
19
20
# File 'lib/judges/statistics.rb', line 18

def initialize
  @data = {}
end

Instance Method Details

#empty?Boolean

Check if statistics are empty.

Returns:

  • (Boolean)

    True if no statistics have been collected



24
25
26
# File 'lib/judges/statistics.rb', line 24

def empty?
  @data.empty?
end

#record(name, time, result, churn = nil) ⇒ Object

Record statistics for a judge execution.

Parameters:

  • name (String)

    The judge name

  • time (Float)

    The execution time for this run

  • result (String)

    The result for this run

  • churn (Churn) (defaults to: nil)

    The churn for this run (can be nil)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/judges/statistics.rb', line 33

def record(name, time, result, churn = nil)
  unless @data[name]
    @data[name] = { total_time: 0.0, cycles: 0, results: [], total_churn: nil }
  end
  stats = @data[name]
  stats[:total_time] += time
  stats[:cycles] += 1
  stats[:results] << result
  return unless churn
  if stats[:total_churn]
    stats[:total_churn] += churn
  else
    stats[:total_churn] = churn
  end
end

#report(loog) ⇒ Object

Generate a formatted statistics report.

Parameters:

  • loog (Loog)

    Logging facility for output



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/judges/statistics.rb', line 51

def report(loog)
  return if empty?
  fmt = "%-30s\t%9s\t%7s\t%15s\t%-15s"
  loog.info(
    [
      'Judge execution summary:',
      format(fmt, 'Judge', 'Seconds', 'Cycles', 'Changes', 'Results'),
      format(fmt, '---', '---', '---', '---', '---'),
      @data.sort_by { |_, stats| stats[:total_time] }.reverse.map do |name, stats|
        format(
          fmt, name, format('%.3f', stats[:total_time]), stats[:cycles],
          stats[:total_churn] ? stats[:total_churn].to_s : 'N/A', summarize(stats[:results])
        )
      end.join("\n  ")
    ].join("\n  ")
  )
end