4

在本地测试并发的最佳方法是什么?即我想测试 10 个并发命中。我知道像Blitz这样的服务。但是,我试图找到一种更简单的方法在本地进行测试以针对竞争条件进行测试。

有任何想法吗?也许通过卷曲?

4

2 回答 2

5

查看 Apache Bench ( ab)。基本用法非常简单:

ab -n 100 -c 10 http://your.application
于 2012-08-14T02:21:31.213 回答
0

对于测试中的本地测试竞争条件,您可以使用这样的助手

# call block in a forked process
def fork_with_new_connection(config, object = nil, options={})
  raise ArgumentError, "Missing block" unless block_given?
  options = {
     :stop => true, :index => 0
  }.merge(options)

  fork do
    # stop the process after fork
    Signal.trap('STOP') if options[:stop]
    begin
      ActiveRecord::Base.establish_connection(config)
      yield(object)
    ensure
      ActiveRecord::Base.remove_connection
    end
  end
end

# call multiply times blocks
def multi_times_call_in_fork(count=3, &block)
  raise ArgumentError, "Missing block" unless block_given?
  config = ActiveRecord::Base.remove_connection
  pids = []
  count.times do |index|
    pids << fork_with_new_connection(config, nil, :index=>index, &block)
  end

  # continue forked processes
  Process.kill("CONT", *pids)

  Process.waitall
  ActiveRecord::Base.establish_connection(config)
end

# example
multi_times_call_in_fork(5)  do
  # do something with race conditions
  # add asserts 
end  
于 2012-08-14T06:55:48.120 回答