10
from logging import StreamHandler, FileHandler, Formatter
12
from optparse import OptionParser
14
from xml.dom import minidom
17
DEFAULT_DIRECTORY = "/var/cache/checkbox/phoronix"
18
DEFAULT_LOG_LEVEL = "critical"
19
DEFAULT_SAVE_NAME = "checkbox"
22
UNIQUE_NAME_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
25
def unique_name(length=15):
26
l = len(UNIQUE_NAME_CHARACTERS)
27
return "".join([UNIQUE_NAME_CHARACTERS[ord(c)%l] for c in os.urandom(length)])
29
def get_benchmark(node, name):
30
for benchmark in node.getElementsByTagName("Benchmark"):
31
for child in benchmark.childNodes:
32
if child.nodeName == "TestName" \
33
and child.firstChild.data == name:
38
def get_result(node, name):
39
for entry in node.getElementsByTagName("Entry"):
40
for child in entry.childNodes:
41
if child.nodeName == "Identifier" \
42
and child.firstChild.data == name:
47
def parse_phoronix(options, suite):
48
file = posixpath.expanduser("~/.phoronix-test-suite/test-results/%s/composite.xml"
50
tree = minidom.parse(file)
51
benchmark = get_benchmark(tree, suite)
53
result = get_result(benchmark, options.run_name)
55
value = result.getElementsByTagName("Value")[0]
56
return value.firstChild.data
60
def run_phoronix(options, suite):
62
("Enter a name to save these results: ", options.save_name),
63
("Enter a unique name for this test run: ", options.run_name)]
65
command = posixpath.join(options.directory, "phoronix-test-suite")
66
args = ["batch-benchmark", suite]
67
connection = pexpect.spawn(command, args=args,
68
cwd=options.directory,
69
timeout=options.timeout)
70
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
71
# Backward compatibility for pexpect
72
if hasattr(connection, "logfile"):
73
connection.logfile = sys.stdout
75
connection.setlog(sys.stdout)
78
questions = [qa[0] for qa in question_answer]
79
index = connection.expect_exact(questions + [pexpect.EOF])
80
if index >= len(question_answer):
83
answer = question_answer[index][1]
85
answer = unique_name()
87
connection.send("%s\n" % answer)
89
return parse_phoronix(options, suite)
92
usage = "Usage: %prog [OPTIONS] SUITE"
93
parser = OptionParser(usage=usage)
94
parser.add_option("-l", "--log", metavar="FILE",
95
help="log file where to send output")
96
parser.add_option("--log-level",
97
default=DEFAULT_LOG_LEVEL,
98
help="one of debug, info, warning, error or critical")
99
parser.add_option("-d", "--directory",
100
default=DEFAULT_DIRECTORY,
101
help="Directory where phoronix was cloned (default %default)")
102
parser.add_option("-r", "--run-name",
103
default=unique_name(),
104
help="Unique name for this test run (default is random)")
105
parser.add_option("-s", "--save-name",
106
default=DEFAULT_SAVE_NAME,
107
help="Name to save these results (default %default)")
108
parser.add_option("-t", "--timeout",
109
default=DEFAULT_TIMEOUT,
110
help="Timeout to run the tests (default %default)")
111
(options, args) = parser.parse_args(args)
114
log_level = logging.getLevelName(options.log_level.upper())
116
log_handlers.append(StreamHandler())
118
log_filename = options.log
119
log_handlers.append(FileHandler(log_filename))
121
format = ("%(asctime)s %(levelname)-8s %(message)s")
123
for handler in log_handlers:
124
handler.setFormatter(Formatter(format))
125
logging.getLogger().addHandler(handler)
127
logging.getLogger().setLevel(log_level)
128
elif not logging.getLogger().handlers:
129
logging.disable(logging.CRITICAL)
133
parser.error("Must specify a SUITE")
135
parser.error("Must specify a single SUITE")
138
value = run_phoronix(options, suite)
147
if __name__ == "__main__":
148
sys.exit(main(sys.argv[1:]))