Sha256: c75c1d8dec5de5364af8facaa38034dc4976f59d93afdab6e66ec93c5b23021e

Contents?: true

Size: 1.47 KB

Versions: 180

Compression:

Stored size: 1.47 KB

Contents

require 'tempfile'

# A very simple memory profiles that checks the full size of a variable
# by serializing into a binary file.
#
# Yes, I know this is very rough, but there are cases where ObjectSpace.memsize_of
# doesn't cooperate, and this is one of the possible workarounds.
#
# For certain cases, it works (TM).
class ObjectBinsize

  def measure(var, label: nil)
    dump(var, label: label)
  end

  def report(var, label: nil, padding: 10)
    file = measure(var, label: label)

    size = format_integer(file.size)
    name = label || File.basename(file.path)
    printf("%#{padding}s   %s\n", size, name)
  end

  private

  def dump(var, **args)
    file = Tempfile.new(args[:label].to_s)
    file.write(Marshal.dump(var))
    file
  ensure
    file.close
  end

  def format_integer(int)
    int.to_s.reverse.gsub(/...(?=.)/, '\&,').reverse
  end

end

if __FILE__ == $0
  prof = ObjectBinsize.new

  prof.report(nil, label: "nil")
  prof.report(false, label: "false")
  prof.report(true, label: "true")
  prof.report(0, label: "integer")
  prof.report("", label: "empty string")
  prof.report({}, label: "empty hash")
  prof.report({}, label: "empty array")

  prof.report({ foo: "1" }, label: "hash 1 item (symbol)")
  prof.report({ foo: "1", bar: 2 }, label: "hash 2 items (symbol)")
  prof.report({ "foo" => "1" }, label: "hash 1 item (string)")
  prof.report({ "foo" => "1", "bar" => 2 }, label: "hash 2 items (string)")

  prof.report("big string" * 200, label: "big string * 200")
end

Version data entries

180 entries across 162 versions & 20 rubygems

Version Path
whitelabelmachinename-ruby-sdk-3.1.2 vendor/bundle/ruby/2.4.0/gems/public_suffix-4.0.7/test/profilers/object_binsize.rb
wallee-ruby-sdk-3.1.2 vendor/bundle/ruby/2.4.0/gems/public_suffix-4.0.7/test/profilers/object_binsize.rb
wallee-ruby-sdk-3.1.1 vendor/bundle/ruby/2.4.0/gems/public_suffix-4.0.7/test/profilers/object_binsize.rb
public_suffix-5.0.0 test/profilers/object_binsize.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/public_suffix-4.0.7/test/profilers/object_binsize.rb
cloudsmith-api-1.61.3 vendor/bundle/ruby/2.6.0/gems/public_suffix-4.0.7/test/profilers/object_binsize.rb
whitelabelmachinename-ruby-sdk-3.0.6 vendor/bundle/ruby/2.7.0/gems/public_suffix-4.0.7/test/profilers/object_binsize.rb
public_suffix-4.0.7 test/profilers/object_binsize.rb
cloudsmith-api-1.42.3 vendor/bundle/ruby/2.6.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
cloudsmith-api-1.33.7 vendor/bundle/ruby/2.6.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
cloudsmith-api-1.30.0 vendor/bundle/ruby/2.6.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/public_suffix-4.0.6/test/profilers/object_binsize.rb