#
# Copyright (C) 2008  Olaf Märker
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

require 'rubygems'
require 'logger'
require 'rake'
gem     'activerecord'
require 'active_record'
require 'pivm'

$LOAD_PATH << './lib'
require 'db/pivm_build'
 
desc "Clean restart, drops ALL data!"
task :cleandb do
  clean_pivm_database
end
  
desc "Establish the connection"
task :connectdb do
  connect_xenodot_database
end
  
desc "Migrate the database through scripts in db. Target specific version with VERSION=x"
task :migrate => :connectdb do
  migrate_pivm_database(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
end
  
desc "Tests the specifications matching spec/**/*_spec.rb"
task :spec do
  require 'spec/rake/spectask'
  $PIVM_SPEC_PATTERN = ENV['SPEC_PATTERN'] || 'spec/**/*_spec.rb'
  Spec::Rake::SpecTask.new(:spec_impl) do |spec|
    spec.spec_opts = ['-f', 's', '-r', 'config/spec_env']
    spec.pattern = $PIVM_SPEC_PATTERN
  end
  Rake::Task[:spec_impl].invoke
end
  
  desc %q{Start some benchmarks to measure the performance of the system
			Use seconds=<number> to set the duration of the benchmark in seconds(default 20)
			Use processes=<number> to control the number of concurrent processes (default 2)
			Use mult=<number> to control the amount of the testdata (default 4)
			Use profile=y to enable profiling}

task :bench do
	$XENODOT_ENV='xenodot_test'
	Xenodot::Backend::Database.connect!
	reset_pivm_database
	
	require 'benchmark/vm_bench.rb'
	
	seconds = ENV['seconds'] || 20
	processes = ENV['processes'] || 2
	mult = ENV['mult'] || 4
	profile = (ENV['profile'] =~ /^(y(es)?|on|1|enable(d)?)$/) ? true : false
	
	bench = VmBench.new(mult.to_i)
	bench.start(seconds.to_i,processes.to_i,profile)
end

desc "Start the server"
task :server do
	require 'pivm/lib/server'
end