Debian Codegraph @

contrib/intarray/bench/bench.pl

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