utils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import ConfigParser
  2. import os
  3. import glob
  4. import json
  5. from ast import literal_eval
  6. def gather_tests(include = [], exclude=[], benchmarker=None):
  7. '''
  8. Given test names as strings, returns a list of FrameworkTest objects.
  9. For example, 'aspnet-mysql-raw' turns into a FrameworkTest object with
  10. variables for checking the test directory, the test database os, and
  11. other useful items.
  12. With no arguments, every test in this framework will be returned.
  13. With include, only tests with this exact name will be returned.
  14. With exclude, all tests but those excluded will be returned.
  15. A benchmarker is needed to construct full FrameworkTest objects. If
  16. one is not provided, a default Benchmarker will be created.
  17. '''
  18. # Avoid setting up a circular import
  19. from benchmark import framework_test
  20. from benchmark.benchmarker import Benchmarker
  21. from setup.linux import setup_util
  22. # Help callers out a bit
  23. if include is None:
  24. include = []
  25. if exclude is None:
  26. exclude = []
  27. # Old, hacky method to exclude all tests was to
  28. # request a test known to not exist, such as ''.
  29. # If test '' was requested, short-circuit and return
  30. # nothing immediately
  31. if len(include) == 1 and '' in include:
  32. return []
  33. # Setup default Benchmarker using example configuration
  34. if benchmarker is None:
  35. print "Creating Benchmarker from benchmark.cfg.example"
  36. default_config = setup_util.get_fwroot() + "/benchmark.cfg.example"
  37. config = ConfigParser.SafeConfigParser()
  38. config.readfp(open(default_config))
  39. defaults = dict(config.items("Defaults"))
  40. # Convert strings into proper python types
  41. for k,v in defaults.iteritems():
  42. try:
  43. defaults[k] = literal_eval(v)
  44. except Exception:
  45. pass
  46. # Ensure we only run the __init__ method of Benchmarker
  47. defaults['install'] = None
  48. benchmarker = Benchmarker(defaults)
  49. # Search in both old and new directories
  50. fwroot = setup_util.get_fwroot()
  51. config_files = glob.glob("%s/*/benchmark_config" % fwroot)
  52. config_files.extend(glob.glob("%s/frameworks/*/*/benchmark_config" % fwroot))
  53. tests = []
  54. for config_file_name in config_files:
  55. config = None
  56. with open(config_file_name, 'r') as config_file:
  57. try:
  58. config = json.load(config_file)
  59. except ValueError:
  60. # User-friendly errors
  61. print("Error loading '%s'." % config_file_name)
  62. raise
  63. # Find all tests in the config file
  64. config_tests = framework_test.parse_config(config,
  65. os.path.dirname(config_file_name), benchmarker)
  66. # Filter
  67. for test in config_tests:
  68. if len(include) is 0 and len(exclude) is 0:
  69. # No filters, we are running everything
  70. tests.append(test)
  71. elif test.name in exclude:
  72. continue
  73. elif test.name in include:
  74. tests.append(test)
  75. else:
  76. # An include list exists, but this test is
  77. # not listed there, so we ignore it
  78. pass
  79. # Ensure we were able to locate everything that was
  80. # explicitly included
  81. if 0 != len(include):
  82. names = {test.name for test in tests}
  83. if 0 != len(set(include) - set(names)):
  84. missing = list(set(include) - set(names))
  85. raise Exception("Unable to locate tests %s" % missing)
  86. tests.sort(key=lambda x: x.name)
  87. return tests
  88. def gather_frameworks(include = [], exclude=[], benchmarker=None):
  89. '''Return a dictionary mapping frameworks->[test1,test2,test3]
  90. for quickly grabbing all tests in a grouped manner.
  91. Args have the same meaning as gather_tests'''
  92. tests = gather_tests(include, exclude, benchmarker)
  93. frameworks = dict()
  94. for test in tests:
  95. if test.framework not in frameworks:
  96. frameworks[test.framework] = []
  97. frameworks[test.framework].append(test)
  98. return frameworks
  99. def header(message, top='-', bottom='-'):
  100. '''
  101. Generates a clean header
  102. '''
  103. topheader = (top * 80)[:80]
  104. bottomheader = (bottom * 80)[:80]
  105. result = ""
  106. if topheader != "":
  107. result += "%s" % topheader
  108. if message != "":
  109. if result == "":
  110. result = " %s" % message
  111. else:
  112. result += "\n %s" % message
  113. if bottomheader != "":
  114. if result == "":
  115. result = "%s" % bottomheader
  116. else:
  117. result += "\n%s" % bottomheader
  118. return result + '\n'