test/test_stackprof.rb in stackprof-0.1.0 vs test/test_stackprof.rb in stackprof-0.2.0

- old
+ new

@@ -2,56 +2,123 @@ require 'stackprof' require 'test/unit' class StackProfTest < Test::Unit::TestCase def test_info - profile = StackProf.run(:wall, 1000){} - assert_equal 1.0, profile[:version] - assert_equal "wall(1000)", profile[:mode] + profile = StackProf.run{} + assert_equal 1.1, profile[:version] + assert_equal :wall, profile[:mode] + assert_equal 1000, profile[:interval] assert_equal 0, profile[:samples] end + def test_running + assert_equal false, StackProf.running? + StackProf.run{ assert_equal true, StackProf.running? } + end + + def test_start_stop_results + assert_equal nil, StackProf.results + assert_equal true, StackProf.start + assert_equal false, StackProf.start + assert_equal true, StackProf.running? + assert_equal nil, StackProf.results + assert_equal true, StackProf.stop + assert_equal false, StackProf.stop + assert_equal false, StackProf.running? + assert_kind_of Hash, StackProf.results + assert_equal nil, StackProf.results + end + def test_object_allocation - profile = StackProf.run(:object, 1) do + profile = StackProf.run(mode: :object) do Object.new Object.new end - assert_equal "object(1)", profile[:mode] + assert_equal :object, profile[:mode] + assert_equal 1, profile[:interval] assert_equal 2, profile[:samples] frame = profile[:frames].values.first assert_equal "block in StackProfTest#test_object_allocation", frame[:name] assert_equal 2, frame[:samples] - assert_equal 14, frame[:line] - assert_equal 1, frame[:lines][15] - assert_equal 1, frame[:lines][16] + line = __LINE__ + assert_equal line-11, frame[:line] + assert_equal [1, 1], frame[:lines][line-10] + assert_equal [1, 1], frame[:lines][line-9] + + frame = profile[:frames].values[1] + assert_equal [2, 0], frame[:lines][line-11] end def test_cputime - profile = StackProf.run(:cpu, 1000) do + profile = StackProf.run(mode: :cpu, interval: 500) do math end + assert_operator profile[:samples], :>, 1 frame = profile[:frames].values.first assert_equal "block in StackProfTest#math", frame[:name] + File.open('/tmp/cputime.dump','w'){|f| f.write Marshal.dump(profile) } end def test_walltime - profile = StackProf.run(:wall, 1000) do + profile = StackProf.run(mode: :wall) do idle end frame = profile[:frames].values.first assert_equal "StackProfTest#idle", frame[:name] assert_in_delta 200, frame[:samples], 5 end + def test_custom + profile = StackProf.run(mode: :custom) do + 10.times do + StackProf.sample + end + end + + assert_equal :custom, profile[:mode] + assert_equal 10, profile[:samples] + + frame = profile[:frames].values.first + assert_equal "block (2 levels) in StackProfTest#test_custom", frame[:name] + assert_equal __LINE__-10, frame[:line] + assert_equal [10, 10], frame[:lines][__LINE__-10] + end + + def test_fork + StackProf.run do + pid = fork do + exit! StackProf.running?? 1 : 0 + end + Process.wait(pid) + assert_equal 0, $?.exitstatus + assert_equal true, StackProf.running? + end + end + + def test_gc + profile = StackProf.run(mode: :cpu, interval: 100) do + GC.start + end + + assert_empty profile[:frames] + assert_operator profile[:gc_samples], :>, 0 + assert_equal 0, profile[:missed_samples] + end + def math 250_000.times do 2 ** 10 end end def idle - sleep 0.2 + r, w = IO.pipe + IO.select([r], nil, nil, 0.2) + ensure + r.close + w.close end end