Module: RobotHelper
Instance Method Summary collapse
- 
  
    
      #robot(total_num, thread_num = 10)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
功能: 灌数据专用方法.
 
Instance Method Details
#robot(total_num, thread_num = 10) ⇒ Object
功能:
灌数据专用方法
参数解释:
- 
total_num 灌数据的总数
 - 
thread_num 使用的线程数,默认为10
 
Example:
# 开20个线程来并发进行操作
robot 100,20 do |i|
    get "http://www.baidu.com/"
end
  
      33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  | 
    
      # File 'lib/common/robot_helper.rb', line 33 def robot(total_num,thread_num=10) @is_press = true threads = [] step = (total_num.to_f/thread_num.ceil).ceil raise "total_num must >0" if step <= 0 remain = total_num % thread_num remain = thread_num if remain == 0 cur = 1 remain.times do |i| threads << Thread.new(step) do |mystep| mystep.times { |j| yield(cur);cur+=1 } end end remain.upto(thread_num-1) do |i| threads << Thread.new(step-1) do |mystep| mystep.times { |j| yield(cur);cur+=1 } end end threads.each { |aThread| aThread.join } @is_press = false $log.debug "robot total_num[#{total_num}] thread_num[#{thread_num}] success!" end  |