Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5b75a4f

Browse files
committed
pgbench: Report errors during run better
When an error occurs during a benchmark run, exit with a nonzero exit code and write a message at the end. Previously, it would just print the error message when it happened but then proceed to print the run summary normally and exit with status 0. To still allow distinguishing setup from run-time errors, we use exit status 2 for the new state, whereas existing errors during pgbench initialization use exit status 1. Reviewed-by: Fabien COELHO <coelho@cri.ensmp.fr>
1 parent 35584fd commit 5b75a4f

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

doc/src/sgml/ref/pgbench.sgml

+12
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,18 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
804804
</refsect2>
805805
</refsect1>
806806

807+
<refsect1>
808+
<title>Exit Status</title>
809+
810+
<para>
811+
A successful run will exit with status 0. Exit status 1 indicates static
812+
problems such as invalid command-line options. Errors during the run such
813+
as database errors or problems in the script will result in exit status 2.
814+
In the latter case, <application>pgbench</application> will print partial
815+
results.
816+
</para>
817+
</refsect1>
818+
807819
<refsect1>
808820
<title>Notes</title>
809821

src/bin/pgbench/pgbench.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -4931,6 +4931,8 @@ main(int argc, char **argv)
49314931
PGresult *res;
49324932
char *env;
49334933

4934+
int exit_code = 0;
4935+
49344936
progname = get_progname(argv[0]);
49354937

49364938
if (argc > 1)
@@ -5675,6 +5677,10 @@ main(int argc, char **argv)
56755677
(void) threadRun(thread);
56765678
#endif /* ENABLE_THREAD_SAFETY */
56775679

5680+
for (int j = 0; j < thread->nstate; j++)
5681+
if (thread->state[j].state == CSTATE_ABORTED)
5682+
exit_code = 2;
5683+
56785684
/* aggregate thread level stats */
56795685
mergeSimpleStats(&stats.latency, &thread->stats.latency);
56805686
mergeSimpleStats(&stats.lag, &thread->stats.lag);
@@ -5699,7 +5705,10 @@ main(int argc, char **argv)
56995705
INSTR_TIME_SUBTRACT(total_time, start_time);
57005706
printResults(threads, &stats, total_time, conn_total_time, latency_late);
57015707

5702-
return 0;
5708+
if (exit_code != 0)
5709+
fprintf(stderr, "Run was aborted; the above results are incomplete.\n");
5710+
5711+
return exit_code;
57035712
}
57045713

57055714
static void *

src/bin/pgbench/t/001_pgbench_with_server.pl

+29-28
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ sub pgbench
537537
# SQL
538538
[
539539
'sql syntax error',
540-
0,
540+
2,
541541
[
542542
qr{ERROR: syntax error},
543543
qr{prepared statement .* does not exist}
@@ -556,11 +556,11 @@ sub pgbench
556556

557557
# SHELL
558558
[
559-
'shell bad command', 0,
559+
'shell bad command', 2,
560560
[qr{\(shell\) .* meta-command failed}], q{\shell no-such-command}
561561
],
562562
[
563-
'shell undefined variable', 0,
563+
'shell undefined variable', 2,
564564
[qr{undefined variable ":nosuchvariable"}],
565565
q{-- undefined variable in shell
566566
\shell echo ::foo :nosuchvariable
@@ -600,75 +600,75 @@ sub pgbench
600600
[qr{unexpected function name}], q{\set i noSuchFunction()}
601601
],
602602
[
603-
'set invalid variable name', 0,
603+
'set invalid variable name', 2,
604604
[qr{invalid variable name}], q{\set . 1}
605605
],
606606
[
607-
'set division by zero', 0,
607+
'set division by zero', 2,
608608
[qr{division by zero}], q{\set i 1/0}
609609
],
610610
[ 'set undefined variable',
611-
0,
611+
2,
612612
[qr{undefined variable "nosuchvariable"}],
613613
q{\set i :nosuchvariable}
614614
],
615615
[ 'set unexpected char', 1, [qr{unexpected character .;.}], q{\set i ;} ],
616616
[
617617
'set too many args',
618-
0,
618+
2,
619619
[qr{too many function arguments}],
620620
q{\set i least(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)}
621621
],
622622
[
623-
'set empty random range', 0,
623+
'set empty random range', 2,
624624
[qr{empty range given to random}], q{\set i random(5,3)}
625625
],
626626
[
627627
'set random range too large',
628-
0,
628+
2,
629629
[qr{random range is too large}],
630630
q{\set i random(:minint, :maxint)}
631631
],
632632
[
633633
'set gaussian param too small',
634-
0,
634+
2,
635635
[qr{gaussian param.* at least 2}],
636636
q{\set i random_gaussian(0, 10, 1.0)}
637637
],
638638
[
639639
'set exponential param greater 0',
640-
0,
640+
2,
641641
[qr{exponential parameter must be greater }],
642642
q{\set i random_exponential(0, 10, 0.0)}
643643
],
644644
[
645645
'set zipfian param to 1',
646-
0,
646+
2,
647647
[qr{zipfian parameter must be in range \(0, 1\) U \(1, \d+\]}],
648648
q{\set i random_zipfian(0, 10, 1)}
649649
],
650650
[
651651
'set zipfian param too large',
652-
0,
652+
2,
653653
[qr{zipfian parameter must be in range \(0, 1\) U \(1, \d+\]}],
654654
q{\set i random_zipfian(0, 10, 1000000)}
655655
],
656656
[
657-
'set non numeric value', 0,
657+
'set non numeric value', 2,
658658
[qr{malformed variable "foo" value: "bla"}], q{\set i :foo + 1}
659659
],
660660
[ 'set no expression', 1, [qr{syntax error}], q{\set i} ],
661661
[ 'set missing argument', 1, [qr{missing argument}i], q{\set} ],
662662
[
663-
'set not a bool', 0,
663+
'set not a bool', 2,
664664
[qr{cannot coerce double to boolean}], q{\set b NOT 0.0}
665665
],
666666
[
667-
'set not an int', 0,
667+
'set not an int', 2,
668668
[qr{cannot coerce boolean to int}], q{\set i TRUE + 2}
669669
],
670670
[
671-
'set not a double', 0,
671+
'set not a double', 2,
672672
[qr{cannot coerce boolean to double}], q{\set d ln(TRUE)}
673673
],
674674
[
@@ -678,7 +678,7 @@ sub pgbench
678678
q{\set i CASE TRUE THEN 1 ELSE 0 END}
679679
],
680680
[
681-
'set random error', 0,
681+
'set random error', 2,
682682
[qr{cannot coerce boolean to int}], q{\set b random(FALSE, TRUE)}
683683
],
684684
[
@@ -691,31 +691,31 @@ sub pgbench
691691
],
692692

693693
# SET: ARITHMETIC OVERFLOW DETECTION
694-
[ 'set double to int overflow', 0,
694+
[ 'set double to int overflow', 2,
695695
[ qr{double to int overflow for 100} ], q{\set i int(1E32)} ],
696-
[ 'set bigint add overflow', 0,
696+
[ 'set bigint add overflow', 2,
697697
[ qr{int add out} ], q{\set i (1<<62) + (1<<62)} ],
698-
[ 'set bigint sub overflow', 0,
698+
[ 'set bigint sub overflow', 2,
699699
[ qr{int sub out} ], q{\set i 0 - (1<<62) - (1<<62) - (1<<62)} ],
700-
[ 'set bigint mul overflow', 0,
700+
[ 'set bigint mul overflow', 2,
701701
[ qr{int mul out} ], q{\set i 2 * (1<<62)} ],
702-
[ 'set bigint div out of range', 0,
702+
[ 'set bigint div out of range', 2,
703703
[ qr{bigint div out of range} ], q{\set i :minint / -1} ],
704704

705705
# SETSHELL
706706
[
707-
'setshell not an int', 0,
707+
'setshell not an int', 2,
708708
[qr{command must return an integer}], q{\setshell i echo -n one}
709709
],
710710
[ 'setshell missing arg', 1, [qr{missing argument }], q{\setshell var} ],
711711
[
712-
'setshell no such command', 0,
712+
'setshell no such command', 2,
713713
[qr{could not read result }], q{\setshell var no-such-command}
714714
],
715715

716716
# SLEEP
717717
[
718-
'sleep undefined variable', 0,
718+
'sleep undefined variable', 2,
719719
[qr{sleep: undefined variable}], q{\sleep :nosuchvariable}
720720
],
721721
[
@@ -738,21 +738,22 @@ sub pgbench
738738
],
739739
[ 'misc empty script', 1, [qr{empty command list for script}], q{} ],
740740
[
741-
'bad boolean', 0,
741+
'bad boolean', 2,
742742
[qr{malformed variable.*trueXXX}], q{\set b :badtrue or true}
743743
],);
744744

745745

746746
for my $e (@errors)
747747
{
748748
my ($name, $status, $re, $script) = @$e;
749+
$status != 0 or die "invalid expected status for test \"$name\"";
749750
my $n = '001_pgbench_error_' . $name;
750751
$n =~ s/ /_/g;
751752
pgbench(
752753
'-n -t 1 -M prepared -Dfoo=bla -Dnull=null -Dtrue=true -Done=1 -Dzero=0.0 ' .
753754
'-Dbadtrue=trueXXX -Dmaxint=9223372036854775807 -Dminint=-9223372036854775808',
754755
$status,
755-
[ $status ? qr{^$} : qr{processed: 0/1} ],
756+
[ $status == 1 ? qr{^$} : qr{processed: 0/1} ],
756757
$re,
757758
'pgbench script error: ' . $name,
758759
{ $n => $script });

0 commit comments

Comments
 (0)