utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. print "Creating Benchmarker from benchmark.cfg.example"
  30. default_config = setup_util.get_fwroot() + "/benchmark.cfg.example"
  31. config = ConfigParser.SafeConfigParser()
  32. config.readfp(open(default_config))
  33. defaults = dict(config.items("Defaults"))
  34. # Convert strings into proper python types
  35. for k,v in defaults.iteritems():
  36. try:
  37. defaults[k] = literal_eval(v)
  38. except Exception:
  39. pass
  40. # Ensure we only run the __init__ method of Benchmarker
  41. defaults['install'] = None
  42. benchmarker = Benchmarker(defaults)
  43. # Search in both old and new directories
  44. fwroot = setup_util.get_fwroot()
  45. config_files = glob.glob("%s/*/benchmark_config" % fwroot)
  46. config_files.extend(glob.glob("%s/frameworks/*/*/benchmark_config" % fwroot))
  47. tests = []
  48. for config_file_name in config_files:
  49. config = None
  50. with open(config_file_name, 'r') as config_file:
  51. try:
  52. config = json.load(config_file)
  53. except ValueError:
  54. # User-friendly errors
  55. print("Error loading '%s'." % config_file_name)
  56. raise
  57. # Find all tests in the config file
  58. config_tests = framework_test.parse_config(config,
  59. os.path.dirname(config_file_name), benchmarker)
  60. # Filter
  61. for test in config_tests:
  62. if len(include) is 0 and len(exclude) is 0:
  63. # No filters, we are running everything
  64. tests.append(test)
  65. elif test.name in exclude:
  66. continue
  67. elif test.name in include:
  68. tests.append(test)
  69. else:
  70. # An include list exists, but this test is
  71. # not listed there, so we ignore it
  72. pass
  73. tests.sort(key=lambda x: x.name)
  74. return tests
  75. def gather_frameworks(include = [], exclude=[], benchmarker=None):
  76. '''Return a dictionary mapping frameworks->[test1,test2,test3]
  77. for quickly grabbing all tests in a grouped manner.
  78. Args have the same meaning as gather_tests'''
  79. tests = gather_tests(include, exclude, benchmarker)
  80. frameworks = dict()
  81. for test in tests:
  82. if test.framework not in frameworks:
  83. frameworks[test.framework] = []
  84. frameworks[test.framework].append(test)
  85. return frameworks
  86. def header(message, top='-', bottom='-'):
  87. '''
  88. Generates a clean header
  89. '''
  90. topheader = (top * 80)[:80]
  91. bottomheader = (bottom * 80)[:80]
  92. result = ""
  93. if topheader != "":
  94. result += "%s" % topheader
  95. if message != "":
  96. if result == "":
  97. result = " %s" % message
  98. else:
  99. result += "\n %s" % message
  100. if bottomheader != "":
  101. if result == "":
  102. result = "%s" % bottomheader
  103. else:
  104. result += "\n%s" % bottomheader
  105. return result + '\n'