File: thread_test.rb

package info (click to toggle)
ruby-prof 0.17.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,656 kB
  • sloc: ruby: 5,043; ansic: 2,175; makefile: 6
file content (187 lines) | stat: -rwxr-xr-x 6,337 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)
require 'timeout'
require 'benchmark'

# --  Tests ----
class ThreadTest < TestCase
  def setup
    # Need to use wall time for this test due to the sleep calls
    RubyProf::measure_mode = RubyProf::WALL_TIME
  end

  def test_thread_count
    RubyProf.start

    thread = Thread.new do
      sleep(1)
    end

    thread.join
    result = RubyProf.stop
    assert_equal(2, result.threads.length)
  end

  def test_thread_identity
    RubyProf.start
    sleep_thread = Thread.new do
      sleep(1)
    end
    sleep_thread.join
    result = RubyProf.stop

    thread_ids = result.threads.map {|thread| thread.id}.sort
    threads = [Thread.current, sleep_thread]
    assert_equal(2, result.threads.length)

    assert(thread_ids.include?(threads[0].object_id))
    assert(thread_ids.include?(threads[1].object_id))

    assert_instance_of(Thread, ObjectSpace._id2ref(thread_ids[0]))
    assert(threads.include?(ObjectSpace._id2ref(thread_ids[0])))

    assert_instance_of(Thread, ObjectSpace._id2ref(thread_ids[1]))
    assert(threads.include?(ObjectSpace._id2ref(thread_ids[1])))
  end

  def test_thread_timings
    RubyProf.start
    thread = Thread.new do
      sleep 0
      # force it to hit thread.join, below, first
      # thus forcing sleep(1), below, to be counted as (wall) self_time
      # since we currently count time "in some other thread" as self.wait_time
      # for whatever reason
      sleep(1)
    end
    thread.join
    result = RubyProf.stop

    # Check background thread
    assert_equal(2, result.threads.length)

    rp_thread = result.threads.detect {|t| t.id == thread.object_id}
    methods = rp_thread.methods.sort.reverse
    # fails on travis. why?
    # expected_methods = ["ThreadTest#test_thread_timings", "Kernel#sleep"]
    # assert_equal(expected_methods, methods.map(&:full_name))

    method = methods[0]
    assert_equal('ThreadTest#test_thread_timings', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(1, method.total_time, 0.05)
    assert_in_delta(0, method.self_time, 0.05)
    assert_in_delta(0, method.wait_time, 0.05)
    assert_in_delta(1, method.children_time, 0.05)
    assert_equal(1, method.call_infos.length)
    call_info = method.call_infos[0]
    assert_equal('ThreadTest#test_thread_timings', call_info.call_sequence)
    assert_equal(1, call_info.children.length)

    method = methods[1]
    assert_equal('Kernel#sleep', method.full_name)
    assert_equal(2, method.called)
    assert_in_delta(1, method.total_time, 0.05)
    assert_in_delta(1.0, method.self_time, 0.05)
    assert_in_delta(0, method.wait_time, 0.05)
    assert_in_delta(0, method.children_time, 0.05)

    assert_equal(1, method.call_infos.length)
    call_info = method.call_infos[0]
    assert_equal('ThreadTest#test_thread_timings->Kernel#sleep', call_info.call_sequence)
    assert_equal(0, call_info.children.length)

    # Check foreground thread
    rp_thread = result.threads.detect {|athread| athread.id == Thread.current.object_id}
    methods = rp_thread.methods.sort.reverse
    assert_equal(4, methods.length)
    methods = methods.sort.reverse

    method = methods[0]
    assert_equal('ThreadTest#test_thread_timings', method.full_name)
    # the sub calls to Object#new, when popped,
    # cause the parent frame to be created for method #test_thread_timings, which means a +1 when it's popped in the end
    # xxxx a test that shows it the other way, too (never creates parent frame--if that's even possible)
    assert_equal(1, method.called)
    assert_in_delta(1, method.total_time, 0.05)
    assert_in_delta(0, method.self_time, 0.05)
    assert_in_delta(0, method.wait_time, 0.05)
    assert_in_delta(1, method.children_time, 0.05)

    assert_equal(1, method.call_infos.length)
    call_info = method.call_infos[0]
    assert_equal('ThreadTest#test_thread_timings', call_info.call_sequence)
    assert_equal(2, call_info.children.length)

    method = methods[1]
    assert_equal('Thread#join', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(1, method.total_time, 0.05)
    assert_in_delta(0, method.self_time, 0.05)
    assert_in_delta(1.0, method.wait_time, 0.05)
    assert_in_delta(0, method.children_time, 0.05)

    assert_equal(1, method.call_infos.length)
    call_info = method.call_infos[0]
    assert_equal('ThreadTest#test_thread_timings->Thread#join', call_info.call_sequence)
    assert_equal(0, call_info.children.length)

    method = methods[2]
    assert_equal('<Class::Thread>#new', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time, 0.05)
    assert_in_delta(0, method.self_time, 0.05)
    assert_in_delta(0, method.wait_time, 0.05)
    assert_in_delta(0, method.children_time, 0.05)

    assert_equal(1, method.call_infos.length)
    call_info = method.call_infos[0]
    assert_equal('ThreadTest#test_thread_timings-><Class::Thread>#new', call_info.call_sequence)
    assert_equal(1, call_info.children.length)

    method = methods[3]
    assert_equal('Thread#initialize', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time, 0.05)
    assert_in_delta(0, method.self_time, 0.05)
    assert_in_delta(0, method.wait_time, 0.05)
    assert_in_delta(0, method.children_time, 0.05)

    assert_equal(1, method.call_infos.length)
    call_info = method.call_infos[0]
    assert_equal('ThreadTest#test_thread_timings-><Class::Thread>#new->Thread#initialize', call_info.call_sequence)
    assert_equal(0, call_info.children.length)
  end

  # useless test: what does it test?
  def test_thread_back_and_forth
    result = nil
    seconds = Benchmark.realtime do
      result = RubyProf.profile do
        a = Thread.new { 100_000.times { sleep 0 }}
        b = Thread.new { 100_000.times { sleep 0 }}
        a.join
        b.join
      end
    end
    methods = result.threads.map {|thread| thread.methods}
    timings = methods.flatten.sort
    assert(timings[-1].total_time < seconds)
  end

  # useless test: what does it test?
  def test_thread
    RubyProf.profile do
      begin
        Timeout::timeout(2) do
          while true
            next
          end
        end
      rescue Timeout::Error
      end
    end
  end
end