use Win32;
use Win32::Process;
use Win32::Process (STILL_ACTIVE);
$run_time = 60;
$sleep_buffer = 15;
$haircut_time = 0.3;
$user = 'scott';
$pass = 'tiger';
sub ErrorReport{
print Win32::FormatMessage( Win32::GetLastError() );
}
sub run_customers_and_barbers {
my($num_customers,$num_barbers) = @_;
system("echo truncate table worklog\; | sqlplus -s $user/$pass");
system("echo update customers set needs_cut=0,entered_shop=null\; | sqlplus -s $user/$pass");
system("echo commit\; | sqlplus -s $user/$pass");
run_customers($num_customers);
run_barbers($num_barbers);
}
sub run_customers {
my($num_customers) = @_;
$pid = fork;
if($pid == 0) {
exec("echo exec barber_shop.add_customers\($num_customers,$run_time\) | sqlplus -s $user/$pass");
}
}
sub run_barbers {
my($num_barbers) = @_;
for($i=0;$i<$num_barbers;$i++)
{
$pid = fork;
if($pid == 0) {
#print "forked!\n";
exec("echo exec barber_shop.add_barber\($haircut_time,$run_time\) | sqlplus -s $user/$pass");
}
}
}
sub test_num_customers {
system("echo create table barbershop_1 \(num_custs number, avg_wait number, max_wait number, throughput number\)\; | sqlplus -s $user/$pass");
system("echo truncate table barbershop_1\; | sqlplus -s $user/$pass");
for (my $c = 1;$c<16;$c++) {
printf("running $c customers with 3 babrbers\n");
run_customers_and_barbers($c,3);
sleep $run_time+$sleep_buffer;
system("echo insert into barbershop_1 select $c,avg\(to_number\(replace(event,\'waited: \',\'\'\)\)\) as avg_wait,max\(to_number\(replace\(event,\'waited: \',\'\'\)\)\) as max_wait,count\(\*\) as throughput from worklog where event like \'waited:\%\'\; | sqlplus -s $user/$pass");
system("echo commit\;| sqlplus -s $user/$pass");
};
system("echo select \* from barbershop_1\ order by num_custs; | sqlplus -s $user/$pass");
}
sub test_num_barbers {
system("echo create table barbershop_2 \(num_barbers number, avg_wait number, max_wait number, throughput number\)\; | sqlplus -s $user/$pass");
system("echo truncate table barbershop_2\; | sqlplus -s $user/$pass");
for (my $b=1;$b<9;$b++) {
printf("running 3 customers with $b babrbers\n");
run_customers_and_barbers(3,$b);
sleep $run_time+$sleep_buffer;
system("echo insert into barbershop_2 select $b,avg\(to_number\(replace\(event,\'waited: \',\'\'\)\)\) as avg_wait,max\(to_number\(replace\(event,\'waited: \',\'\'\)\)\) as max_wait,count\(\*\) as throughput from worklog where event like \'waited:\%\'\; | sqlplus -s $user/$pass");
system("echo commit\;| sqlplus -s $user/$pass");
};
system("echo select \* from barbershop_2\ order by num_barbers; | sqlplus -s $user/$pass");
}
# under window, where fork behaves strangely, it is safer to run tests seperately (only one of the lines uncommented)
test_num_customers;
#test_num_barbers;