一応とっておくその2

名前解決をスレッド20個で処理するようにした版
http://techracho.bpsinc.jp/piichan1031/2010_07_05/2030

require 'resolv'
require "thread"

def multi_process(ary, concurrency = 20, qsize = nil)
  q = (qsize) ? SizedQueue.new(qsize) : Queue.new

  producer = Thread.start(q, concurrency){|p_q, p_c|
    ary.each_with_index do |item, index|
      q.enq [ item, index, true]
    end

    p_c.times{ q.enq [nil, nil, false] }
  }

  workers = []
  concurrency.times do
    workers << Thread.start(q){ |w_q|
      task, index, flag = w_q.deq
      while flag
        yield task, index
        task, index, flag = w_q.deq
      end
    }
  end

  producer.join
  workers.each{|w| w.join }
end

logArray = []
ipsArray = []
namesArray = []

open("/var/log/httpd/apache_log") do |file|
  file.each do |s|

    logArray << s.split(nil)
    logArray.flatten
    ipsArray << logArray.shift
  end
end

lock = Mutex.new
multi_process(ipsArray) do |ip, index|
  lock.synchronize{ namesArray << Resolv.getnames(ip.shift) }
end

names = namesArray.uniq
hostsRank = Hash::new

names.each{|name|

  cnt = 0

  namesArray.each{|names|
    if(name == names)
      cnt = cnt + 1 
    end
  }

  hostsRank[name] = cnt

}
print hostsRank.sort_by{|key,val| -val}