--- misc/rserv_test.pl 2003/08/05 09:52:40 1.1 +++ misc/rserv_test.pl 2003/08/06 00:53:27 1.2 @@ -1,34 +1,55 @@ -#!/usr/bin/perl - +#!/usr/bin/perl -w +use strict; use Pg; use Getopt::Long; use POSIX ":sys_wait_h"; $| = 1; -$control = 0; - +my $control = 0; -$result = GetOptions("numprocs=i", "numstmts=i", "help"); -if (defined($opt_help)) { - print "Usage: $0 --numprocs=number_of_processes --numstmts=number_of_statements\n"; - print "\n"; - exit(0); +my ($numprocs,$numstmts) = (4,100); +my ($debug,$verbose) = (0,0); +my ($help,$host,$port,$user,$password); + +my $result = GetOptions( + "debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help, + "host=s" => \$host, "port=i" => \$port, + "user=s" => \$user, "password=s" => \$password, + "numprocs=i" => \$numprocs, "numstmts=i" => \$numstmts, + ); + +if (defined($help) || (scalar(@ARGV) < 3)) { + print "Usage: $0 [options] db table column +Options: + --host=hostname --port=port + --user=username --password=string + --numprocs=4 --numstmts=100 +"; + exit ((scalar(@ARGV) < 3)? 1:0); } - -my $numprocs = $opt_numprocs || 4; -my $numstmts = $opt_numstmts || 100; - print STDERR "Running $numprocs threads, $numstmts inserts each\n"; -my $info = "dbname=immanis host=kelly user=root"; -#my $info = "dbname=replicate host=alicia user=nelio"; +my $db = $ARGV[0] || "master"; +my $table = $ARGV[1]; +my $col = $ARGV[2]; + +my $info = "dbname=$db"; +$info = "$info host=$host" if (defined($host)); +$info = "$info port=$port" if (defined($port)); +$info = "$info user=$user" if (defined($user)); +$info = "$info password=$password" if (defined($password)); my @pids = (); -for ($i=0; $i < $numprocs; $i++) { - if (($pid = fork()) == 0) { +my $q; + +for (my $i=0; $i < $numprocs; $i++) { + my $pid = fork(); + if (! defined($pid)) { + print "Can't fork...\n"; + } elsif ($pid == 0) { doInserts($i+1); exit; } elsif ($pid != undef) { @@ -36,7 +57,7 @@ } } -foreach $pid (@pids) { +foreach my $pid (@pids) { my $x = -1; do { sleep(1); @@ -48,10 +69,10 @@ ######################### sub doInserts { - ($pid) = @_; + my ($pid) = @_; print "<$pid> Running...\n"; - $conn = Pg::connectdb($info); + my $conn = Pg::connectdb($info); if ($conn->status != PGRES_CONNECTION_OK) { print "<$pid> Failed opening $info\n"; print "<$pid> Abort!\n"; @@ -66,9 +87,9 @@ } print "<$pid> Inserting $numstmts records...\n"; - $sql = "INSERT INTO test (value) VALUES"; - for ($i = 0; $i < $numstmts; $i++) { - $time = time; + my $sql = "INSERT INTO $table ($col) VALUES"; + for (my $i = 0; $i < $numstmts; $i++) { + my $time = time; $q = "$sql ('test_${pid}_${i}_$time')"; $result = $conn->exec($q); if ($result->resultStatus ne PGRES_COMMAND_OK) {