Sha256: 11755f5bde6654a3639a2df2eb5a5c9799a21d9c12436a4bcd5e0f15d3385fdb

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# Copyright 2013 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0, found in the LICENSE file.

require_relative File.join("spec_helper")

describe AllocationStats do
  it "should trace everything if TRACE_PROCESS_ALLOCATIONS" do
    IO.popen({"TRACE_PROCESS_ALLOCATIONS" => "1"}, "ruby -r ./lib/allocation_stats -e 'puts 0'") do |io|
      out = io.read
      out.should match("Object Allocation Report")
    end
  end

  it "should only track new objects" do
    existing_array = [1,2,3,4,5]

    stats = AllocationStats.trace do
      new_array = [1,2,3,4,5]
    end

    stats.new_allocations.class.should be Array
    stats.new_allocations.size.should == 1
  end

  it "should only track new objects, non-block mode" do
    existing_array = [1,2,3,4,5]

    stats = AllocationStats.trace
    new_array = [1,2,3,4,5]
    stats.stop

    stats.new_allocations.class.should be Array
    stats.new_allocations.size.should == 1
  end

  it "should only track new objects; String keys in Hashes count twice :(" do
    existing_array = [1,2,3,4,5]

    stats = AllocationStats.trace do
      new_hash = {"foo" => "bar", "baz" => "quux"}
    end

    stats.new_allocations.size.should == 7
  end

  it "should only track new objects, using instance method" do
    existing_array = [1,2,3,4,5]

    stats = AllocationStats.new

    stats.trace do
      new_object = Object.new
      new_array  = [4]
      new_string = "yarn"
    end

    stats.new_allocations.class.should be Array
    stats.new_allocations.size.should == 3
  end

  it "should only track new objects" do
    existing_array = [1,2,3,4,5]

    my_instance = MyClass.new

    stats = AllocationStats.new(burn: 3).trace do
      # this method instantiates 2**(n-1) Strings on the n'th call
      my_instance.memoizing_method
    end

    stats.new_allocations.class.should be Array
    stats.new_allocations.size.should == 8
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
allocation_stats-0.1.2 spec/allocation_stats_spec.rb
allocation_stats-0.1.1 spec/allocation_stats_spec.rb