utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. # Setup default Benchmarker using example configuration
  28. if benchmarker is None:
  29. default_config = setup_util.get_fwroot() + "/benchmark.cfg.example"
  30. config = ConfigParser.SafeConfigParser()
  31. config.readfp(open(default_config))
  32. defaults = dict(config.items("Defaults"))
  33. # Convert strings into proper python types
  34. for k,v in defaults.iteritems():
  35. try:
  36. defaults[k] = literal_eval(v)
  37. except:
  38. pass
  39. # Ensure we only run the __init__ method of Benchmarker
  40. defaults['install'] = None
  41. benchmarker = Benchmarker(defaults)
  42. # Search in both old and new directories
  43. fwroot = os.environ['FWROOT']
  44. config_files = glob.glob("%s/*/benchmark_config" % fwroot)
  45. config_files.extend(glob.glob("%s/frameworks/*/*/benchmark_config" % fwroot))
  46. tests = []
  47. for config_file_name in config_files:
  48. config = None
  49. with open(config_file_name, 'r') as config_file:
  50. try:
  51. config = json.load(config_file)
  52. except:
  53. # User-friendly errors
  54. print("Error loading '%s'." % config_file_name)
  55. raise
  56. # Find all tests in the config file
  57. config_tests = framework_test.parse_config(config,
  58. os.path.dirname(config_file_name), benchmarker)
  59. # Filter
  60. for test in config_tests:
  61. if test.name in exclude:
  62. continue
  63. elif len(include) is 0 or test.name in include:
  64. tests.append(test)
  65. tests.sort(key=lambda x: x.name)
  66. return tests
  67. def header(message, top='-', bottom='-'):
  68. '''
  69. Generates a clean header
  70. '''
  71. topheader = (top * 80)[:80]
  72. bottomheader = (bottom * 80)[:80]
  73. return "%s\n %s\n%s" % (topheader, message, bottomheader)