utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 test.name in exclude:
  63. continue
  64. elif len(include) is 0 or test.name in include:
  65. tests.append(test)
  66. tests.sort(key=lambda x: x.name)
  67. return tests
  68. def gather_frameworks(include = [], exclude=[], benchmarker=None):
  69. '''Return a dictionary mapping frameworks->[test1,test2,test3]
  70. for quickly grabbing all tests in a grouped manner.
  71. Args have the same meaning as gather_tests'''
  72. tests = gather_tests(include, exclude, benchmarker)
  73. frameworks = dict()
  74. for test in tests:
  75. if test.framework not in frameworks:
  76. frameworks[test.framework] = []
  77. frameworks[test.framework].append(test)
  78. return frameworks
  79. def header(message, top='-', bottom='-'):
  80. '''
  81. Generates a clean header
  82. '''
  83. topheader = (top * 80)[:80]
  84. bottomheader = (bottom * 80)[:80]
  85. result = ""
  86. if topheader != "":
  87. result += "%s" % topheader
  88. if message != "":
  89. if result == "":
  90. result = " %s" % message
  91. else:
  92. result += "\n %s" % message
  93. if bottomheader != "":
  94. if result == "":
  95. result = "%s" % bottomheader
  96. else:
  97. result += "\n%s" % bottomheader
  98. return result + '\n'