#!/usr/bin/env ruby

$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
require 'railsbench/perf_info'

files = []
if ARGV.length==0
  $stderr.puts "usage: perf_times file1 file2 ..."
  $stderr.exit 1
else
  ARGV.each do |arg|
    files << File.open_or_die(arg)
  end
end

files.each do |file|
  pi = PerfInfo.new(file)
  iter = pi.iterations
  options = pi.options

  printf "\nperf data file: #{file.path}\n"
  printf "    requests=#{iter}, options=#{options}\n\n"
  k = 'loading environment'
  printf "%-32s  %9.5f\n\n", k, pi.load_time_mean
  printf "%-32s  %9s  %7s  %7s  %7s\n", 'page request', 'total', 'stddev%', 'r/s', 'ms/r'

  pi.keys.each do |k|
    t = pi.timings_mean(k)
    devp = pi.timings_stddev_percentage(k)
    urls = pi.requests_per_key
    printf "%-32s  %9.5f  %7.4f  %7.2f  %7.2f\n",
           truncate(k), t, devp, (iter*urls)/t, t*1000/(iter*urls)
  end

  printf "\n%-32s  %9.5f  %7.4f  %7.2f  %7.2f\n",
           "all requests", pi.total_time_mean, pi.total_time_stddev_percentage,
           pi.request_count/pi.total_time_mean, pi.total_time_mean*1000/pi.request_count

  if pi.gc_stats?
    printf "\n%-32s  %9s  %7s  %7s  %7s\n",
           "garbage collection statistics", "time", "stddev%", "count", "total%"
    printf "%-32s  %9.5f  %7.4f  %7.2f  %7.2f\n",
           "", pi.gc_time_mean, pi.gc_time_stddev_percentage, pi.gc_calls_mean, (pi.gc_time_mean/pi.total_time_mean)*100
  end

  file.close

end

__END__

#    Copyright (C) 2005-2008  Stefan Kaes
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
