經測試,RUBY的線程用在網絡這一塊還是有點用

當網絡阻塞時,雖然RUBY的線程是非原生線程,其作用還是在

[b]服務器端[/b]
用phpg寫了一個小頁面,軟件是用nginx+fastcgi,spawn-fcgi設置-C 爲 20,啓動20個進程,這樣在多線程測試纔有效,不然就是一個php cgi進程上面的隊列,必須等第一個請求完成,第二個請求才會處理

php頁面代碼

<?php
sleep(3);
phpinfo();
?>


[b]rub測試端[/b]

require 'open-uri'
s = Time.now
puts s
10.times do
open("http://localhost/index.php")
end
e = Time.now
puts e
puts "no thread #{e -s}"

threads = []
s = Time.now
puts s
10.times do
threads.push(Thread.new{ open("http://localhost/index.php") })
end
while true
threads.each {|t|
if t.alive?
Thread.pass
break
else
threads.delete t
end
}
break if threads.length < 1
end
e = Time.now
puts e
puts "thread #{e -s}"


測試結果,不用線程耗時30秒,啓動10個線程,耗時3秒
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章