1 |
#!/usr/bin/perl |
#!/usr/bin/perl -w |
|
|
|
2 |
|
|
3 |
|
use strict; |
4 |
use Pg; |
use Pg; |
5 |
use Getopt::Long; |
use Getopt::Long; |
6 |
use POSIX ":sys_wait_h"; |
use POSIX ":sys_wait_h"; |
7 |
|
|
8 |
|
|
9 |
$| = 1; |
$| = 1; |
10 |
$control = 0; |
my $control = 0; |
|
|
|
11 |
|
|
12 |
$result = GetOptions("numprocs=i", "numstmts=i", "help"); |
my ($numprocs,$numstmts) = (4,100); |
13 |
if (defined($opt_help)) { |
my ($debug,$verbose) = (0,0); |
14 |
print "Usage: $0 --numprocs=number_of_processes --numstmts=number_of_statements\n"; |
my ($help,$host,$port,$user,$password); |
15 |
print "\n"; |
|
16 |
exit(0); |
my $result = GetOptions( |
17 |
|
"debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help, |
18 |
|
"host=s" => \$host, "port=i" => \$port, |
19 |
|
"user=s" => \$user, "password=s" => \$password, |
20 |
|
"numprocs=i" => \$numprocs, "numstmts=i" => \$numstmts, |
21 |
|
); |
22 |
|
|
23 |
|
if (defined($help) || (scalar(@ARGV) < 3)) { |
24 |
|
print "Usage: $0 [options] db table column |
25 |
|
Options: |
26 |
|
--host=hostname --port=port |
27 |
|
--user=username --password=string |
28 |
|
--numprocs=4 --numstmts=100 |
29 |
|
"; |
30 |
|
exit ((scalar(@ARGV) < 3)? 1:0); |
31 |
} |
} |
32 |
|
|
|
|
|
|
my $numprocs = $opt_numprocs || 4; |
|
|
my $numstmts = $opt_numstmts || 100; |
|
|
|
|
33 |
print STDERR "Running $numprocs threads, $numstmts inserts each\n"; |
print STDERR "Running $numprocs threads, $numstmts inserts each\n"; |
34 |
|
|
35 |
my $info = "dbname=immanis host=kelly user=root"; |
my $db = $ARGV[0] || "master"; |
36 |
#my $info = "dbname=replicate host=alicia user=nelio"; |
my $table = $ARGV[1]; |
37 |
|
my $col = $ARGV[2]; |
38 |
|
|
39 |
|
my $info = "dbname=$db"; |
40 |
|
$info = "$info host=$host" if (defined($host)); |
41 |
|
$info = "$info port=$port" if (defined($port)); |
42 |
|
$info = "$info user=$user" if (defined($user)); |
43 |
|
$info = "$info password=$password" if (defined($password)); |
44 |
|
|
45 |
my @pids = (); |
my @pids = (); |
46 |
for ($i=0; $i < $numprocs; $i++) { |
my $q; |
47 |
if (($pid = fork()) == 0) { |
|
48 |
|
for (my $i=0; $i < $numprocs; $i++) { |
49 |
|
my $pid = fork(); |
50 |
|
if (! defined($pid)) { |
51 |
|
print "Can't fork...\n"; |
52 |
|
} elsif ($pid == 0) { |
53 |
doInserts($i+1); |
doInserts($i+1); |
54 |
exit; |
exit; |
55 |
} elsif ($pid != undef) { |
} elsif ($pid != undef) { |
57 |
} |
} |
58 |
} |
} |
59 |
|
|
60 |
foreach $pid (@pids) { |
foreach my $pid (@pids) { |
61 |
my $x = -1; |
my $x = -1; |
62 |
do { |
do { |
63 |
sleep(1); |
sleep(1); |
69 |
######################### |
######################### |
70 |
|
|
71 |
sub doInserts { |
sub doInserts { |
72 |
($pid) = @_; |
my ($pid) = @_; |
73 |
print "<$pid> Running...\n"; |
print "<$pid> Running...\n"; |
74 |
|
|
75 |
$conn = Pg::connectdb($info); |
my $conn = Pg::connectdb($info); |
76 |
if ($conn->status != PGRES_CONNECTION_OK) { |
if ($conn->status != PGRES_CONNECTION_OK) { |
77 |
print "<$pid> Failed opening $info\n"; |
print "<$pid> Failed opening $info\n"; |
78 |
print "<$pid> Abort!\n"; |
print "<$pid> Abort!\n"; |
87 |
} |
} |
88 |
|
|
89 |
print "<$pid> Inserting $numstmts records...\n"; |
print "<$pid> Inserting $numstmts records...\n"; |
90 |
$sql = "INSERT INTO test (value) VALUES"; |
my $sql = "INSERT INTO $table ($col) VALUES"; |
91 |
for ($i = 0; $i < $numstmts; $i++) { |
for (my $i = 0; $i < $numstmts; $i++) { |
92 |
$time = time; |
my $time = time; |
93 |
$q = "$sql ('test_${pid}_${i}_$time')"; |
$q = "$sql ('test_${pid}_${i}_$time')"; |
94 |
$result = $conn->exec($q); |
$result = $conn->exec($q); |
95 |
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
if ($result->resultStatus ne PGRES_COMMAND_OK) { |