<commit_before>#!/usr/bin/env ruby

# open log file
log = File.new("uat.out",  "w")

# redirect stderr to log file
$stderr = log

results = {}
total_examples = 0
total_fail = 0

Dir["spec/uat/*#{ARGV[0]}.rb"].each do |path|
  cmd  = "spec #{path}"
  puts "Launching #{cmd}"
  $stdout.flush
  log.write "#{cmd}\n"
  output = `#{cmd} 2>&1`
  if (output.match(/(\d+) examples?, (\d+) failures?/))
    results[path] = { "examples" => $1.to_i,
                      "failures" => $2.to_i }
    total_examples += $1.to_i
    total_fail += $2.to_i
    #puts "Examples: #{$1}, Failures: #{$2}"
  end
  log.write(output)
  log.flush
end

printf("%-60s%10s%10s\n", 'Name', 'Examples', 'Failures')
results.keys.each do |test|
  printf("%-60s%10s%10s\n", test, results[test]['examples'], results[test]['failures'])
end

puts ""
printf("%-60s%10s%10s\n", 'Total', total_examples, total_fail)<commit_msg>- Added threading to support concurrent selenium tests on one or more RC servers.  Also added a few command-line options.<commit_after>#!/usr/bin/env ruby

require 'thread'
require 'optparse'
require 'optparse/time'
require 'ostruct'

class UserAcceptanceTest
  def initialize()  
    @lock = Mutex.new  # For thread safety  
    @options = parse(ARGV)
    @server = @options.server.split(/,\s*/)
    @port = @options.port
    @concurrent = @options.concurrent
    @results = {}
    @total_examples = 0
    @total_fail = 0
    @uats = Dir.glob("spec/uat/#{@options.tests}.rb")
  end  

  def run()
    @threads = []
    @server.each do |s|
      1 .. @concurrent.to_i.times do |c|
        t = Thread.new {
          log = File.new("uat.#{s}-#{c}.#{@port}.out", "w")
          $stderr = log # redirect stderr to log file
          while (path = nextUAT()) do
            cmd  = "spec #{path}"
            puts "Launching #{s}-#{c}:#{@port} => #{cmd}"
            $stdout.flush
            log.write "#{cmd}\n"
            output = `export SEL_RC_SERVER=#{s}; export RC_PORT=#{@port}; #{cmd} 2>&1`
            if (output.match(/(\d+) examples?, (\d+) failures?/))
              @lock.synchronize {
                @results[path] = { "examples" => $1.to_i,
                                   "failures" => $2.to_i }
                @total_examples += $1.to_i
                @total_fail += $2.to_i
              }
            end
            log.write(output)
            log.flush
          end
        }
        @threads.push(t)
      end
    end

    # wait for threads to finish
    @threads.each do |t|
      t.join
    end

    printf("%-60s%10s%10s\n", 'Name', 'Examples', 'Failures')
    results.keys.each do |test|
      file = test.sub(/^.*\//, '')
      printf("%-60s%10s%10s\n", file, results[test]['examples'], results[test]['failures'])
    end

    puts ""
    printf("%-60s%10s%10s\n", 'Total', total_examples, total_fail)
  end
  
  private

  def parse(args)
    # The options specified on the command line will be collected in *options*.
    # We set default values here.
    options = OpenStruct.new
    options.server = "localhost"
    options.port = 4444
    options.tests = '*'
    options.concurrent = 1

    opts = OptionParser.new do |opts|
      opts.banner = "Usage: run_uats.rb [options]"

      opts.separator ""
      opts.separator "Specific options:"

      opts.on("-s", "--server HOST",
              "Selenium RC host.  Separate multiples with commas.") do |server|
        options.server = server
      end

      opts.on("-p", "--port PORT",
              "Selenium RC tcp port.") do |port|
        options.port = port
      end

      opts.on("-t", "--tests GLOB",
              "GLOB pattern for UATs to be run.") do |tests|
        options.tests = tests
      end

      opts.on("-c", "--concurrent INTEGER",
              "Number of concurrent UATs to run per server.") do |concurrent|
        options.concurrent = concurrent
      end
    end

    opts.parse!(args)
    options
  end  # parse()`

  def nextUAT
    @lock.synchronize {
      @t = @uats.pop
    }
  end

  def options
    @options
  end

  def server
    @server
  end

  def port
    @port
  end

  def results
    @results
  end

  def total_examples
    @total_examples
  end

  def total_fail
    @total_fail
  end
  
end  

uat = UserAcceptanceTest.new()
uat.run()
{'fraction_non_alphanumeric': 0.15548780487804878, 'fraction_numerical': 0.014305816135084427, 'mean_word_length': 2.154585798816568, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '4492638', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1438, 'n_words': 361}