#!/usr/bin/env ruby

options = ""
if ARGV.length==0
  puts "usage: perf_times_gc file1 file2 ..."
  exit 1
end
files=[]
ARGV.each do |arg|
  fn = arg
  fn = fn.sub(/^\/([cdefgh])(\/)/, '\1:\2') if RUBY_PLATFORM =~ /win32/
  begin
    if File.stat(fn).readable?
      files << File.open(fn)
    else
      print "file #{fn} is unreadable\n"
      exit 1
    end
  rescue
    print "file #{fn} does not exist\n"
    exit 1
  end
end

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

files.each_with_index do |file, idx|
  puts "="*32 if idx>0

  gci = GCInfo.new(file)

  printf "GC data file: #{File.expand_path(file.path)}\n\n"
  printf "requests processed     : %9d\n", gci.num_requests
  printf "collections            : %9d\n", gci.collections
  printf "garbage total          : %9d\n", gci.garbage_produced
  printf "gc time total (sec)    : %12.2f\n", gci.time_total.to_f/1000
  printf "garbage per request    : %12.2f\n", gci.garbage_produced.to_f/gci.num_requests
  printf "requests per collection: %12.2f\n", gci.num_requests.to_f/gci.collections

  printf "\n             %12s %8s %9s %9s\n", "mean", "stddev%", "min", "max"
  number_format = "%12.2f %8.1f %9d %9d"

  printf "gc time(ms): #{number_format}\n",
         gci.time_mean, gci.time_stddev_percentage, gci.time_min, gci.time_max

  printf "heap slots : #{number_format}\n",
         gci.processed_mean, gci.processed_stddev_percentage, gci.processed_min, gci.processed_max

  printf "live       : #{number_format}\n",
         gci.live_mean, gci.live_stddev_percentage, gci.live_min, gci.live_max

  printf "freed      : #{number_format}\n",
         gci.freed_mean, gci.freed_stddev_percentage, gci.freed_min, gci.freed_max

  printf "freelist   : #{number_format}\n",
         gci.freelist_mean, gci.freelist_stddev_percentage, gci.freelist_min, gci.freelist_max

  printf "\nheap topology:\n"
  printf "%9s   %6s\n", "slot_size", "#heaps"
  heaps_count = {}
  gci.topology.each{|size| heaps_count[size] = (heaps_count[size] || 0) + 1}
  heaps_count.keys.sort.each do |size|
    printf "%9d   %6d\n", size, heaps_count[size]
  end

  if gci.mallocs > 0
    printf "\nleak anlysis:\n"
    printf "mallocs/bytes : %10d / %10d\n", gci.mallocs, gci.malloced
    printf "leaks/bytes   : %10d / %10d\n", gci.leaks, gci.leaked
    printf "leaked/request: %10.2f bytes\n", gci.leaked.to_f/gci.num_requests
  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
