class LogGenerator::Executors

Windowsだと割り込みが55msや10msだったりとするので100msごとに処理するように。 汚いソースになっちゃった・・。 MultimediaTimer使えばいいんだけど、めんどくさ。

Constants

FIXED_RATE

Public Class Methods

exec(config) { |{ :start_time => start_time, :total_count => total_count, :elapsed_time => (now - start_time).round, }| ... } click to toggle source
# File lib/apache-loggen/base.rb, line 235
def self.exec(config)

  rate_per_sec = config[:rate]
  display = config[:progress]

  limited = rate_per_sec > 0
  if limited then
    mspr = 1000.0 / rate_per_sec # ms per rec.
    rate = rate_per_sec.to_f / (1000 / FIXED_RATE) # rec per 100ms
  end
  start_time = Time.now

  time = last_display = Time.now
  count = 0
  total_count = 0
  while true do

    break unless yield({
      :start_time => start_time,
      :total_count => total_count,
      :elapsed_time => (Time.now - start_time).round,
    })

    total_count += 1
    count += 1

    if limited && count >= rate then
      spent = ((Time.now - time) * 1000).round
      sleep_ms = mspr - spent
      sleep(sleep_ms / 1000.0) if sleep_ms > 0
      time = Time.now
      count = 0
    end

    if display then
      if Time.now - last_display >= 1.0 then
        $stderr.printf("\r%d[rec] %.2f[rec/s]", total_count, total_count / (Time.now - start_time + 0.001))
        last_display = Time.now
      end
    end

  end
end