run-tests.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env python
  2. import argparse
  3. import ConfigParser
  4. import sys
  5. import os
  6. import multiprocessing
  7. import subprocess
  8. from pprint import pprint
  9. from benchmark.benchmarker import Benchmarker
  10. from setup.linux.unbuffered import Unbuffered
  11. ###################################################################################################
  12. # Main
  13. ###################################################################################################
  14. def main(argv=None):
  15. # Do argv default this way, as doing it in the functional declaration sets it at compile time
  16. if argv is None:
  17. argv = sys.argv
  18. # Enable unbuffered output so messages will appear in the proper order with subprocess output.
  19. sys.stdout=Unbuffered(sys.stdout)
  20. # Ensure the current directory (which should be the benchmark home directory) is in the path so that the tests can be imported.
  21. sys.path.append('.')
  22. # Ensure toolset/setup/linux is in the path so that the tests can "import setup_util".
  23. sys.path.append('toolset/setup/linux')
  24. conf_parser = argparse.ArgumentParser(
  25. description=__doc__,
  26. formatter_class=argparse.RawDescriptionHelpFormatter,
  27. add_help=False)
  28. conf_parser.add_argument('--conf_file', default='benchmark.cfg', metavar='FILE', help='Optional configuration file to provide argument defaults. All config options can be overridden using the command line.')
  29. args, remaining_argv = conf_parser.parse_known_args()
  30. try:
  31. with open (args.conf_file):
  32. config = ConfigParser.SafeConfigParser()
  33. config.read([os.getcwd() + '/' + args.conf_file])
  34. defaults = dict(config.items("Defaults"))
  35. except IOError:
  36. if args.conf_file != 'benchmark.cfg':
  37. print 'Configuration file not found!'
  38. defaults = { "client-host":"localhost"}
  39. ##########################################################
  40. # Set up default values
  41. ##########################################################
  42. serverHost = os.environ.get('TFB_SERVER_HOST')
  43. clientHost = os.environ.get('TFB_CLIENT_HOST')
  44. clientUser = os.environ.get('TFB_CLIENT_USER')
  45. clientIden = os.environ.get('TFB_CLIENT_IDENTITY_FILE')
  46. databaHost = os.getenv('TFB_DATABASE_HOST', clientHost)
  47. databaUser = os.getenv('TFB_DATABASE_USER', clientUser)
  48. dbIdenFile = os.getenv('TFB_DATABASE_IDENTITY_FILE', clientIden)
  49. maxThreads = 8
  50. try:
  51. maxThreads = multiprocessing.cpu_count()
  52. except:
  53. pass
  54. ##########################################################
  55. # Set up argument parser
  56. ##########################################################
  57. parser = argparse.ArgumentParser(description='Run the Framework Benchmarking test suite.',
  58. parents=[conf_parser],
  59. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  60. # SSH options
  61. parser.add_argument('-s', '--server-host', default=serverHost, help='The application server.')
  62. parser.add_argument('-c', '--client-host', default=clientHost, help='The client / load generation server.')
  63. parser.add_argument('-u', '--client-user', default=clientUser, help='The username to use for SSH to the client instance.')
  64. parser.add_argument('-i', '--client-identity-file', dest='client_identity_file', default=clientIden,
  65. help='The key to use for SSH to the client instance.')
  66. parser.add_argument('-d', '--database-host', default=databaHost,
  67. help='The database server. If not provided, defaults to the value of --client-host.')
  68. parser.add_argument('--database-user', default=databaUser,
  69. help='The username to use for SSH to the database instance. If not provided, defaults to the value of --client-user.')
  70. parser.add_argument('--database-identity-file', default=dbIdenFile, dest='database_identity_file',
  71. help='The key to use for SSH to the database instance. If not provided, defaults to the value of --client-identity-file.')
  72. parser.add_argument('-p', dest='password_prompt', action='store_true', help='Prompt for password')
  73. parser.add_argument('--install-software', action='store_true', help='runs the installation script before running the rest of the commands')
  74. # Install options
  75. parser.add_argument('--install', choices=['client', 'database', 'server', 'all'], default=None,
  76. help='Runs installation script(s) before continuing on to execute the tests.')
  77. parser.add_argument('--install-error-action', choices=['abort', 'continue'], default='continue', help='action to take in case of error during installation')
  78. # Test options
  79. parser.add_argument('--test', nargs='+', help='names of tests to run')
  80. parser.add_argument('--exclude', nargs='+', help='names of tests to exclude')
  81. parser.add_argument('--type', choices=['all', 'json', 'db', 'query', 'fortune', 'update', 'plaintext'], default='all', help='which type of test to run')
  82. parser.add_argument('-m', '--mode', choices=['benchmark', 'verify'], default='benchmark', help='verify mode will only start up the tests, curl the urls and shutdown')
  83. parser.add_argument('--list-tests', action='store_true', default=False, help='lists all the known tests that can run')
  84. parser.add_argument('--list-test-metadata', action='store_true', default=False, help='writes all the test metadata as a JSON file in the results directory')
  85. parser.add_argument('--name', default="ec2", help='The name to give this test. Results will be placed in a folder using this name.')
  86. parser.add_argument('--os', choices=['linux', 'windows'], default='linux', help='The operating system of the application/framework server (the one running' +
  87. 'this binary')
  88. parser.add_argument('--database-os', choices=['linux', 'windows'], default='linux', help='The operating system of the database server.')
  89. # Benchmark options
  90. parser.add_argument('--max-concurrency', default=256, help='the maximum number of HTTP connections that wrk will keep open. The query tests will run at this maximum', type=int)
  91. parser.add_argument('--max-queries', default=20, help='The maximum number of queries to run during the query test', type=int)
  92. parser.add_argument('--query-interval', default=5, type=int, help='Query tests will go from 1 query to max queries in increments of interval queries')
  93. parser.add_argument('--max-threads', default=maxThreads, help='The max number of threads to run wrk at. This should be set to the number of cores for your client system.', type=int)
  94. parser.add_argument('--duration', default=15, help='Time in seconds that each test should run for.')
  95. parser.add_argument('--starting-concurrency', default=8, type=int)
  96. parser.add_argument('--sleep', type=int, default=60, help='the amount of time to sleep after starting each test to allow the server to start up.')
  97. # Misc Options
  98. parser.add_argument('--parse', help='Parses the results of the given timestamp and merges that with the latest results')
  99. parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Causes the configuration to print before any other commands are executed.')
  100. parser.set_defaults(**defaults) # Must do this after add, or each option's default will override the configuration file default
  101. args = parser.parse_args(remaining_argv)
  102. # Verify and massage options
  103. if args.client_user is None:
  104. print 'Usernames (e.g. --client-user and --database-user) are required!'
  105. print 'The system will SSH into the client and the database for the install stage'
  106. print 'Aborting'
  107. exit(1)
  108. if args.database_user is None:
  109. args.database_user = args.client_user
  110. if args.database_host is None:
  111. args.database_host = args.client_host
  112. if args.verbose:
  113. print 'Configuration options: '
  114. pprint(args)
  115. benchmarker = Benchmarker(vars(args))
  116. # Ensure a consistent environment for any subprocesses run during
  117. # the lifetime of this program
  118. # Note: This will not work if your command starts with 'sudo', you
  119. # will need sudo sh -c ". config/benchmark_profile && your_command"
  120. setup_env = '. config/benchmark_profile && env'
  121. mini_environ = os.environ.copy()
  122. mini_environ.clear()
  123. mini_environ['HOME']=os.environ['HOME']
  124. mini_environ['PATH']=os.environ['PATH']
  125. mini_environ['USER']=os.environ['USER']
  126. fwroot=subprocess.check_output("pwd", shell=True)
  127. mini_environ['FWROOT']=fwroot
  128. os.environ.clear()
  129. env = subprocess.check_output(setup_env, shell=True, env=mini_environ)
  130. for line in env.split('\n'):
  131. try:
  132. split=line.index('=')
  133. key=line[:split]
  134. value=line[split+1:]
  135. os.environ[key]=value
  136. except:
  137. print "WARN: Cannot parse %s from config/benchmark_profile" % line
  138. continue
  139. out = subprocess.check_output('env', shell=True)
  140. print 'Checking environment'
  141. print out
  142. # Run the benchmarker in the specified mode
  143. if benchmarker.list_tests:
  144. benchmarker.run_list_tests()
  145. elif benchmarker.list_test_metadata:
  146. benchmarker.run_list_test_metadata()
  147. elif benchmarker.parse != None:
  148. benchmarker.parse_timestamp()
  149. else:
  150. benchmarker.run()
  151. if __name__ == "__main__":
  152. sys.exit(main())