git » libjio » commit 3e389f6

jiostress: Use optparse to parse command line options

author Alberto Bertogli
2009-09-16 19:28:26 UTC
committer Alberto Bertogli
2009-09-16 19:30:24 UTC
parent 1e48fcaad1ce534758cb68198da30f8c0e0907bf

jiostress: Use optparse to parse command line options

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

tests/stress/jiostress +27 -33

diff --git a/tests/stress/jiostress b/tests/stress/jiostress
index 02f65cf..b186312 100755
--- a/tests/stress/jiostress
+++ b/tests/stress/jiostress
@@ -11,6 +11,7 @@ import sys
 import os
 import random
 import traceback
+from optparse import OptionParser
 import libjio
 
 try:
@@ -426,51 +427,44 @@ class Stresser:
 # Main
 #
 
-def usage():
-	print("""
-Use: jiostress <file name> <file size in Mb> [<number of operations>]
-	[--fi] [--as]
-
-If the number of operations is not provided, the default (500) will be
-used.
-
-If the "--fi" option is passed, the test will perform fault injection. This
-option conflicts with "--as".
-
-If the "--as" option is passed, lingering transactions will be used, along
-with the automatic syncing thread. This option conflicts with "--fi".
-""")
-
 
 def main():
+	usage = "Use: %prog [options] <file name> <file size in Mb>"
+	parser = OptionParser(usage = usage)
+	parser.add_option("-n", "--nops", dest = "nops", type = "int",
+		default = 500,
+		help = "number of operations (defaults to %default)")
+	parser.add_option("-f", "--fi", dest = "use_fi",
+		action = "store_true", default = False,
+		help = "use fault injection (conflicts with --as)")
+	parser.add_option("-a", "--as", dest = "use_as",
+		action = "store_true", default = False,
+		help = "use J_LINGER + autosync (conflicts with --fi)")
+
+	options, args = parser.parse_args()
+
+	if len(args) != 2:
+		parser.print_help()
+		sys.exit(1)
+
+	fname = args[0]
 	try:
-		fname = sys.argv[1]
-		fsize = int(sys.argv[2]) * 1024 * 1024
-		nops = 500
-		if len(sys.argv) >= 4 and sys.argv[3].isnumeric():
-			nops = int(sys.argv[3])
-
-		use_fi = False
-		if '--fi' in sys.argv:
-			use_fi = True
-
-		use_as = False
-		if '--as' in sys.argv:
-			use_as = True
-	except:
-		usage()
+		fsize = int(args[1]) * 1024 * 1024
+	except ValueError:
+		print("Error: the size of the file must be numeric")
 		sys.exit(1)
 
-	if use_fi and use_as:
+	if options.use_fi and options.use_as:
 		print("Error: --fi and --as cannot be used together")
 		sys.exit(1)
 
-	s = Stresser(fname, fsize, nops, use_fi, use_as)
+	s = Stresser(fname, fsize, options.nops, options.use_fi,
+			options.use_as)
 	print("Running stress test")
 	nfailures = s.run()
 	del s
 	print("Stress test completed")
-	print("  %d operations" % nops)
+	print("  %d operations" % options.nops)
 	print("  %d simulated failures" % nfailures)
 
 	r = jfsck(fname)