summaryrefslogtreecommitdiffstats
path: root/contrib/intarray/bench/bench.pl
blob: 22127b787a5d40ddf9cb0d8fffb4e25497cfa527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/perl

# Copyright (c) 2021-2022, PostgreSQL Global Development Group

use strict;
use warnings;

# make sure we are in a sane environment.
use DBI();
use DBD::Pg();
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use Getopt::Std;

my %opt;
getopts('d:b:s:veorauc', \%opt);

if (!(scalar %opt && defined $opt{s}))
{
	print <<EOT;
Usage:
$0 -d DATABASE -s SECTIONS [-b NUMBER] [-v] [-e] [-o] [-r] [-a] [-u]
-d DATABASE	-DATABASE
-b NUMBER	-number of repeats
-s SECTIONS	-sections, format	sid1[,sid2[,sid3[...]]]]
-v		-verbose (show SQL)
-e		-show explain
-r		-use RD-tree index
-a		-AND section
-o		-show output
-u		-unique
-c		-count

EOT
	exit;
}

$opt{d} ||= '_int4';
my $dbi = DBI->connect('DBI:Pg:dbname=' . $opt{d});

my %table;
my @where;

$table{message} = 1;

if ($opt{a})
{
	if ($opt{r})
	{
		push @where, "message.sections @ '{$opt{s}}'";
	}
	else
	{
		foreach my $sid (split(/[,\s]+/, $opt{s}))
		{
			push @where, "message.mid = msp$sid.mid";
			push @where, "msp$sid.sid = $sid";
			$table{"message_section_map msp$sid"} = 1;
		}
	}
}
else
{
	if ($opt{r})
	{
		push @where, "message.sections && '{$opt{s}}'";
	}
	else
	{
		$table{message_section_map} = 1;
		push @where, "message.mid = message_section_map.mid";
		push @where, "message_section_map.sid in ($opt{s})";
	}
}

my $outf;
if ($opt{c})
{
	$outf =
	  ($opt{u}) ? 'count( distinct message.mid )' : 'count( message.mid )';
}
else
{
	$outf = ($opt{u}) ? 'distinct( message.mid )' : 'message.mid';
}
my $sql =
    "select $outf from "
  . join(', ', keys %table)
  . " where "
  . join(' AND ', @where) . ';';

if ($opt{v})
{
	print "$sql\n";
}

if ($opt{e})
{
	my @plan =
	  map { "$_->[0]\n" } @{ $dbi->selectall_arrayref("explain $sql") };
	print @plan;
}

my $t0    = [gettimeofday];
my $count = 0;
my $b     = $opt{b};
$b ||= 1;
my @a;
foreach (1 .. $b)
{
	@a = exec_sql($dbi, $sql);
	$count = $#a;
}
my $elapsed = tv_interval($t0, [gettimeofday]);
if ($opt{o})
{
	foreach (@a)
	{
		print "$_->{mid}\t$_->{sections}\n";
	}
}
print sprintf(
	"total: %.02f sec; number: %d; for one: %.03f sec; found %d docs\n",
	$elapsed, $b, $elapsed / $b,
	$count + 1);
$dbi->disconnect;

sub exec_sql
{
	my ($dbi, $sql, @keys) = @_;
	my $sth = $dbi->prepare($sql) || die;
	$sth->execute(@keys) || die;
	my $r;
	my @row;
	while (defined($r = $sth->fetchrow_hashref))
	{
		push @row, $r;
	}
	$sth->finish;
	return @row;
}