<commit_before>
module Stella::Engine
  module Functional
    extend Stella::Engine::Base
    extend self
    
    def run(plan, opts={})
      opts = process_options! plan, opts
      
      if Stella::Engine.service
        Stella::Engine.service.testplan_sync plan
        Stella::Engine.service.testrun_create opts
      end
      
      Stella.ld "OPTIONS: #{opts.inspect}"
      Stella.stdout.info2 "Hosts: " << opts[:hosts].join(', ') if !opts[:hosts].empty?
      
      client = Stella::Client.new opts[:hosts].first, 1, opts
      client.add_observer(self)
      
      client.enable_nowait_mode if opts[:nowait]
      
      Stella.stdout.info2 $/, "Starting test...", $/
      
      # Identify this thread to Benelux
      Benelux.current_track :functional 
      
      start_time = Time.now.utc
      
      dig = Stella.stdout.lev > 1 ? plan.digest_cache : plan.digest_cache.shorter
      Stella.stdout.info " %-65s  ".att(:reverse) % ["#{plan.desc}  (#{dig})"]
      plan.usecases.each_with_index do |uc,i|
        desc = (uc.desc || "Usecase ##{i+1}")
        dig = Stella.stdout.lev > 1 ? uc.digest_cache : uc.digest_cache.shorter
        Stella.stdout.info ' %-65s '.att(:reverse).bright % ["#{desc}  (#{dig}) "]
        Stella.rescue { client.execute uc }
      end
      
      test_time = Time.now.utc - start_time
      
      # Need to use thread timeline b/c the clients are running in the
      # main thread which Benelux.update_global_timeline does not touch.
      tt = Benelux.thread_timeline
      
      #puts tt.messages.filter(:kind => :log)
      
      failed = tt.stats.group(:failed).merge
      total = tt.stats.group(:do_request).merge
      
      if Stella::Engine.service
        Stella::Engine.service.testrun_summary :successful => (total.n-failed.n),
                                               :failed => failed.n,
                                               :duration => test_time
      end
      
      failed == 0
    end
    
    
    def update_prepare_request(client_id, usecase, req, counter)
      notice = "repeat: #{counter-1}" if counter > 1
      dig = Stella.stdout.lev > 1 ? req.digest_cache : req.digest_cache.shorter
      desc = "#{req.desc}  (#{dig}) "
      Stella.stdout.info2 "  %-46s %16s ".bright % [desc, notice]
    end
    
    
    def update_receive_response(client_id, usecase, uri, req, params, headers, counter, container)
      log = Stella::Engine::Log.new Time.now.to_f, container.unique_id, client_id,
                                    'testrunid',
                                    usecase.digest.short, req.digest.short,
                                    req.http_method, container.status, uri,
                                    params, headers
      Benelux.thread_timeline.add_message log.to_json,<commit_msg>Log to Benelux message<commit_after>
module Stella::Engine
  module Functional
    extend Stella::Engine::Base
    extend self
    
    def run(plan, opts={})
      opts = process_options! plan, opts
      
      if Stella::Engine.service
        Stella::Engine.service.testplan_sync plan
        Stella::Engine.service.testrun_create opts
      end
      
      Stella.ld "OPTIONS: #{opts.inspect}"
      Stella.stdout.info2 "Hosts: " << opts[:hosts].join(', ') if !opts[:hosts].empty?
      
      client = Stella::Client.new opts[:hosts].first, 1, opts
      client.add_observer(self)
      
      client.enable_nowait_mode if opts[:nowait]
      
      Stella.stdout.info2 $/, "Starting test...", $/
      
      # Identify this thread to Benelux
      Benelux.current_track :functional 
      
      start_time = Time.now.utc
      
      dig = Stella.stdout.lev > 1 ? plan.digest_cache : plan.digest_cache.shorter
      Stella.stdout.info " %-65s  ".att(:reverse) % ["#{plan.desc}  (#{dig})"]
      plan.usecases.each_with_index do |uc,i|
        desc = (uc.desc || "Usecase ##{i+1}")
        dig = Stella.stdout.lev > 1 ? uc.digest_cache : uc.digest_cache.shorter
        Stella.stdout.info ' %-65s '.att(:reverse).bright % ["#{desc}  (#{dig}) "]
        Stella.rescue { client.execute uc }
      end
      
      test_time = Time.now.utc - start_time
      
      # Need to use thread timeline b/c the clients are running in the
      # main thread which Benelux.update_global_timeline does not touch.
      tt = Benelux.thread_timeline
      
      puts tt.messages.filter(:kind => :log)
      
      failed = tt.stats.group(:failed).merge
      total = tt.stats.group(:do_request).merge
      
      if Stella::Engine.service
        Stella::Engine.service.testrun_summary :successful => (total.n-failed.n),
                                               :failed => failed.n,
                                               :duration => test_time
      end
      
      failed == 0
    end
    
    
    def update_prepare_request(client_id, usecase, req, counter)
      notice = "repeat: #{counter-1}" if counter > 1
      dig = Stella.stdout.lev > 1 ? req.digest_cache : req.digest_cache.shorter
      desc = "#{req.desc}  (#{dig}) "
      Stella.stdout.info2 "  %-46s %16s ".bright % [desc, notice]
    end
    
    
    def update_receive_response(client_id, usecase, uri, req, params, headers, counter, container)
      log = Stella::Engine::Log.new Time.now.to_f, container.unique_id, client_id,
                                    'testrunid',
                                    usecase.digest.short, req.digest.short,
                                    req.http_method, container.status, uri,
                                    params, headers
      Benelux.thread_timeline.add_message log.to_json,
{'fraction_non_alphanumeric': 0.11709310589907605, 'fraction_numerical': 0.006751954513148543, 'mean_word_length': 1.9075413223140496, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '222120', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1706, 'n_words': 356}