THREADS=20
CVS="cvs -z3 -d :pserver:anonymous@eugen-desktop.nexus:/e co e17 >/dev/null 2>/dev/null"
SVN="svn checkout svn://eugen-desktop.nexus/trunk  >/dev/null 2> /dev/null"
SVN_HTTP="svn checkout http://eugen-desktop.nexus/svn/trunk e17/ >/dev/null 2> /dev/null"
GIT="git-clone git://eugen-desktop/e.git/ e17 >/dev/null 2>/dev/null"
GIT_HTTP="git-clone http://eugen-desktop/e.git/ e17 >/dev/null 2>/dev/null"

def name(object)
  return 'CVS (pserver)' if object==CVS
  return 'Subversion (svnserve)' if object==SVN
  return 'Subversion (webdav)' if object==SVN_HTTP
  return 'Git (git protocol)' if object==GIT
  return 'Git (http)' if object==GIT_HTTP
end

# For every one of the SCMs and methods we test with
for scm in [CVS,SVN,SVN_HTTP,GIT,GIT_HTTP] do
  threads=[]
  sum=0.0
  puts("Benchmarking #{name(scm)} with #{THREADS} threads... Please wait (this can really take a while)")
  for i in 1..THREADS do
    thr=Thread.new do
      # We grab the current time, 
      # run the checkout command then grab the time again
      t1=Time.now
      system("mkdir #{i} && cd #{i} && #{scm}")
      t2=Time.now 
    
      # Update the sum variable
      sum+=t2-t1
    end
    threads << thr
  end 

  # Make sure the main thread waits for'em
  threads.each do |thr|
    thr.join
  end
  
  # And the moment we've all been waiting for 
  average_time=sum/THREADS
  puts
  puts("* #{name(scm)} --> average checkout time: #{average_time}s")

  # Now delete the files we've downloaded so the next SCM cand work
  puts("Deleting downloaded files ...")
  for i in 1..THREADS do
    system("rm -rf #{i}/")
  end
  puts("Done. Let things cool down a little and press enter to continue")
  gets
end
