<commit_before>module Stressque
  class Injector
    attr_reader :harness, :redis
    attr_accessor :running

    def initialize(harness)
      @harness = harness
      @redis = Resque.redis
    end

    def run
      running = true
      mark_start
      while(running) do
        if too_fast?
          sleep(0.003)
          next
        end
        inject
      end
    end

    def total_injections
      redis.get(injections_key).to_i
    end

    def current_rate
      return 0 if elapsed_time == 0
      total_injections.to_f / elapsed_time
    end

    def too_fast?
      current_rate > harness.target_rate
    end

    private
    def inject
      klass = harness.pick_job_def.to_job_class
      Resque.enqueue klass
      redis.incr injections_key
    end

    def mark_start
      redis.set(start_key, current_time)
      sleep(1)
    end

    def start_time
      redis.get(start_key).to_i
    end

    def current_time
      redis.time.first.to_i
    end

    def elapsed_time
      current_time - start_time
    end

    def injections_key
      "stressque:#{harness.name}:injections"
    end

    def start_key
      "stressque:#{harness.name}:start"<commit_msg>Compatible with Redis < 2.6<commit_after>module Stressque
  class Injector
    attr_reader :harness, :redis
    attr_accessor :running

    def initialize(harness)
      @harness = harness
      @redis = Resque.redis
    end

    def run
      running = true
      mark_start
      while(running) do
        if too_fast?
          sleep(0.003)
          next
        end
        inject
      end
    end

    def total_injections
      redis.get(injections_key).to_i
    end

    def current_rate
      return 0 if elapsed_time == 0
      total_injections.to_f / elapsed_time
    end

    def too_fast?
      current_rate > harness.target_rate
    end

    private
    def inject
      klass = harness.pick_job_def.to_job_class
      Resque.enqueue klass
      redis.incr injections_key
    end

    def mark_start
      redis.set(start_key, current_time)
      sleep(1)
    end

    def start_time
      redis.get(start_key).to_i
    end

    def current_time
      redis.info.fetch("uptime_in_seconds").to_i
    end

    def elapsed_time
      current_time - start_time
    end

    def injections_key
      "stressque:#{harness.name}:injections"
    end

    def start_key
      "stressque:#{harness.name}:start"
{'fraction_non_alphanumeric': 0.05845511482254697, 'fraction_numerical': 0.006680584551148226, 'mean_word_length': 2.0100502512562812, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '484270', 'n_tokens_mistral': 825, 'n_tokens_neox': 773, 'n_words': 174}