Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of https://github.com/rdp/ruby-prof

  • Loading branch information...
commit 47fe7850ee72480698cbc97a0344a5ca74f667a8 2 parents d1e431f + 8858a6d
Roger Pack rdp authored
12 ext/ruby_prof/ruby_prof.c
@@ -1078,10 +1078,8 @@ pop_frames(st_data_t key, st_data_t value, st_data_t now_arg)
1078 1078 }
1079 1079
1080 1080 static void
1081   -prof_pop_threads()
  1081 +prof_pop_threads(prof_measure_t now)
1082 1082 {
1083   - /* Get current measurement */
1084   - prof_measure_t now = get_measurement();
1085 1083 st_foreach(threads_tbl, pop_frames, (st_data_t) &now);
1086 1084 }
1087 1085
@@ -1613,6 +1611,10 @@ prof_resume(VALUE self)
1613 1611 static VALUE
1614 1612 prof_stop(VALUE self)
1615 1613 {
  1614 + /* get 'now' before prof emove hook because it calls GC.disable_stats
  1615 + which makes the call within prof_pop_threads of now return 0, which is wrong
  1616 + */
  1617 + prof_measure_t now = get_measurement();
1616 1618 if (threads_tbl == NULL)
1617 1619 {
1618 1620 rb_raise(rb_eRuntimeError, "RubyProf.start was not yet called");
@@ -1626,10 +1628,10 @@ prof_stop(VALUE self)
1626 1628 fclose(trace_file);
1627 1629 trace_file = NULL;
1628 1630 }
1629   -
  1631 +
1630 1632 prof_remove_hook();
1631 1633
1632   - prof_pop_threads();
  1634 + prof_pop_threads(now);
1633 1635
1634 1636 /* Create the result */
1635 1637 result = prof_result_new();
18 test/measurement_test.rb
@@ -4,10 +4,12 @@
4 4
5 5 class MeasurementTest < Test::Unit::TestCase
6 6 def setup
  7 + # also done in C these days...
7 8 GC.enable_stats if GC.respond_to?(:enable_stats)
8 9 end
9 10
10 11 def teardown
  12 + # also done in C for normal runs...
11 13 GC.disable_stats if GC.respond_to?(:disable_stats)
12 14 end
13 15
@@ -69,6 +71,13 @@ def test_allocations
69 71 end
70 72 end
71 73
  74 + def memory_test_helper
  75 + result = RubyProf.profile {Array.new}
  76 + total = result.threads.values.first.inject(0) { |sum, m| sum + m.total_time }
  77 + assert(total < 1_000_000, 'Total should not have subtract overflow error')
  78 + total
  79 + end
  80 +
72 81 if RubyProf::MEMORY
73 82 def test_memory_mode
74 83 RubyProf::measure_mode = RubyProf::MEMORY
@@ -81,11 +90,8 @@ def test_memory
81 90
82 91 u = RubyProf.measure_memory
83 92 assert(u >= t, [t, u].inspect)
84   -
85 93 RubyProf::measure_mode = RubyProf::MEMORY
86   - result = RubyProf.profile {Array.new}
87   - total = result.threads.values.first.inject(0) { |sum, m| sum + m.total_time }
88   -
  94 + total = memory_test_helper
89 95 assert(total > 0, 'Should measure more than zero kilobytes of memory usage')
90 96 assert_not_equal(0, total % 1, 'Should not truncate fractional kilobyte measurements')
91 97 end
@@ -105,6 +111,8 @@ def test_gc_runs
105 111
106 112 u = RubyProf.measure_gc_runs
107 113 assert u > t, [t, u].inspect
  114 + RubyProf::measure_mode = RubyProf::GC_RUNS
  115 + memory_test_helper
108 116 end
109 117 end
110 118
@@ -117,6 +125,8 @@ def test_gc_time
117 125
118 126 u = RubyProf.measure_gc_time
119 127 assert u > t, [t, u].inspect
  128 + RubyProf::measure_mode = RubyProf::GC_TIME
  129 + memory_test_helper
120 130 end
121 131 end
122 132 end
2  test/stack_printer_test.rb
@@ -46,7 +46,7 @@ def test_stack_can_be_printed
46 46 assert file_contents =~ /Thread: (\d+) \(100\.00% ~ ([.0-9]+)\)/
47 47 actual_time = $2.to_f
48 48 difference = (expected_time-actual_time).abs
49   - assert difference < 0.01 # less than 1 ms
  49 + assert_in_delta(expected_time, actual_time, 0.01)
50 50 end
51 51
52 52 def test_method_elimination
2  test/thread_test.rb
@@ -155,7 +155,7 @@ def test_thread_back_and_forth
155 155 a.join
156 156 b.join
157 157 end
158   - assert result.threads.values.flatten.sort[-1].total_time < 10 # 10s
  158 + assert result.threads.values.flatten.sort[-1].total_time < 10 # 10s. Amazingly, this can fail in OS X at times. Amazing.
159 159 end
160 160
161 161 def test_thread

0 comments on commit 47fe785

Please sign in to comment.
Something went wrong with that request. Please try again.