view paper/log/calc_sd.rb @ 62:2cb5ac9282b0

writed description of ods.put
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sat, 01 Feb 2014 11:44:34 +0900
parents 8344c4493e61
children
line wrap: on
line source

#!/usr/local/bin/ruby

class Array
  def average
    inject(0.0) { |sum, i| sum += i } / size
  end

  def variance
    ave = average
    inject(0.0) { |sum, i| sum += (i - ave)**2 } / size
  end

  def standard_devitation
    Math::sqrt(variance)
  end
end


 def log_select(filename) 
   return logArray = File.open(filename, "r") do |file|
      file.readlines.select{|line| line =~ /sec/ }
   end 
 end

 def create_table(logArray)
    totalFailed = 0
    timeArray = []
    logArray.each{|line|
     tokens = line.split(' ')
     sec = tokens[0].to_i * 1000
     millsec = tokens[2].to_i
     time = sec + millsec
     timeArray.push(time)
     totalFailed = totalFailed + tokens[4].to_i
    }
   if totalFailed != 0 then
     puts "totalFailed :" + totalFailed.to_s
   end
   size = timeArray.size().to_s
   return {size => timeArray}
 end


 def add_table(table1, table2)
   t = {}
   table1.each {|key, value|
     if table2.key?(key) then
	t[key] = value.concat(table2[key])
     else
        t[key] = value
     end
   }
   table2.each {|key, value|
     if table1.key?(key) then
       # nop continue
     else
        t[key] = value
     end
    }
   return t
 end

 def print_log(nodes, array)
    average = (array.average.to_i).to_f / 1000 
    sd = (array.standard_devitation.to_i).to_f / 1000
    puts nodes.to_s + " " + average.to_s + " " + sd.to_s
 end

 def start(files)
   totalTable = {}
   files.each {|filename| 
     logArray = log_select(filename)
     table = create_table(logArray)
     totalTable = add_table(table, totalTable)
    }

    totalTable.each {|key, value|
	print_log(key, value)
    }
   
 end

start(ARGV)