File: fiber_test.rb

package info (click to toggle)
ruby-prof 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: ruby: 9,805; ansic: 2,968; makefile: 7
file content (196 lines) | stat: -rw-r--r-- 6,505 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
#!/usr/bin/env ruby
# encoding: UTF-8

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

# --  Tests ----
class FiberTest < TestCase
  def enumerator_with_fibers
    enum = Enumerator.new do |yielder|
      [1,2].each do |x|
        yielder.yield x
      end
    end

    enum.next
    enum.next
  end

  def fiber_yield_resume
    fiber = Fiber.new do
              Fiber.yield 1
              Fiber.yield 2
            end

    fiber.resume
    fiber.resume
  end

  def test_fibers
    skip "Skip on s390x" if(Gem::Platform.local.cpu == "s390x")
    result = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) { enumerator_with_fibers }

    assert_equal(2, result.threads.size)

    thread1 = result.threads[0]
    methods = thread1.methods.sort.reverse
    assert_equal(5, methods.count)

    method = methods[0]
    assert_equal('FiberTest#test_fibers', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[1]
    assert_equal('FiberTest#enumerator_with_fibers', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[2]
    assert_equal('Enumerator#next', method.full_name)
    assert_equal(2, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

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

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

    thread2 = result.threads[1]
    methods = thread2.methods.sort.reverse
    assert_equal(4, methods.count)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[0]
    assert_equal('Enumerator#each', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[1]
    assert_equal('Enumerator::Generator#each', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[2]
    assert_equal('Array#each', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[3]
    assert_equal('Enumerator::Yielder#yield', method.full_name)
    assert_equal(2, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)
  end

  def test_fiber_resume
    result  = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) { fiber_yield_resume }

    assert_equal(2, result.threads.size)

    thread1 = result.threads[0]
    methods = thread1.methods.sort.reverse
    assert_equal(5, methods.count)

    method = methods[0]
    assert_equal('FiberTest#test_fiber_resume', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[1]
    assert_equal('FiberTest#fiber_yield_resume', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[2]
    assert_equal('Fiber#resume', method.full_name)
    assert_equal(2, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

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

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

    thread1 = result.threads[1]
    methods = thread1.methods.sort.reverse
    assert_equal(2, methods.count)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

    method = methods[0]
    assert_equal('FiberTest#fiber_yield_resume', method.full_name)
    assert_equal(1, method.called)
    assert_in_delta(0, method.total_time)
    assert_in_delta(0, method.self_time)
    assert_in_delta(0, method.wait_time)
    assert_in_delta(0, method.children_time)

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