~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

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, "message.mid = msp$sid.mid";
 
46
                        push @where, "msp$sid.sid = $sid";
 
47
                        $table{"message_section_map msp$sid"}=1;
 
48
                }
 
49
        }
 
50
} else {
 
51
        if ( $opt{r} ) {
 
52
                push @where, "message.sections && '{$opt{s}}'";
 
53
        } else {
 
54
                $table{message_section_map} = 1;
 
55
                push @where, "message.mid = message_section_map.mid";
 
56
                push @where, "message_section_map.sid in ($opt{s})";
 
57
        }
 
58
}
 
59
 
 
60
my $outf;
 
61
if ( $opt{c} ) {
 
62
        $outf = ( $opt{u} ) ? 'count( distinct message.mid )' : 'count( message.mid )';
 
63
} else {
 
64
        $outf = ( $opt{u} ) ? 'distinct( message.mid )' : 'message.mid';
 
65
}
 
66
my $sql = "select $outf from ".join(', ', keys %table)." where ".join(' AND ', @where).';';
 
67
 
 
68
if ( $opt{v} ) {
 
69
        print "$sql\n";
 
70
}
 
71
 
 
72
if ( $opt{e} ) {
 
73
        $dbi->do("explain $sql");
 
74
}
 
75
 
 
76
my $t0 = [gettimeofday];
 
77
my $count=0;
 
78
my $b=$opt{b};
 
79
$b||=1;
 
80
my @a;
 
81
foreach ( 1..$b ) {
 
82
        @a=exec_sql($dbi,$sql);
 
83
        $count=$#a;
 
84
}
 
85
my $elapsed = tv_interval ( $t0, [gettimeofday]);
 
86
if ( $opt{o} ) {
 
87
        foreach ( @a ) {
 
88
                print "$_->{mid}\t$_->{sections}\n";
 
89
        }
 
90
}
 
91
print sprintf("total: %.02f sec; number: %d; for one: %.03f sec; found %d docs\n", $elapsed, $b, $elapsed/$b, $count+1 );
 
92
$dbi -> disconnect;
 
93
 
 
94
sub exec_sql {
 
95
        my ($dbi, $sql, @keys) = @_;
 
96
        my $sth=$dbi->prepare($sql) || die;
 
97
        $sth->execute( @keys ) || die;
 
98
        my $r;
 
99
        my @row;
 
100
        while ( defined ( $r=$sth->fetchrow_hashref ) ) {
 
101
                push @row, $r;
 
102
        }
 
103
        $sth->finish;
 
104
        return @row;
 
105
}