From 6eb9c5a5657d1fe77b55cc261450f3538d35a94d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 14:19:15 +0200 Subject: Adding upstream version 13.4. Signed-off-by: Daniel Baumann --- contrib/intarray/bench/bench.pl | 138 ++++++++++++++++++++++++++++++++++ contrib/intarray/bench/create_test.pl | 89 ++++++++++++++++++++++ 2 files changed, 227 insertions(+) create mode 100755 contrib/intarray/bench/bench.pl create mode 100755 contrib/intarray/bench/create_test.pl (limited to 'contrib/intarray/bench') diff --git a/contrib/intarray/bench/bench.pl b/contrib/intarray/bench/bench.pl new file mode 100755 index 0000000..daf3feb --- /dev/null +++ b/contrib/intarray/bench/bench.pl @@ -0,0 +1,138 @@ +#!/usr/bin/perl + +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 <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; +} diff --git a/contrib/intarray/bench/create_test.pl b/contrib/intarray/bench/create_test.pl new file mode 100755 index 0000000..3f2a6e4 --- /dev/null +++ b/contrib/intarray/bench/create_test.pl @@ -0,0 +1,89 @@ +#!/usr/bin/perl + +# contrib/intarray/bench/create_test.pl + +use strict; +use warnings; + +print <', "message.tmp") || die; +open(my $map, '>', "message_section_map.tmp") || die; + +srand(1); + +#foreach my $i ( 1..1778 ) { +#foreach my $i ( 1..3443 ) { +#foreach my $i ( 1..5000 ) { +#foreach my $i ( 1..29362 ) { +#foreach my $i ( 1..33331 ) { +#foreach my $i ( 1..83268 ) { +foreach my $i (1 .. 200000) +{ + my @sect; + if (rand() < 0.7) + { + $sect[0] = int((rand()**4) * 100); + } + else + { + my %hash; + @sect = + grep { $hash{$_}++; $hash{$_} <= 1 } + map { int((rand()**4) * 100) } 0 .. (int(rand() * 5)); + } + if ($#sect < 0 || rand() < 0.1) + { + print $msg "$i\t\\N\n"; + } + else + { + print $msg "$i\t{" . join(',', @sect) . "}\n"; + map { print $map "$i\t$_\n" } @sect; + } +} +close $map; +close $msg; + +copytable('message'); +copytable('message_section_map'); + +print <) { print; } + close $fff; + print "\\.\n"; + return; +} -- cgit v1.2.3