Mongo Ruby驱动程序并发缓慢

时间:2012-06-21 15:58:35

标签: ruby mongodb

当我使用mongo ruby​​驱动程序迭代许多文档时运行查询时,我遇到了一些使用多个线程的极慢执行时间。

基准:

require 'mongo'
require 'benchmark'

DB = Mongo::Connection.new('localhost', 27017, :pool_size => 10)['development']
@coll = DB['posts']

code = -> { @coll.find(:foo => "BAZ").each {|d| d } }
Benchmark.bm do |x|
  (1..10).each do |i| 
    x.report("#{i} single thread") do
      i.times { code.call }
    end
    x.report("#{i} multi thread") do
      thr = []
      i.times do
        thr << Thread.new do
          code.call
        end
      end
      thr.each {|t| t.join }
    end
  end
end

返回以下结果:

   user     system      total        real
  1 single thread  1.290000   0.200000   1.490000 (  1.545077)
  1 multi threado  1.340000   0.200000   1.540000 (  1.576529)
  2 single thread  2.590000   0.370000   2.960000 (  3.083968)
  2 multi thread  3.400000   1.230000   4.630000 (  3.934912)
  3 single thread  3.780000   0.550000   4.330000 (  4.506092)
  3 multi thread  5.570000   1.190000   6.760000 (  7.489573)
  4 single thread  5.260000   0.740000   6.000000 (  6.373467)
  4 multi thread  8.600000   1.820000  10.420000 ( 10.018789)
  5 single thread  6.630000   0.900000   7.530000 (  7.855555)
  5 multi thread 11.730000   2.330000  14.060000 ( 13.259765)
  6 single thread  8.050000   1.120000   9.170000 (  9.597855)
  6 multi thread 15.370000   2.750000  18.120000 ( 17.791968)
  7 single thread  9.110000   1.330000  10.440000 ( 10.994842)
  7 multi thread 19.640000   3.320000  22.960000 ( 21.921684)
  8 single thread 10.530000   1.480000  12.010000 ( 12.603867)
  8 multi thread 24.590000   3.770000  28.360000 ( 29.433455)
  9 single thread 12.360000   1.730000  14.090000 ( 15.171189)
  9 multi thread 30.170000   4.380000  34.550000 ( 33.567508)
  10 single thread 13.340000   1.930000  15.270000 ( 16.365007)
  10 multi thread 36.090000   4.800000  40.890000 ( 42.793302)

为什么代码执行速度这么慢?实际上它应该能够同时在多个线程中处理相同的查询吗?

0 个答案:

没有答案