teste_thread_schedule_2.rb

Alexandre Riveira, 07/06/2014 07:33 AM

Download (1.05 KB)

 
1
#puts "definindo taskset -c -p 1 #{Process.pid}"
2
`taskset -c -p 2 #{Process.pid}`
3
require 'pg'
4

    
5
inicio_geral = Time.now
6
t1 = Thread.new do 
7
  Thread.current[:target] = 0
8
  while true
9
    Thread.current[:target] += 1 
10
  end
11
end
12

    
13
10.downto(0) do |i|
14
  inicio = Time.now
15
  Kernel.sleep(1)
16
  puts "faltando #{i} segundos (#{Time.now - inicio})"
17
end
18

    
19
puts "zerando thread com contagem em #{t1[:target]}"
20
t1[:target] = 0
21

    
22
t2 = Thread.new do
23
  Thread.current[:target] = 0
24
  conn  = PGconn.open(:dbname => 'mydatabase', :user => 'postgres')
25
  while true
26
    conn.exec("SELECT * FROM mytable WHERE id = 1")
27
    Thread.current[:target] += 1
28
  end
29
end
30

    
31
t3 = Thread.new do
32
  Thread.current[:target] = 0
33
  while true
34
    Thread.current[:target] += 1
35
  end
36
end
37
10.times do
38
    puts "first #{t1[:target]}"
39
    puts "second #{t2[:target]}"
40
    puts "third #{t3[:target]}"
41
    puts "\n\n"
42
    sleep(1)
43
end
44
total = t1[:target].to_i + t2[:target].to_i + t3[:target].to_i
45
puts "total #{total}"
46
puts "media #{total/(Time.now - inicio_geral).to_i}"
47
puts "\n\n"
48
puts "time #{Time.now - inicio_geral}"
49