run-tests.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python
  2. import argparse
  3. import ConfigParser
  4. import sys
  5. import os
  6. import multiprocessing
  7. import itertools
  8. import copy
  9. import subprocess
  10. from pprint import pprint
  11. from benchmark.benchmarker import Benchmarker
  12. from setup.linux.unbuffered import Unbuffered
  13. from setup.linux import setup_util
  14. # Enable cross-platform colored output
  15. from colorama import init
  16. init()
  17. class StoreSeqAction(argparse.Action):
  18. '''Helper class for parsing a sequence from the command line'''
  19. def __init__(self, option_strings, dest, nargs=None, **kwargs):
  20. super(StoreSeqAction, self).__init__(option_strings, dest, type=str, **kwargs)
  21. def __call__(self, parser, namespace, values, option_string=None):
  22. setattr(namespace, self.dest, self.parse_seq(values))
  23. def parse_seq(self, argument):
  24. try:
  25. return [int(argument)]
  26. except ValueError:
  27. pass
  28. if ":" in argument: #
  29. try:
  30. (start,step,end) = argument.split(':')
  31. except ValueError:
  32. print "Invalid: %s" % argument
  33. print "Requires start:step:end, e.g. 1:2:10"
  34. raise
  35. result = range(int(start), int(end), int(step))
  36. else: # 1,2,3,7
  37. result = argument.split(',')
  38. return [abs(int(item)) for item in result]
  39. ###################################################################################################
  40. # Main
  41. ###################################################################################################
  42. def main(argv=None):
  43. ''' Runs the program. There are three ways to pass arguments
  44. 1) environment variables TFB_*
  45. 2) configuration file benchmark.cfg
  46. 3) command line flags
  47. In terms of precedence, 3 > 2 > 1, so config file trumps environment variables
  48. but command line flags have the final say
  49. '''
  50. # Do argv default this way, as doing it in the functional declaration sets it at compile time
  51. if argv is None:
  52. argv = sys.argv
  53. # Enable unbuffered output so messages will appear in the proper order with subprocess output.
  54. sys.stdout=Unbuffered(sys.stdout)
  55. # Update python environment
  56. # 1) Ensure the current directory (which should be the benchmark home directory) is in the path so that the tests can be imported.
  57. sys.path.append('.')
  58. # 2) Ensure toolset/setup/linux is in the path so that the tests can "import setup_util".
  59. sys.path.append('toolset/setup/linux')
  60. # Update environment for shell scripts
  61. fwroot = setup_util.get_fwroot()
  62. if not fwroot:
  63. fwroot = os.getcwd()
  64. setup_util.replace_environ(config='config/benchmark_profile', root=fwroot)
  65. print "FWROOT is %s"%setup_util.get_fwroot()
  66. conf_parser = argparse.ArgumentParser(
  67. description=__doc__,
  68. formatter_class=argparse.RawDescriptionHelpFormatter,
  69. add_help=False)
  70. 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.')
  71. args, remaining_argv = conf_parser.parse_known_args()
  72. try:
  73. with open (args.conf_file):
  74. config = ConfigParser.SafeConfigParser()
  75. config.read([os.getcwd() + '/' + args.conf_file])
  76. defaults = dict(config.items("Defaults"))
  77. except IOError:
  78. if args.conf_file != 'benchmark.cfg':
  79. print 'Configuration file not found!'
  80. defaults = { "client-host":"localhost"}
  81. ##########################################################
  82. # Set up default values
  83. ##########################################################
  84. serverHost = os.environ.get('TFB_SERVER_HOST')
  85. clientHost = os.environ.get('TFB_CLIENT_HOST')
  86. clientUser = os.environ.get('TFB_CLIENT_USER')
  87. clientIden = os.environ.get('TFB_CLIENT_IDENTITY_FILE')
  88. databaHost = os.getenv('TFB_DATABASE_HOST', clientHost)
  89. databaUser = os.getenv('TFB_DATABASE_USER', clientUser)
  90. dbIdenFile = os.getenv('TFB_DATABASE_IDENTITY_FILE', clientIden)
  91. maxThreads = 8
  92. try:
  93. maxThreads = multiprocessing.cpu_count()
  94. except:
  95. pass
  96. ##########################################################
  97. # Set up argument parser
  98. ##########################################################
  99. parser = argparse.ArgumentParser(description="Install or run the Framework Benchmarks test suite.",
  100. parents=[conf_parser],
  101. formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  102. epilog='''If an argument includes (type int-sequence), then it accepts integer lists in multiple forms.
  103. Using a single number e.g. 5 will create a list [5]. Using commas will create a list containing those
  104. values e.g. 1,3,6 creates [1, 3, 6]. Using three colon-separated numbers of start:step:end will create a
  105. list, using the semantics of python's range function, e.g. 1:3:15 creates [1, 4, 7, 10, 13] while
  106. 0:1:5 creates [0, 1, 2, 3, 4]
  107. ''')
  108. # SSH options
  109. parser.add_argument('-s', '--server-host', default=serverHost, help='The application server.')
  110. parser.add_argument('-c', '--client-host', default=clientHost, help='The client / load generation server.')
  111. parser.add_argument('-u', '--client-user', default=clientUser, help='The username to use for SSH to the client instance.')
  112. parser.add_argument('-i', '--client-identity-file', dest='client_identity_file', default=clientIden,
  113. help='The key to use for SSH to the client instance.')
  114. parser.add_argument('-d', '--database-host', default=databaHost,
  115. help='The database server. If not provided, defaults to the value of --client-host.')
  116. parser.add_argument('--database-user', default=databaUser,
  117. help='The username to use for SSH to the database instance. If not provided, defaults to the value of --client-user.')
  118. parser.add_argument('--database-identity-file', default=dbIdenFile, dest='database_identity_file',
  119. help='The key to use for SSH to the database instance. If not provided, defaults to the value of --client-identity-file.')
  120. parser.add_argument('-p', dest='password_prompt', action='store_true', help='Prompt for password')
  121. # Install options
  122. parser.add_argument('--install', choices=['client', 'database', 'server', 'all'], default=None,
  123. help='Runs installation script(s) before continuing on to execute the tests.')
  124. parser.add_argument('--install-error-action', choices=['abort', 'continue'], default='continue', help='action to take in case of error during installation')
  125. parser.add_argument('--install-strategy', choices=['unified', 'pertest'], default='unified',
  126. help='''Affects `--install server`: With unified, all server software is installed into a single directory.
  127. With pertest each test gets its own installs directory, but installation takes longer''')
  128. parser.add_argument('--install-only', action='store_true', default=False, help='Do not run benchmark or verification, just install and exit')
  129. # Test options
  130. parser.add_argument('--test', nargs='+', help='names of tests to run')
  131. parser.add_argument('--exclude', nargs='+', help='names of tests to exclude')
  132. parser.add_argument('--type', choices=['all', 'json', 'db', 'query', 'fortune', 'update', 'plaintext'], default='all', help='which type of test to run')
  133. parser.add_argument('-m', '--mode', choices=['benchmark', 'verify'], default='benchmark', help='verify mode will only start up the tests, curl the urls and shutdown')
  134. parser.add_argument('--list-tests', action='store_true', default=False, help='lists all the known tests that can run')
  135. 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')
  136. parser.add_argument('--name', default="ec2", help='The name to give this test. Results will be placed in a folder using this name.')
  137. parser.add_argument('--os', choices=['linux', 'windows'], default='linux', help='The operating system of the application/framework server (the one running' +
  138. 'this binary')
  139. parser.add_argument('--database-os', choices=['linux', 'windows'], default='linux', help='The operating system of the database server.')
  140. # Benchmark options
  141. 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)
  142. parser.add_argument('--max-queries', default=20, help='The maximum number of queries to run during the query test', type=int)
  143. 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')
  144. 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)
  145. parser.add_argument('--duration', default=15, help='Time in seconds that each test should run for.')
  146. parser.add_argument('--starting-concurrency', default=8, type=int)
  147. 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.')
  148. # Misc Options
  149. parser.add_argument('--parse', help='Parses the results of the given timestamp and merges that with the latest results')
  150. parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Causes the configuration to print before any other commands are executed.')
  151. parser.set_defaults(**defaults) # Must do this after add, or each option's default will override the configuration file default
  152. args = parser.parse_args(remaining_argv)
  153. # Verify and massage options
  154. if args.client_user is None:
  155. print 'Usernames (e.g. --client-user and --database-user) are required!'
  156. print 'The system will SSH into the client and the database for the install stage'
  157. print 'Aborting'
  158. exit(1)
  159. if args.database_user is None:
  160. args.database_user = args.client_user
  161. if args.database_host is None:
  162. args.database_host = args.client_host
  163. if args.verbose:
  164. print 'Configuration options: '
  165. pprint(args)
  166. benchmarker = Benchmarker(vars(args))
  167. # Run the benchmarker in the specified mode
  168. if benchmarker.list_tests:
  169. benchmarker.run_list_tests()
  170. elif benchmarker.list_test_metadata:
  171. benchmarker.run_list_test_metadata()
  172. elif benchmarker.parse != None:
  173. benchmarker.parse_timestamp()
  174. elif not benchmarker.install_only:
  175. return benchmarker.run()
  176. if __name__ == "__main__":
  177. sys.exit(main())