| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# |
| 5 |
# pgbench tests which do not need a server |
| 6 |
# |
| 7 |
|
| 8 |
use strict; |
| 9 |
use warnings FATAL => 'all'; |
| 10 |
|
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
# create a directory for scripts |
| 15 |
my $testname = $0; |
| 16 |
$testname =~ s,.*/,,; |
| 17 |
$testname =~ s/\.pl$//; |
| 18 |
|
| 19 |
my $testdir = "$PostgreSQL::Test::Utils::tmp_check/t_${testname}_stuff"; |
| 20 |
mkdir $testdir |
| 21 |
or BAIL_OUT("could not create test directory \"${testdir}\": $!"); |
| 22 |
|
| 23 |
# invoke pgbench |
| 24 |
sub pgbench |
| 25 |
{ |
| 26 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 27 |
|
| 28 |
my ($opts, $stat, $out, $err, $name) = @_; |
| 29 |
command_checks_all([ 'pgbench', split(/\s+/, $opts) ], |
| 30 |
$stat, $out, $err, $name); |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
# invoke pgbench with scripts |
| 35 |
sub pgbench_scripts |
| 36 |
{ |
| 37 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 38 |
|
| 39 |
my ($opts, $stat, $out, $err, $name, $files) = @_; |
| 40 |
my @cmd = ('pgbench', split /\s+/, $opts); |
| 41 |
if (defined $files) |
| 42 |
{ |
| 43 |
for my $fn (sort keys %$files) |
| 44 |
{ |
| 45 |
my $filename = $testdir . '/' . $fn; |
| 46 |
|
| 47 |
# cleanup file weight if any |
| 48 |
$filename =~ s/\@\d+$//; |
| 49 |
|
| 50 |
# cleanup from prior runs |
| 51 |
unlink $filename; |
| 52 |
append_to_file($filename, $$files{$fn}); |
| 53 |
push @cmd, '--file' => $filename; |
| 54 |
} |
| 55 |
} |
| 56 |
command_checks_all(\@cmd, $stat, $out, $err, $name); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
# |
| 61 |
# Option various errors |
| 62 |
# |
| 63 |
|
| 64 |
my @options = ( |
| 65 |
|
| 66 |
# name, options, stderr checks |
| 67 |
[ |
| 68 |
'bad option', |
| 69 |
'-h home -p 5432 -U calvin ---debug --bad-option', |
| 70 |
[qr{--help.*more information}] |
| 71 |
], |
| 72 |
[ |
| 73 |
'no file', |
| 74 |
'-f no-such-file', |
| 75 |
[qr{could not open file "no-such-file":}] |
| 76 |
], |
| 77 |
[ |
| 78 |
'no builtin', |
| 79 |
'-b no-such-builtin', |
| 80 |
[qr{no builtin script .* "no-such-builtin"}] |
| 81 |
], |
| 82 |
[ |
| 83 |
'invalid weight', |
| 84 |
'--builtin=select-only@one', |
| 85 |
[qr{invalid weight specification: \@one}] |
| 86 |
], |
| 87 |
[ |
| 88 |
'invalid weight', |
| 89 |
'-b select-only@-1', |
| 90 |
[qr{weight spec.* out of range .*: -1}] |
| 91 |
], |
| 92 |
[ 'too many scripts', '-S ' x 129, [qr{at most 128 SQL scripts}] ], |
| 93 |
[ |
| 94 |
'bad #clients', '-c three', |
| 95 |
[qr{invalid value "three" for option -c/--client}] |
| 96 |
], |
| 97 |
[ |
| 98 |
'bad #threads', '-j eleven', |
| 99 |
[qr{invalid value "eleven" for option -j/--jobs}] |
| 100 |
], |
| 101 |
[ |
| 102 |
'bad scale', '-i -s two', |
| 103 |
[qr{invalid value "two" for option -s/--scale}] |
| 104 |
], |
| 105 |
[ |
| 106 |
'invalid #transactions', |
| 107 |
'-t zil', [qr{invalid value "zil" for option -t/--transactions}] |
| 108 |
], |
| 109 |
[ |
| 110 |
'invalid duration', |
| 111 |
'-T ten', [qr{invalid value "ten" for option -T/--time}] |
| 112 |
], |
| 113 |
[ |
| 114 |
'-t XOR -T', |
| 115 |
'-N -l --aggregate-interval=5 --log-prefix=notused -t 1000 -T 1', |
| 116 |
[qr{specify either }] |
| 117 |
], |
| 118 |
[ |
| 119 |
'-T XOR -t', |
| 120 |
'-P 1 --progress-timestamp -l --sampling-rate=0.001 -T 10 -t 1000', |
| 121 |
[qr{specify either }] |
| 122 |
], |
| 123 |
[ 'bad variable', '--define foobla', [qr{invalid variable definition}] ], |
| 124 |
[ 'invalid fillfactor', '-F 1', [qr{-F/--fillfactor must be in range}] ], |
| 125 |
[ 'invalid query mode', '-M no-such-mode', [qr{invalid query mode}] ], |
| 126 |
[ |
| 127 |
'invalid progress', '--progress=0', |
| 128 |
[qr{-P/--progress must be in range}] |
| 129 |
], |
| 130 |
[ 'invalid rate', '--rate=0.0', [qr{invalid rate limit}] ], |
| 131 |
[ 'invalid latency', '--latency-limit=0.0', [qr{invalid latency limit}] ], |
| 132 |
[ |
| 133 |
'invalid sampling rate', '--sampling-rate=0', |
| 134 |
[qr{invalid sampling rate}] |
| 135 |
], |
| 136 |
[ |
| 137 |
'invalid aggregate interval', |
| 138 |
'--aggregate-interval=-3', |
| 139 |
[qr{--aggregate-interval must be in range}] |
| 140 |
], |
| 141 |
[ |
| 142 |
'weight zero', |
| 143 |
'-b se@0 -b si@0 -b tpcb@0', |
| 144 |
[qr{weight must not be zero}] |
| 145 |
], |
| 146 |
[ 'init vs run', '-i -S', [qr{cannot be used in initialization}] ], |
| 147 |
[ 'run vs init', '-S -F 90', [qr{cannot be used in benchmarking}] ], |
| 148 |
[ 'ambiguous builtin', '-b s', [qr{ambiguous}] ], |
| 149 |
[ |
| 150 |
'--progress-timestamp => --progress', '--progress-timestamp', |
| 151 |
[qr{allowed only under}] |
| 152 |
], |
| 153 |
[ |
| 154 |
'-I without init option', |
| 155 |
'-I dtg', |
| 156 |
[qr{cannot be used in benchmarking mode}] |
| 157 |
], |
| 158 |
[ |
| 159 |
'invalid init step', |
| 160 |
'-i -I dta', |
| 161 |
[ |
| 162 |
qr{unrecognized initialization step}, |
| 163 |
qr{Allowed step characters are} |
| 164 |
] |
| 165 |
], |
| 166 |
[ |
| 167 |
'bad random seed', |
| 168 |
'--random-seed=one', |
| 169 |
[ |
| 170 |
qr{unrecognized random seed option "one"}, |
| 171 |
qr{Expecting an unsigned integer, "time" or "rand"}, |
| 172 |
qr{error while setting random seed from --random-seed option} |
| 173 |
] |
| 174 |
], |
| 175 |
[ |
| 176 |
'bad partition method', |
| 177 |
'-i --partition-method=BAD', |
| 178 |
[ qr{"range"}, qr{"hash"}, qr{"BAD"} ] |
| 179 |
], |
| 180 |
[ |
| 181 |
'bad partition number', |
| 182 |
'-i --partitions -1', |
| 183 |
[qr{--partitions must be in range}] |
| 184 |
], |
| 185 |
[ |
| 186 |
'partition method without partitioning', |
| 187 |
'-i --partition-method=hash', |
| 188 |
[qr{partition-method requires greater than zero --partitions}] |
| 189 |
], |
| 190 |
[ |
| 191 |
'bad maximum number of tries', |
| 192 |
'--max-tries -10', |
| 193 |
[qr{invalid number of maximum tries: "-10"}] |
| 194 |
], |
| 195 |
[ |
| 196 |
'an infinite number of tries', |
| 197 |
'--max-tries 0', |
| 198 |
[ |
| 199 |
qr{an unlimited number of transaction tries can only be used with --latency-limit or a duration} |
| 200 |
] |
| 201 |
], |
| 202 |
|
| 203 |
# logging sub-options |
| 204 |
[ |
| 205 |
'sampling => log', '--sampling-rate=0.01', |
| 206 |
[qr{log sampling .* only when}] |
| 207 |
], |
| 208 |
[ |
| 209 |
'sampling XOR aggregate', |
| 210 |
'-l --sampling-rate=0.1 --aggregate-interval=3', |
| 211 |
[qr{sampling .* aggregation .* cannot be used at the same time}] |
| 212 |
], |
| 213 |
[ |
| 214 |
'aggregate => log', '--aggregate-interval=3', |
| 215 |
[qr{aggregation .* only when}] |
| 216 |
], |
| 217 |
[ 'log-prefix => log', '--log-prefix=x', [qr{prefix .* only when}] ], |
| 218 |
[ |
| 219 |
'duration & aggregation', |
| 220 |
'-l -T 1 --aggregate-interval=3', |
| 221 |
[qr{aggr.* not be higher}] |
| 222 |
], |
| 223 |
[ |
| 224 |
'duration % aggregation', |
| 225 |
'-l -T 5 --aggregate-interval=3', |
| 226 |
[qr{multiple}] |
| 227 |
],); |
| 228 |
|
| 229 |
for my $o (@options) |
| 230 |
{ |
| 231 |
my ($name, $opts, $err_checks) = @$o; |
| 232 |
pgbench($opts, 1, [qr{^$}], $err_checks, |
| 233 |
'pgbench option error: ' . $name); |
| 234 |
} |
| 235 |
|
| 236 |
# Help |
| 237 |
pgbench( |
| 238 |
'--help', 0, |
| 239 |
[ |
| 240 |
qr{benchmarking tool for PostgreSQL}, |
| 241 |
qr{Usage}, |
| 242 |
qr{Initialization options:}, |
| 243 |
qr{Common options:}, |
| 244 |
qr{Report bugs to} |
| 245 |
], |
| 246 |
[qr{^$}], |
| 247 |
'pgbench help'); |
| 248 |
|
| 249 |
# Version |
| 250 |
pgbench('-V', 0, [qr{^pgbench .PostgreSQL. }], [qr{^$}], 'pgbench version'); |
| 251 |
|
| 252 |
# list of builtins |
| 253 |
pgbench( |
| 254 |
'-b list', |
| 255 |
0, |
| 256 |
[qr{^$}], |
| 257 |
[ |
| 258 |
qr{Available builtin scripts:}, qr{tpcb-like}, |
| 259 |
qr{simple-update}, qr{select-only} |
| 260 |
], |
| 261 |
'pgbench builtin list'); |
| 262 |
|
| 263 |
# builtin listing |
| 264 |
pgbench( |
| 265 |
'--show-script se', |
| 266 |
0, |
| 267 |
[qr{^$}], |
| 268 |
[ |
| 269 |
qr{select-only: }, qr{SELECT abalance FROM pgbench_accounts WHERE}, |
| 270 |
qr{(?!UPDATE)}, qr{(?!INSERT)} |
| 271 |
], |
| 272 |
'pgbench builtin listing'); |
| 273 |
|
| 274 |
my @script_tests = ( |
| 275 |
|
| 276 |
# name, err, { file => contents } |
| 277 |
[ |
| 278 |
'missing endif', |
| 279 |
[qr{\\if without matching \\endif}], |
| 280 |
{ 'if-noendif.sql' => '\if 1' } |
| 281 |
], |
| 282 |
[ |
| 283 |
'missing if on elif', |
| 284 |
[qr{\\elif without matching \\if}], |
| 285 |
{ 'elif-noif.sql' => '\elif 1' } |
| 286 |
], |
| 287 |
[ |
| 288 |
'missing if on else', |
| 289 |
[qr{\\else without matching \\if}], |
| 290 |
{ 'else-noif.sql' => '\else' } |
| 291 |
], |
| 292 |
[ |
| 293 |
'missing if on endif', |
| 294 |
[qr{\\endif without matching \\if}], |
| 295 |
{ 'endif-noif.sql' => '\endif' } |
| 296 |
], |
| 297 |
[ |
| 298 |
'elif after else', |
| 299 |
[qr{\\elif after \\else}], |
| 300 |
{ 'else-elif.sql' => "\\if 1\n\\else\n\\elif 0\n\\endif" } |
| 301 |
], |
| 302 |
[ |
| 303 |
'else after else', |
| 304 |
[qr{\\else after \\else}], |
| 305 |
{ 'else-else.sql' => "\\if 1\n\\else\n\\else\n\\endif" } |
| 306 |
], |
| 307 |
[ |
| 308 |
'if syntax error', |
| 309 |
[qr{syntax error in command "if"}], |
| 310 |
{ 'if-bad.sql' => "\\if\n\\endif\n" } |
| 311 |
], |
| 312 |
[ |
| 313 |
'elif syntax error', |
| 314 |
[qr{syntax error in command "elif"}], |
| 315 |
{ 'elif-bad.sql' => "\\if 0\n\\elif +\n\\endif\n" } |
| 316 |
], |
| 317 |
[ |
| 318 |
'else syntax error', |
| 319 |
[qr{unexpected argument in command "else"}], |
| 320 |
{ 'else-bad.sql' => "\\if 0\n\\else BAD\n\\endif\n" } |
| 321 |
], |
| 322 |
[ |
| 323 |
'endif syntax error', |
| 324 |
[qr{unexpected argument in command "endif"}], |
| 325 |
{ 'endif-bad.sql' => "\\if 0\n\\endif BAD\n" } |
| 326 |
], |
| 327 |
[ |
| 328 |
'not enough arguments for least', |
| 329 |
[qr{at least one argument expected \(least\)}], |
| 330 |
{ 'bad-least.sql' => "\\set i least()\n" } |
| 331 |
], |
| 332 |
[ |
| 333 |
'not enough arguments for greatest', |
| 334 |
[qr{at least one argument expected \(greatest\)}], |
| 335 |
{ 'bad-greatest.sql' => "\\set i greatest()\n" } |
| 336 |
], |
| 337 |
[ |
| 338 |
'not enough arguments for hash', |
| 339 |
[qr{unexpected number of arguments \(hash\)}], |
| 340 |
{ 'bad-hash-1.sql' => "\\set i hash()\n" } |
| 341 |
], |
| 342 |
[ |
| 343 |
'too many arguments for hash', |
| 344 |
[qr{unexpected number of arguments \(hash\)}], |
| 345 |
{ 'bad-hash-2.sql' => "\\set i hash(1,2,3)\n" } |
| 346 |
], |
| 347 |
# overflow |
| 348 |
[ |
| 349 |
'bigint overflow 1', |
| 350 |
[qr{bigint constant overflow}], |
| 351 |
{ 'overflow-1.sql' => "\\set i 100000000000000000000\n" } |
| 352 |
], |
| 353 |
[ |
| 354 |
'double overflow 2', |
| 355 |
[qr{double constant overflow}], |
| 356 |
{ 'overflow-2.sql' => "\\set d 1.0E309\n" } |
| 357 |
], |
| 358 |
[ |
| 359 |
'double overflow 3', |
| 360 |
[qr{double constant overflow}], |
| 361 |
{ 'overflow-3.sql' => "\\set d .1E310\n" } |
| 362 |
], |
| 363 |
[ |
| 364 |
'set i', |
| 365 |
[ qr{set i 1 }, qr{\^ error found here} ], |
| 366 |
{ 'set_i_op' => "\\set i 1 +\n" } |
| 367 |
], |
| 368 |
[ |
| 369 |
'not enough arguments to permute', |
| 370 |
[qr{unexpected number of arguments \(permute\)}], |
| 371 |
{ 'bad-permute-1.sql' => "\\set i permute(1)\n" } |
| 372 |
], |
| 373 |
[ |
| 374 |
'too many arguments to permute', |
| 375 |
[qr{unexpected number of arguments \(permute\)}], |
| 376 |
{ 'bad-permute-2.sql' => "\\set i permute(1, 2, 3, 4)\n" } |
| 377 |
],); |
| 378 |
|
| 379 |
for my $t (@script_tests) |
| 380 |
{ |
| 381 |
my ($name, $err, $files) = @$t; |
| 382 |
pgbench_scripts('', 1, [qr{^$}], $err, 'pgbench option error: ' . $name, |
| 383 |
$files); |
| 384 |
} |
| 385 |
|
| 386 |
done_testing(); |