~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/intarray/bench/bench.pl

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl 
 
2
 
 
3
use strict;
 
4
# make sure we are in a sane environment.
 
5
use DBI();
 
6
use DBD::Pg();
 
7
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
 
8
use Getopt::Std;
 
9
 
 
10
my %opt;
 
11
getopts('d:b:s:veorauc', \%opt);
 
12
 
 
13
if ( !( scalar %opt && defined $opt{s} ) ) {
 
14
        print <<EOT;
 
15
Usage:
 
16
$0 -d DATABASE -s SECTIONS [-b NUMBER] [-v] [-e] [-o] [-r] [-a] [-u]
 
17
-d DATABASE     -DATABASE
 
18
-b NUMBER       -number of repeats
 
19
-s SECTIONS     -sections, format       sid1[,sid2[,sid3[...]]]]
 
20
-v              -verbose (show SQL)
 
21
-e              -show explain
 
22
-r              -use RD-tree index
 
23
-a              -AND section
 
24
-o              -show output
 
25
-u              -unique
 
26
-c              -count
 
27
 
 
28
EOT
 
29
        exit;
 
30
}
 
31
 
 
32
$opt{d} ||= '_int4';
 
33
my $dbi=DBI->connect('DBI:Pg:dbname='.$opt{d});
 
34
 
 
35
my %table;
 
36
my @where;
 
37
 
 
38
$table{message}=1;
 
39
 
 
40
if ( $opt{a} ) {
 
41
        if ( $opt{r} ) {
 
42
                push @where, "message.sections @ '{$opt{s}}'";
 
43
        } else {
 
44
                foreach my $sid ( split(/[,\s]+/, $opt{s} )) {
 
45
                        push @where, "EXISTS ( select  message_section_map.mid from message_section_map where message.mid=message_section_map.mid and message_section_map.sid = $sid )";
 
46
                }
 
47
        }
 
48
} else {
 
49
        if ( $opt{r} ) {
 
50
                push @where, "message.sections && '{$opt{s}}'";
 
51
        } else {
 
52
                $table{message_section_map} = 1;
 
53
                push @where, "message.mid = message_section_map.mid";
 
54
                push @where, "message_section_map.sid in ($opt{s})";
 
55
        }
 
56
}
 
57
 
 
58
my $outf;
 
59
if ( $opt{c} ) {
 
60
        $outf = ( $opt{u} ) ? 'count( distinct message.mid )' : 'count( message.mid )';
 
61
} else {
 
62
        $outf = ( $opt{u} ) ? 'distinct( message.mid )' : 'message.mid';
 
63
}
 
64
my $sql = "select $outf from ".join(', ', keys %table)." where ".join(' AND ', @where).';';
 
65
 
 
66
if ( $opt{v} ) {
 
67
        print "$sql\n";
 
68
}
 
69
 
 
70
if ( $opt{e} ) {
 
71
        $dbi->do("explain $sql");
 
72
}
 
73
 
 
74
my $t0 = [gettimeofday];
 
75
my $count=0;
 
76
my $b=$opt{b};
 
77
$b||=1;
 
78
my @a;
 
79
foreach ( 1..$b ) {
 
80
        @a=exec_sql($dbi,$sql);
 
81
        $count=$#a;
 
82
}
 
83
my $elapsed = tv_interval ( $t0, [gettimeofday]);
 
84
if ( $opt{o} ) {
 
85
        foreach ( @a ) {
 
86
                print "$_->{mid}\t$_->{sections}\n";
 
87
        }
 
88
 
89
print sprintf("total: %.02f sec; number: %d; for one: %.03f sec; found %d docs\n", $elapsed, $b, $elapsed/$b, $count+1 );
 
90
$dbi -> disconnect;
 
91
 
 
92
sub exec_sql {
 
93
        my ($dbi, $sql, @keys) = @_;
 
94
        my $sth=$dbi->prepare($sql) || die;
 
95
        $sth->execute( @keys ) || die; 
 
96
        my $r;  
 
97
        my @row;
 
98
        while ( defined ( $r=$sth->fetchrow_hashref ) ) {
 
99
                push @row, $r;
 
100
        }               
 
101
        $sth->finish;   
 
102
        return @row;
 
103
}
 
104