<commit_before>require 'redis'

namespace :events do

  # add lib to the default include path
  unless $:.include?(File.dirname(__FILE__) + '/../lib/')
    $: << File.dirname(__FILE__) + '/../lib'
  end

  require 'flapjack/configuration'
  require 'flapjack/data/event'
  require 'flapjack/data/entity_check'

  def get_initial_state(prng, p_initial_state_ok)
    if prng.rand < p_initial_state_ok
      'OK'
    else
      'CRITICAL'
    end
  end

  desc "benchmark event set 1"
  task :benchmark_set_1 do
    num_checks_per_entity = 5
    num_entities = 100
    num_checks = num_checks_per_entity * num_entities
    interval = 60
    cycles_per_hour    = (60 * 60) / interval
    cycles_per_day     = (60 * 60 * 24) / interval
    cycles_per_week    = (60 * 60 * 24 * 7) / interval
    cycles_per_month   = (60 * 60 * 24 * 7 * 30) / interval
    failure_prob_min   = 1.0 / cycles_per_month
    failure_prob_max   = 1.0 / cycles_per_hour
    recovery_prob_min  = 1.0 / cycles_per_week
    recovery_prob_max  = 1.0
    p_initial_state_ok = 1

    prng = Random.new(42)

    FLAPJACK_ENV = ENV['FLAPJACK_ENV'] || 'development'
    config_file = File.join('etc', 'flapjack_config.yaml')

    config = Flapjack::Configuration.new
    config.load( config_file )

    @config_env = config.all
    @redis_config = config.for_redis

    if @config_env.nil? || @config_env.empty?
      puts "No config data for environment '#{FLAPJACK_ENV}' found in '#{config_file}'"
      exit(false)
    end

    redis = Redis.new(@redis_config)

    ok = 0
    critical = 0
    check_id = 1
    entities = (1..num_entities).to_a.inject({}) {|memo, id|
      checks = (1..num_checks_per_entity).to_a.inject({}) {|memo_check, id_check|
        memo_check[check_id] = {:name => "Check Type #{id_check}",
                                :state => ( prng.rand < p_initial_state_ok ? 'OK' : 'CRITICAL' ),
                                :p_failure => prng.rand(failure_prob_min..failure_prob_max),
                                :p_recovery => prng.rand(recovery_prob_min..recovery_prob_max)}
        ok       += 1 if memo_check[check_id][:state] == 'OK'
        critical += 1 if memo_check[check_id][:state] == 'CRITICAL'
        check_id += 1
        memo_check
      }
      memo[id] = checks
      memo
    }
    puts "ok: #{ok * 100.0 / num_checks}% (#{ok}), critical: #{100.0 * critical / num_checks}% (#{critical})"

    events_created  = 0
    ok_to_critical  = 0
    critical_to_ok  = 0
    ok_events       = 0
    critical_events = 0
    state_changes   = 0
    (0..(cycles_per_hour)).to_a.each {|i|
      changes = 0
      ok = 0
      critical = 0
      summary = "You tell me summer's here \nand the time is wrong \n"
      summary << "You tell me winter's here \nAnd your days are getting long"
      entities.each_pair {|entity_id, checks|
        checks.each_pair {|check_id, check|
          changed = false
          previous_state = check[:state]
          case previous_state
          when "OK"
            if prng.rand < check[:p_failure]
              check[:state] = "CRITICAL"<commit_msg>parameterise benchmark task, add flapjack start and stop etc<commit_after>require 'redis'
require 'oj'

namespace :events do

  # add lib to the default include path
  unless $:.include?(File.dirname(__FILE__) + '/../lib/')
    $: << File.dirname(__FILE__) + '/../lib'
  end

  require 'flapjack/configuration'
  require 'flapjack/data/event'
  require 'flapjack/data/entity_check'

  FLAPJACK_ENV = ENV['FLAPJACK_ENV'] || 'test'
  config_file = File.join('etc', 'flapjack_config.yaml')

  config = Flapjack::Configuration.new
  config.load( config_file )

  @config_env = config.all
  @redis_config = config.for_redis

  if @config_env.nil? || @config_env.empty?
    puts "No config data for environment '#{FLAPJACK_ENV}' found in '#{config_file}'"
    exit(false)
  end

  redis = Redis.new(@redis_config)

  desc "nukes the redis db, generates the events, runs and shuts down flapjack"
  task :clean_run_benchmark => [:reset_redis, :benchmark, :shutdown, :run_flapjack]

  desc "reset the redis database"
  task :reset_redis do
    raise "I'm not going to let you reset your production redis db, sorry about that." if FLAPJACK_ENV.downcase == "production"
    puts "db size before: #{redis.dbsize}"
    redis.flushdb
    puts "db size after: #{redis.dbsize}"
  end

  desc "add a shutdown event to the events queue"
  task :shutdown do
    redis.lpush('events', Oj.dump('type'    => 'shutdown',
                                  'state'   => ''))
  end

  desc "starts flapjack"
  task :run_flapjack do
    puts "Starting flapjack..."
    if system({"FLAPJACK_ENV" => FLAPJACK_ENV}, "bin/flapjack start --no-daemonize")
      puts "Flapjack run completed successfully"
    else
      puts "Problem starting flapjack: #{$?}"
    end
  end

  desc "run benchmark - simulate a stream of events from the check execution system"
  task :benchmark do

    num_checks_per_entity = (ENV['CHECKS_PER_ENTITY'] || 5).to_i
    num_entities          = (ENV['ENTITIES'] || 100).to_i
    interval              = (ENV['INTERVAL'] || 60).to_i
    hours                 = (ENV['HOURS'] || 1).to_f
    seed                  = (ENV['SEED'] || 42).to_i

    puts "Behaviour can be modified by setting any combination of the following environment variables: "
    puts "CHECKS_PER_ENTITY - #{num_checks_per_entity}"
    puts "ENTITIES          - #{num_entities}"
    puts "INTERVAL          - #{interval}"
    puts "HOURS             - #{hours}"
    puts "SEED              - #{seed}"
    puts "FLAPJACK_ENV      - #{FLAPJACK_ENV}"

    cycles_per_hour    = (60 * 60) / interval
    cycles_per_day     = (60 * 60 * 24) / interval
    cycles_per_week    = (60 * 60 * 24 * 7) / interval
    cycles_per_month   = (60 * 60 * 24 * 7 * 30) / interval
    failure_prob_min   = 1.0 / cycles_per_month
    failure_prob_max   = 1.0 / cycles_per_hour
    recovery_prob_min  = 1.0 / cycles_per_week
    recovery_prob_max  = 1.0
    p_initial_state_ok = 1
    num_checks = num_checks_per_entity * num_entities

    prng = Random.new(seed)



    ok = 0
    critical = 0
    check_id = 1
    entities = (1..num_entities).to_a.inject({}) {|memo, id|
      checks = (1..num_checks_per_entity).to_a.inject({}) {|memo_check, id_check|
        memo_check[check_id] = {:name => "Check Type #{id_check}",
                                :state => ( prng.rand < p_initial_state_ok ? 'OK' : 'CRITICAL' ),
                                :p_failure => prng.rand(failure_prob_min..failure_prob_max),
                                :p_recovery => prng.rand(recovery_prob_min..recovery_prob_max)}
        ok       += 1 if memo_check[check_id][:state] == 'OK'
        critical += 1 if memo_check[check_id][:state] == 'CRITICAL'
        check_id += 1
        memo_check
      }
      memo[id] = checks
      memo
    }
    puts "ok: #{ok * 100.0 / num_checks}% (#{ok}), critical: #{100.0 * critical / num_checks}% (#{critical})"

    events_created  = 0
    ok_to_critical  = 0
    critical_to_ok  = 0
    ok_events       = 0
    critical_events = 0
    state_changes   = 0
    (0..(hours * cycles_per_hour)).to_a.each {|i|
      changes = 0
      ok = 0
      critical = 0
      summary = "You tell me summer's here \nand the time is wrong \n"
      summary << "You tell me winter's here \nAnd your days are getting long"
      entities.each_pair {|entity_id, checks|
        checks.each_pair {|check_id, check|
          changed = false
          previous_state = check[:state]
          case previous_state
          when "OK"
            if prng.rand < check[:p_failure]
              check[:state] = "CRITICAL"
{'fraction_non_alphanumeric': 0.11884550084889643, 'fraction_numerical': 0.018414522659004832, 'mean_word_length': 2.424865831842576, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1155208', 'n_tokens_mistral': 2744, 'n_tokens_neox': 2585, 'n_words': 625}