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
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/public_suffix-5.0.3/test/profilers/object_binsize.rb
public_suffix-5.0.3 test/profilers/object_binsize.rb
cloudsmith-api-2.0.7 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.6 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.5 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.4 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.3 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.2 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
postfinancecheckout-ruby-sdk-3.3.0 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
wallee-ruby-sdk-3.3.0 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.1 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
cloudsmith-api-2.0.0 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/profilers/object_binsize.rb
public_suffix-5.0.1 test/profilers/object_binsize.rb