1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use Pg; |
5 |
use Getopt::Long; |
6 |
use POSIX ":sys_wait_h"; |
7 |
|
8 |
|
9 |
$| = 1; |
10 |
my $control = 0; |
11 |
|
12 |
my ($numprocs,$numstmts) = (4,100); |
13 |
my ($debug,$verbose) = (0,0); |
14 |
my ($help,$host,$port,$user,$password); |
15 |
|
16 |
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 |
|
33 |
print STDERR "Running $numprocs threads, $numstmts inserts each\n"; |
34 |
|
35 |
my $db = $ARGV[0] || "master"; |
36 |
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 = (); |
46 |
my $q; |
47 |
|
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); |
54 |
exit; |
55 |
} elsif ($pid != undef) { |
56 |
push @pids, $pid; |
57 |
} |
58 |
} |
59 |
|
60 |
foreach my $pid (@pids) { |
61 |
my $x = -1; |
62 |
do { |
63 |
sleep(1); |
64 |
$x = waitpid($pid, 0); |
65 |
} until $x == $pid; |
66 |
} |
67 |
|
68 |
|
69 |
######################### |
70 |
|
71 |
sub doInserts { |
72 |
my ($pid) = @_; |
73 |
print "<$pid> Running...\n"; |
74 |
|
75 |
my $conn = Pg::connectdb($info); |
76 |
if ($conn->status != PGRES_CONNECTION_OK) { |
77 |
print "<$pid> Failed opening $info\n"; |
78 |
print "<$pid> Abort!\n"; |
79 |
last; |
80 |
} |
81 |
|
82 |
$result = $conn->exec("BEGIN"); |
83 |
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
84 |
print "<$pid> Error in query '$q': ". $conn->errorMessage."\n"; |
85 |
print "<$pid> Abort!\n"; |
86 |
last; |
87 |
} |
88 |
|
89 |
print "<$pid> Inserting $numstmts records...\n"; |
90 |
my $sql = "INSERT INTO $table ($col) VALUES"; |
91 |
for (my $i = 0; $i < $numstmts; $i++) { |
92 |
my $time = time; |
93 |
$q = "$sql ('test_${pid}_${i}_$time')"; |
94 |
$result = $conn->exec($q); |
95 |
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
96 |
print "<$pid> Error in query '$q': ". $conn->errorMessage."\n"; |
97 |
$conn->exec("ROLLBACK"); |
98 |
print "<$pid> Abort!\n"; |
99 |
last; |
100 |
} |
101 |
} |
102 |
print "<$pid> done!\n"; |
103 |
|
104 |
$result = $conn->exec("COMMIT"); |
105 |
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
106 |
print "<$pid> Error in query '$q': ". $conn->errorMessage."\n"; |
107 |
$conn->exec("ROLLBACK"); |
108 |
print "<$pid> Abort!\n"; |
109 |
last; |
110 |
} |
111 |
|
112 |
print "<$pid> Finished.\n"; |
113 |
$control++; |
114 |
} |