MT#5757 bin/check.sh: add -g option to generate graph only if test fails

(cherry picked from commit 608c604a33250db6cc48c61930094047f00c7ce0)