1 |
dpavlin |
1.2 |
#!/usr/bin/perl -w |
2 |
dpavlin |
1.1 |
|
3 |
dpavlin |
1.3 |
# original version by NĂ©lio Alves Pereira Filho |
4 |
|
|
# modification by Dobrica Pavlinusic |
5 |
|
|
|
6 |
dpavlin |
1.2 |
use strict; |
7 |
dpavlin |
1.1 |
use Pg; |
8 |
|
|
use Getopt::Long; |
9 |
|
|
use POSIX ":sys_wait_h"; |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
$| = 1; |
13 |
dpavlin |
1.2 |
my $control = 0; |
14 |
dpavlin |
1.1 |
|
15 |
dpavlin |
1.2 |
my ($numprocs,$numstmts) = (4,100); |
16 |
|
|
my ($debug,$verbose) = (0,0); |
17 |
|
|
my ($help,$host,$port,$user,$password); |
18 |
|
|
|
19 |
|
|
my $result = GetOptions( |
20 |
|
|
"debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help, |
21 |
|
|
"host=s" => \$host, "port=i" => \$port, |
22 |
|
|
"user=s" => \$user, "password=s" => \$password, |
23 |
|
|
"numprocs=i" => \$numprocs, "numstmts=i" => \$numstmts, |
24 |
|
|
); |
25 |
|
|
|
26 |
|
|
if (defined($help) || (scalar(@ARGV) < 3)) { |
27 |
|
|
print "Usage: $0 [options] db table column |
28 |
|
|
Options: |
29 |
|
|
--host=hostname --port=port |
30 |
|
|
--user=username --password=string |
31 |
|
|
--numprocs=4 --numstmts=100 |
32 |
|
|
"; |
33 |
|
|
exit ((scalar(@ARGV) < 3)? 1:0); |
34 |
dpavlin |
1.1 |
} |
35 |
|
|
|
36 |
|
|
print STDERR "Running $numprocs threads, $numstmts inserts each\n"; |
37 |
|
|
|
38 |
dpavlin |
1.2 |
my $db = $ARGV[0] || "master"; |
39 |
|
|
my $table = $ARGV[1]; |
40 |
|
|
my $col = $ARGV[2]; |
41 |
|
|
|
42 |
|
|
my $info = "dbname=$db"; |
43 |
|
|
$info = "$info host=$host" if (defined($host)); |
44 |
|
|
$info = "$info port=$port" if (defined($port)); |
45 |
|
|
$info = "$info user=$user" if (defined($user)); |
46 |
|
|
$info = "$info password=$password" if (defined($password)); |
47 |
dpavlin |
1.1 |
|
48 |
|
|
my @pids = (); |
49 |
dpavlin |
1.2 |
my $q; |
50 |
|
|
|
51 |
|
|
for (my $i=0; $i < $numprocs; $i++) { |
52 |
|
|
my $pid = fork(); |
53 |
|
|
if (! defined($pid)) { |
54 |
|
|
print "Can't fork...\n"; |
55 |
|
|
} elsif ($pid == 0) { |
56 |
dpavlin |
1.1 |
doInserts($i+1); |
57 |
|
|
exit; |
58 |
|
|
} elsif ($pid != undef) { |
59 |
|
|
push @pids, $pid; |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
dpavlin |
1.2 |
foreach my $pid (@pids) { |
64 |
dpavlin |
1.1 |
my $x = -1; |
65 |
|
|
do { |
66 |
|
|
sleep(1); |
67 |
|
|
$x = waitpid($pid, 0); |
68 |
|
|
} until $x == $pid; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
######################### |
73 |
|
|
|
74 |
|
|
sub doInserts { |
75 |
dpavlin |
1.2 |
my ($pid) = @_; |
76 |
dpavlin |
1.1 |
print "<$pid> Running...\n"; |
77 |
|
|
|
78 |
dpavlin |
1.2 |
my $conn = Pg::connectdb($info); |
79 |
dpavlin |
1.1 |
if ($conn->status != PGRES_CONNECTION_OK) { |
80 |
|
|
print "<$pid> Failed opening $info\n"; |
81 |
|
|
print "<$pid> Abort!\n"; |
82 |
|
|
last; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
$result = $conn->exec("BEGIN"); |
86 |
|
|
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
87 |
|
|
print "<$pid> Error in query '$q': ". $conn->errorMessage."\n"; |
88 |
|
|
print "<$pid> Abort!\n"; |
89 |
|
|
last; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
print "<$pid> Inserting $numstmts records...\n"; |
93 |
dpavlin |
1.2 |
my $sql = "INSERT INTO $table ($col) VALUES"; |
94 |
|
|
for (my $i = 0; $i < $numstmts; $i++) { |
95 |
|
|
my $time = time; |
96 |
dpavlin |
1.1 |
$q = "$sql ('test_${pid}_${i}_$time')"; |
97 |
|
|
$result = $conn->exec($q); |
98 |
|
|
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
99 |
|
|
print "<$pid> Error in query '$q': ". $conn->errorMessage."\n"; |
100 |
|
|
$conn->exec("ROLLBACK"); |
101 |
|
|
print "<$pid> Abort!\n"; |
102 |
|
|
last; |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
print "<$pid> done!\n"; |
106 |
|
|
|
107 |
|
|
$result = $conn->exec("COMMIT"); |
108 |
|
|
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
109 |
|
|
print "<$pid> Error in query '$q': ". $conn->errorMessage."\n"; |
110 |
|
|
$conn->exec("ROLLBACK"); |
111 |
|
|
print "<$pid> Abort!\n"; |
112 |
|
|
last; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
print "<$pid> Finished.\n"; |
116 |
|
|
$control++; |
117 |
|
|
} |