/[rserv]/misc/rserv_test.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /misc/rserv_test.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Wed Aug 6 17:38:26 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.2: +3 -0 lines
File MIME type: text/plain
added credits

1 #!/usr/bin/perl -w
2
3 # original version by NĂ©lio Alves Pereira Filho
4 # modification by Dobrica Pavlinusic
5
6 use strict;
7 use Pg;
8 use Getopt::Long;
9 use POSIX ":sys_wait_h";
10
11
12 $| = 1;
13 my $control = 0;
14
15 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 }
35
36 print STDERR "Running $numprocs threads, $numstmts inserts each\n";
37
38 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
48 my @pids = ();
49 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 doInserts($i+1);
57 exit;
58 } elsif ($pid != undef) {
59 push @pids, $pid;
60 }
61 }
62
63 foreach my $pid (@pids) {
64 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 my ($pid) = @_;
76 print "<$pid> Running...\n";
77
78 my $conn = Pg::connectdb($info);
79 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 my $sql = "INSERT INTO $table ($col) VALUES";
94 for (my $i = 0; $i < $numstmts; $i++) {
95 my $time = time;
96 $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 }

  ViewVC Help
Powered by ViewVC 1.1.26