module BenchmarkGC

Constants

VERSION

Public Class Methods

add_objects(objects) click to toggle source
# File lib/benchmark_g_c.rb, line 19
def self.add_objects(objects)
  @@total_objects_allocated += objects
end
increment_bchecks() click to toggle source
# File lib/benchmark_g_c.rb, line 11
def self.increment_bchecks
  @@total_bchecks += 1
end
total_bchecks() click to toggle source
# File lib/benchmark_g_c.rb, line 7
def self.total_bchecks
  @@total_bchecks
end
total_objects_allocated() click to toggle source
# File lib/benchmark_g_c.rb, line 15
def self.total_objects_allocated
  @@total_objects_allocated
end

Public Instance Methods

average_results(results) click to toggle source
# File lib/benchmark_g_c.rb, line 72
def average_results(results)
  summed_results = sum_results(results)
  summed_results.each do |key, value|
    summed_results[key] = value / results.length
  end
end
bcheck(options = {}) { || ... } click to toggle source
# File lib/benchmark_g_c.rb, line 23
def bcheck(options = {})
  default_options = { name: '', times: 1, logger: true }
  options = default_options.merge(options)
  name = options[:name]
  times = options[:times]
  logger = options[:logger]
  if defined? Rails.env
    ActiveRecord::Base.logger = nil unless logger
  end
  results = []
  return_value = nil
  times.times do
    time_before = Time.now
    stat_before = GC.stat
    return_value = yield
    time_after = Time.now
    stat_after = GC.stat
    results.push(
      time_elapsed: time_after - time_before,
      heap_free_slots_added: stat_after[:heap_free_slots] - stat_before[:heap_free_slots],
      objects_allocated: stat_after[:total_allocated_objects] - stat_before[:total_allocated_objects],
      objects_freed: stat_after[:total_freed_objects] - stat_before[:total_freed_objects]
    )
    print_benchmark(name, time_before, time_after, stat_before, stat_after)
  end
  averaged_results = average_results(results)
  BenchmarkGC::increment_bchecks
  BenchmarkGC::add_objects(averaged_results[:objects_allocated])
  print_averaged_results(averaged_results, times)
  return_value
end
insert_underscores(string) click to toggle source
# File lib/benchmark_g_c.rb, line 106
def insert_underscores(string)
  (1..(string.length - 1) / 3).each do |n|
    string.insert(n * 3 + n - 1, '_') unless string[n * 3 + n - 1] == '-'
  end
  string
end
print_averaged_results(results, times_run) click to toggle source
print_benchmark(name, time_before, time_after, stat_before, stat_after) click to toggle source
print_stats(name, before, after) click to toggle source
sum_results(results) click to toggle source
# File lib/benchmark_g_c.rb, line 79
def sum_results(results)
  summed_results = results[0].clone
  results[1..-1].each do |result|
    summed_results.keys.each do |key|
      summed_results[key] += result[key]
    end
  end
  summed_results
end
underscore_number(number) click to toggle source
# File lib/benchmark_g_c.rb, line 102
def underscore_number(number)
  insert_underscores(number.round.to_s.reverse).reverse
end