Module: Benchmark

Defined in:
mrbgems/mruby-benchmark/mrblib/benchmark.rb

Defined Under Namespace

Classes: Report, Tms

Class Method Summary collapse

Class Method Details

.bm(label_width = 0) {|report| ... } ⇒ Object

Formatted benchmark with labeled reports

Yields:

  • (report)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'mrbgems/mruby-benchmark/mrblib/benchmark.rb', line 116

def self.bm(label_width = 0)
  report = Report.new(label_width)

  # Print header
  if $stdout
    if label_width > 0
      $stdout.print " " * label_width
    end
    $stdout.puts "      user     system      total        real"
  end

  yield report

  report
end

.measure(memory: false) ⇒ Object

Measure execution time of a block



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'mrbgems/mruby-benchmark/mrblib/benchmark.rb', line 72

def self.measure(memory: false)
  start_time = Time.now
  start_objects = nil
  start_count = nil

  if memory
    if Object.const_defined?(:ObjectSpace)
      start_count = ObjectSpace.count_objects
      start_objects = start_count.values.inject(0) {|sum, n| sum + n }
    end
  end

  yield

  end_time = Time.now
  real = end_time - start_time

  objects_allocated = nil
  memory_allocated = nil

  if memory && start_count
    end_count = ObjectSpace.count_objects
    end_objects = end_count.values.inject(0) {|sum, n| sum + n }
    objects_allocated = end_objects - start_objects

    # Estimate memory based on object count
    # Average object overhead in mruby (approximate)
    memory_allocated = objects_allocated * 40
  end

  # mruby typically doesn't have per-process CPU time
  # Set user/system times to 0
  Tms.new(0.0, 0.0, 0.0, 0.0, real, nil, objects_allocated, memory_allocated)
end

.realtimeObject

Return only real time as a float



108
109
110
111
112
113
# File 'mrbgems/mruby-benchmark/mrblib/benchmark.rb', line 108

def self.realtime
  start_time = Time.now
  yield
  end_time = Time.now
  end_time - start_time
end