Skip to content
This repository
tree: 6c54cd85bd
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 103 lines (78 sloc) 2.232 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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
require File.dirname(__FILE__) + "/../ext/memprof"

require 'rubygems'
require 'bacon'
Bacon.summary_on_exit

require 'tempfile'

describe Memprof do
  @tempfile = Tempfile.new('memprof_spec')

  def filename
    @tempfile.path
  end

  def filedata
    File.read(filename)
  end

  before do
    Memprof.stop
  end

  should 'print stats to a file' do
    Memprof.start
    "abc"
    Memprof.stats(filename)

    filedata.strip.should == "1 #{__FILE__}:#{__LINE__-3}:String"
  end

  should 'allow calling ::stats multiple times' do
    Memprof.start
    []
    Memprof.stats(filename)
    []
    Memprof.stats(filename)

    filedata.strip.split("\n").size.should == 2
  end

  should 'clear stats after ::stats!' do
    Memprof.start
    []
    Memprof.stats!(filename)
    Memprof.stats(filename)

    filedata.strip.should.be.empty
  end

  should 'collect stats via ::track' do
    Memprof.track(filename) do
      "abc"
    end

    filedata.should =~ /1 #{__FILE__}:#{__LINE__-3}:String/
  end

  should 'dump objects as json' do
    Memprof.start
    1.23+1
    Memprof.dump(filename)

    filedata.should =~ /"file": "#{__FILE__}"/
    filedata.should =~ /"line": #{__LINE__-4}/
    filedata.should =~ /"type": "float"/
    filedata.should =~ /"data": 2\.23/
  end

  should 'raise error when calling ::stats or ::dump without ::start' do
    lambda{ Memprof.stats }.should.raise(RuntimeError).message.should =~ /Memprof.start/
    lambda{ Memprof.dump }.should.raise(RuntimeError).message.should =~ /Memprof.start/
  end

  should 'dump out the entire heap' do
    Memprof.stop
    Memprof.dump_all(filename)

    obj = File.open(filename, 'r').readlines.find do |line|
      line =~ /"dump out the entire heap"/
    end

    obj.should =~ /"length":24/
    obj.should =~ /"type":"string"/
    obj.should =~ /"_id":"0x(\w+?)"/
  end

  should 'dump out the entire heap with tracking info' do
    Memprof.start
    @str = "some random" + " string"
    Memprof.dump_all(filename)

    obj = File.open(filename, 'r').readlines.find do |line|
      line =~ /"some random string"/
    end

    obj.should =~ /"type":"string"/
    obj.should =~ /"file":".+?memprof_spec.rb"/
    obj.should =~ /"line":#{__LINE__-9}/
  end
end

Something went wrong with that request. Please try again.