Project

General

Profile

Feature #10600 ยป queue_benchmark.rb

djellemah (John Anderson), 03/25/2015 07:30 PM

 
1
require 'benchmark'
2
require 'thread'
3

    
4
N = Integer(ARGV.shift || 1000)
5

    
6
puts "RUBY_DESCRIPTION: #{RUBY_DESCRIPTION}"
7
puts "Queue#close: #{Queue.instance_methods.include?(:close) ? 'yes' : 'no'}"
8

    
9
GC.disable
10

    
11
# Adapted from http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/391324
12

    
13
Benchmark.bm(50) do |bm|
14
  bm.report '01 producer 01 consumer' do
15
    q = SizedQueue.new 1000
16
    th = Thread.new do
17
      n = 0
18
      loop do
19
        item = q.pop
20
        raise StopIteration unless item
21
        n += 1
22
      end
23
    end
24

    
25
    N.times do
26
       100.times do |item|
27
         q.push item
28
       end
29
    end
30

    
31
    if q.respond_to? :close
32
      q.close StopIteration
33
    else
34
      q.push nil
35
    end
36

    
37
    th.join
38
  end
39

    
40
  bm.report '01 producer 02 consumer' do
41
    q = SizedQueue.new 1000
42
    cthreads = 2.times.map do |i|
43
      Thread.new do
44
        n = 0
45
        loop do
46
          item = q.pop
47
          raise StopIteration unless item
48
          n += 1
49
        end
50
      end
51
    end
52

    
53
    N.times do
54
       100.times do |item|
55
         q.push item
56
       end
57
    end
58

    
59
    if q.respond_to? :close
60
      q.close StopIteration
61
    else
62
      cthreads.each{ q.push nil }
63
    end
64

    
65
    cthreads.each &:join
66
  end
67

    
68
  bm.report '01 producer 99 consumer' do
69
    q = SizedQueue.new 1000
70
    cthreads = 99.times.map do |i|
71
      Thread.new do
72
        n = 0
73
        loop do
74
          item = q.pop
75
          raise StopIteration unless item
76
          n += 1
77
        end
78
      end
79
    end
80

    
81
    N.times do
82
       100.times do |item|
83
         q.push item
84
       end
85
    end
86

    
87
    if q.respond_to? :close
88
      q.close StopIteration
89
    else
90
      cthreads.each{ q.push nil }
91
    end
92

    
93
    cthreads.each &:join
94
  end
95

    
96
  bm.report '02 producer 01 consumer' do
97
    q = SizedQueue.new 1000
98
    cthreads = 1.times.map do |i|
99
      Thread.new do
100
        n = 0
101
        loop do
102
          item = q.pop
103
          raise StopIteration unless item
104
          n += 1
105
        end
106
      end
107
    end
108

    
109
    count_pthreads = 2
110
    pthreads = count_pthreads.times.map do |i|
111
      Thread.new do
112
        (N / count_pthreads).times do
113
          100.times do |item|
114
            q.push item
115
          end
116
        end
117
      end
118
    end
119

    
120
    pthreads.each &:join
121

    
122
    if q.respond_to? :close
123
      q.close StopIteration
124
    else
125
      cthreads.each{ q.push nil }
126
    end
127

    
128
    cthreads.each &:join
129
  end
130

    
131
  bm.report '99 producer 01 consumer' do
132
    q = SizedQueue.new 1000
133
    cthreads = 1.times.map do |i|
134
      Thread.new do
135
        n = 0
136
        loop do
137
          item = q.pop
138
          raise StopIteration unless item
139
          n += 1
140
        end
141
      end
142
    end
143

    
144
    count_pthreads = 99
145
    pthreads = count_pthreads.times.map do |i|
146
      Thread.new do
147
        (N / count_pthreads).times do
148
          100.times do |item|
149
            q.push item
150
          end
151
        end
152
      end
153
    end
154

    
155
    pthreads.each &:join
156

    
157
    if q.respond_to? :close
158
      q.close StopIteration
159
    else
160
      cthreads.each{ q.push nil }
161
    end
162

    
163
    cthreads.each &:join
164
  end
165
end