File: merge_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 (146 lines) | stat: -rw-r--r-- 5,332 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
#!/usr/bin/env ruby
# encoding: UTF-8

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

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.1.0')

require_relative './scheduler'

  # --  Tests ----
class MergeTest < TestCase
  def worker1
    sleep(0.5)
  end

  def worker2
    sleep(0.5)
    sleep(0.5)
  end

  def worker3
    sleep(0.5)
  end

  def concurrency_single_worker
    scheduler = Scheduler.new
    Fiber.set_scheduler(scheduler)

    3.times do
      Fiber.schedule do
        worker1
      end
    end
    Fiber.scheduler.close
  end

  def concurrency_multiple_workers
    scheduler = Scheduler.new
    Fiber.set_scheduler(scheduler)

    3.times do |i|
      Fiber.schedule do
        method = "worker#{i + 1}".to_sym
        send(method)
      end
    end
    Fiber.scheduler.close
  end

  def test_single_worker_unmerged
    result  = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) { concurrency_single_worker }
    assert_equal(4, result.threads.size)

    thread = result.threads[0]
    assert_in_delta(0.5, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[1]
    assert_in_delta(0.5, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[2]
    assert_in_delta(0.5, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[3]
    assert_in_delta(0.5, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)
  end

  def test_single_worker_merged
    result  = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) { concurrency_single_worker }
    result.merge!

    assert_equal(2, result.threads.size)

    thread = result.threads[0]
    assert_in_delta(0.5, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[1]
    assert_in_delta(1.5, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(1.5, thread.call_tree.target.children_time, 0.1)
  end

  def test_multiple_workers_unmerged
    result  = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) { concurrency_multiple_workers }
    assert_equal(4, result.threads.count)

    thread = result.threads[0]
    assert_in_delta(1.0, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(1.0, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[1]
    assert_in_delta(1.0, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[2]
    assert_in_delta(1.0, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(1.0, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[3]
    assert_in_delta(1.0, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(0.5, thread.call_tree.target.children_time, 0.1)
  end

  def test_multiple_workers_merged
    result  = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) { concurrency_multiple_workers }
    result.merge!

    assert_equal(2, result.threads.count)

    thread = result.threads[0]
    assert_in_delta(1.0, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(1.0, thread.call_tree.target.children_time, 0.1)

    thread = result.threads[1]
    assert_in_delta(3.0, thread.call_tree.target.total_time, 0.1)
    assert_in_delta(0.0, thread.call_tree.target.self_time, 0.1)
    assert_in_delta(1.0, thread.call_tree.target.wait_time, 0.1)
    assert_in_delta(2.0, thread.call_tree.target.children_time, 0.1)
  end
end
end