#!/usr/bin/env ruby

# will-it-scale.workload is defined as the total number of operation for
# all processes/threads
LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/statistics"

scalability = []
per_process = []
per_thread = []
workload = 0

# tasks,processes,processes_idle,threads,threads_idle,linear
# 0,0,100,0,100,0
# 192,0,0.00,46252474,0.10,0
while (line = STDIN.gets)
  case line.chomp!
  when /^[a-z_,]+$/
    names = line.split ','
    names.shift
  when /^[0-9.,]+$/
    values = line.split ','
    nr_task = values.shift
    next if nr_task == '0'

    processes = values[0].to_i
    threads = values[2].to_i

    # bad sample when stderr has
    # tasks,processes,processes_idle,threads,threads_idle,linear
    # 0,0,100,0,100,0
    # 192,0,0.00,0,0.08,0
    #
    # when stderr is
    # context_switch1_threads: tests/context_switch1.c:33: child: Assertion `ret == 1' failed.
    # context_switch1_threads: tests/context_switch1.c:33: child: Assertion `ret == 1' failed.
    # Aborted
    next if processes.zero? && threads.zero?

    linear = values[-1].to_i
    scalability << values[0].to_f / linear if linear != 0 && nr_task != '1'
    per_process << processes.to_f / nr_task.to_i
    workload += processes.to_f
    per_thread << threads.to_f / nr_task.to_i
    workload += threads.to_f

    names.each_with_index do |name, i|
      puts nr_task + '.' + name + ': ' + values[i]
    end
  when /(Assertion .* failed)/
    puts $1.sub(/.*: /, '').tr('^a-zA-Z0-9<>!=', '_').gsub(/__+/, '_') + ': 1'
  end
end

printf "scalability: %f\n", scalability[-1] unless scalability.empty?
printf "per_process_ops: %d\n", per_process.average unless per_process.empty?
printf "per_thread_ops: %d\n", per_thread.average unless per_thread.empty?
printf "workload: %d\n", workload unless workload.zero?
