-
Notifications
You must be signed in to change notification settings - Fork 28
/
lpts.py
executable file
·431 lines (371 loc) · 17.7 KB
/
lpts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
'''
lpt环境设置、任务创建、任务执行、创建report
'''
import os
import sys
#from lib import lptenv
#设定系统环境
if not os.getenv('LPTROOT'):
lptdir = os.path.dirname(os.path.realpath(sys.modules[__name__].__file__))
os.environ['LPTROOT'] = lptdir
else:
lptdir = os.getenv('LPTROOT')
try:
from lpt.lib import lptenv
lptenv.setup(lptdir)
except ImportError:
cwd = os.getcwd()
os.chdir(lptdir)
from lib import lptenv
lptenv.setup(lptdir)
os.chdir(cwd)
from optparse import OptionParser
from optparse import OptionGroup
import optparse
from lpt.lib import lptlog
from lpt.lib.error import *
from lpt.lib.share import utils
from lpt.scripts import jobs
from lpt.scripts import run as Jobrun
from lpt.scripts import report as Testreport
from lpt.lib import lptxml
from lpt.scripts import compare
from lpt.lib import readconfig
LPTROOT = os.getenv('LPTROOT')
DB_DIR = os.path.join(LPTROOT, 'db')
RESULTS_DIR = os.path.join(LPTROOT, 'results')
BIN_DIR = os.path.join(LPTROOT, 'bin')
TMP_DIR = os.path.join(LPTROOT, 'tmp')
for dir in (DB_DIR, RESULTS_DIR, BIN_DIR, TMP_DIR):
if not os.path.isdir(dir):
os.makedirs(dir, mode=0o777)
JOBS_XML = os.path.join(DB_DIR, 'jobs.xml')
VERSION_FILE = os.path.join(LPTROOT, 'release')
defaultParameter = os.path.join(LPTROOT, "parameters/default.conf")
START_MSG = '''
################################################################
# --Linux Performance Testing Suite-- #
# #
# @author: Scemoon #
# @contact: [email protected] #
# @version: %s #
# #
################################################################
''' % utils.get_version(VERSION_FILE)
STOP_MSG = '''
################################################################
# #
# !!! ALL TESTS OVER !!! #
# #
################################################################
'''
def get_list_from_string(option, opt, value, parser):
'''返回list
'''
#print parser.values
return parser.values.g.split(",")
class Lpt:
'''
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Print log message.
-q, --quite No print log message
-L LEVEL, --LEVEL=LEVEL
Log Level:debug,info,warning,error,critical
-t TOOL, --tool=TOOL Test Tool
-g TOOLS, --group=TOOLS
Test Tools, example:unixbench,stream
Job:
--create create job use tools of you give by -t or by -g
option,default use all tools in parameter.conf.
-n JobName, --name=JobName
set job name
-f JobsDB, --file=JobsDB
jobs databases file of xml
-i JobID, --id=JobID
job id for run or report
-l, --list list all jobs
Run:
--run Run job by use job id, default last .You can use tool
of you give by -t option, default run all tools that
not execute in job tasks
--clean clean all temporary file after testing
Report:
-F FORMAT set report format:txt、execl、doc or pdf
-r NAME set report name
-d DIR set report dir
--report create report,with result databases file of xml
-R XML get result databases of xml
--compare performance compare
-X resultsXml, --XML=resultsXml
Result databases of xmls, separate tags with commas
-N names, --Names=names
point Test Objects, separate tags with commas
'''
def __init__(self):
self.tools_list = []
self.jobs_xml = None
self.jobName = "lpt_%s" % utils.gen_random_strs()
def set_jobName(self, name):
pass
def parser(self, argv):
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage, version="%prog 3.0.0")
parser.add_option('-v', '--verbose',
action='store_false',
dest='verbose',
default=False,
help='Print log message.')
parser.add_option('-q', '--quite',
action='store_true',
dest='verbose',
help='No print log message')
parser.add_option('-L', '--LEVEL', action='store',
metavar="LEVEL",
dest='loglevel',
choices=("info", "debug", 'warning', "error", "critical"),
help='Log Level:debug,info,warning,error,critical')
parser.add_option('-t', '--tool',
action='append',
dest="tools_list",
metavar="TOOL",
help="Test Tool")
parser.add_option('-g', '--group',
action='store',
dest="tools_group",
metavar="TOOLS",
help="Test Tools, example:unixbench,stream")
job_group = OptionGroup(parser, "Job")
job_group.add_option('--create',
action='store_const',
const=0,
dest='mode',
help="create job use tools of you give by -t or by -g option,default use all tools in parameter.conf.")
job_group.add_option('-n', '--name',
action='store',
dest='JobName',
metavar="JobName",
help="set job name")
job_group.add_option('-p', '--parameter',
action='store',
dest='parameter',
metavar="conf",
help="parameter config file")
job_group.add_option('-f', '--file',
action='store',
dest='jobs_xml',
metavar="JobsDB",
default=JOBS_XML,
help='jobs databases file of xml')
job_group.add_option('-i', '--id',
action="store",
dest='job_id',
metavar="JobID",
help="job id for run or report")
job_group.add_option('-l', '--list',
action='store_true',
dest='jobs_list',
help='list all jobs')
parser.add_option_group(job_group)
run_group = OptionGroup(parser, "Run")
run_group.add_option('--run',
action="store_const",
const=1,
dest='mode',
help="Run job by use job id, default last .You can use tool of you give by -t option, default run all tools that not execute in job tasks")
run_group.add_option('--clean',
action='store_true',
dest='clean',
default=False,
help='clean all temporary file after testing')
parser.add_option_group(run_group)
report_group = OptionGroup(parser, "Report")
report_group.add_option('-F',
action="store",
dest="format",
choices=('txt', 'xls', 'doc', 'pdf'),
metavar="FORMAT",
default='xls',
help="set report format:txt、execl、doc or pdf")
report_group.add_option('-r',
action="store",
dest="reportname",
metavar="NAME" ,
help="set report name")
report_group.add_option('-d',
action="store",
dest="reportdir",
metavar="DIR" ,
default= RESULTS_DIR,
help="set report dir")
report_group.add_option("--chart",
action="store_true",
dest="genchart",
default=False,
help="generate chart, default False")
report_group.add_option('--report',
action='store_const',
const=2,
dest="mode",
help="create report,with result databases file of xml")
report_group.add_option('-R',
action='store',
dest="resultdb",
metavar="XML",
help="get result databases of xml")
report_group.add_option('--compare',
action="store_const",
const=3,
dest="mode",
help="performance compare")
report_group.add_option('-X','--XML',
action="store",
dest="compareFiles",
metavar="resultsXml",
help="Result databases of xmls, separate tags with commas")
report_group.add_option('-N', '--Names',
action="store",
dest="compareNames",
metavar="names",
help="point Test Objects, separate tags with commas")
parser.add_option_group(report_group)
opts, args = parser.parse_args(argv)
return opts
def check_root(self):
if os.getuid() !=0:
lptlog.warning("请使用root用户执行测试用例")
sys.exit()
#raise UnRootError("请使用root用户执行测试用例")
def _print_tools(self, tool_list, tools_nodes, jobs_object):
status_tools = ["%s:'%s'" %(tool, jobs_object.get_tool_status(tools_nodes, tool)) for tool in tool_list]
return "|".join(status_tools)
def list_jobs(self, jobs_xml):
print("-" * 90)
print("%12s%18s%30s%30s" %("JOBID", "JOBNAME", "resultsDB", "STATUS"))
print("-" * 90)
jobs_object = lptxml.Jobs(jobs_xml)
#jobs_nodes = lptxml.search_job_nodes('job', xml_file=jobs_xml)
jobs_nodes = jobs_object.search_job_nodes('job')
if jobs_nodes:
for job_node in jobs_nodes:
#resultsdb = lptxml.get_job_text("resultsDB", xml_file=jobs_xml, job_node=job_node)
resultsdb = jobs_object.get_job_text(job_node, "resultsDB")
#tool_list = map(lambda x:x.get('id'), lptxml.get_tools_nodes(job_node))
tools_nodes = jobs_object.get_tools_nodes(job_node)
tool_list = [x.get('id') for x in tools_nodes]
if len(tool_list) < 3:
tools_string = self._print_tools(tool_list, tools_nodes, jobs_object)
print("%12s%18s%30s%30s" %(job_node.get('id'), job_node.get('name'), resultsdb, tools_string))
else:
tools_string = self._print_tools(tool_list[0:2], tools_nodes, jobs_object)
print("%12s%18s%30s%30s" %(job_node.get('id'), job_node.get('name'), resultsdb, tools_string))
n = 2
while True:
if len(tool_list[n:]) > 2:
tools_string = self._print_tools(tool_list[n:n+2], tools_nodes, jobs_object)
print("%90s" % tools_string)
n = n + 2
continue
else:
tools_string = self._print_tools(tool_list[n:], tools_nodes, jobs_object)
print("%90s" % tools_string)
break
print("-" * 90)
def parser_opts(self, argv=sys.argv):
opts = self.parser(argv)
#检查是否root用户
# self.check_root()
#设定log格式
if opts.verbose:
lptlog.update_logger(quiet=opts.verbose)
#
if opts.loglevel:
lptlog.update_logger(level=opts.loglevel)
if opts.parameter:
if os.path.isfile(opts.parameter):
parameterConfig = opts.parameter
else:
parameterConfig = defaultParameter
TOOLS_LIST = readconfig.para_conf(config=parameterConfig).get_sections()
#获取工具列表
if opts.tools_list:
#self.tools_list.extend(filter(lambda x: TOOLS_LIST.count(x)>0, opts.tools_list))
self.tools_list.extend([ tool for tool in opts.tools_list if tool in TOOLS_LIST])
if opts.tools_group:
#self.tools_list.extend(filter(lambda x: TOOLS_LIST.count(x)>0, opts.tools_group.split(',')))
self.tools_list.extend([ tool for tool in opts.tools_group.split(',') if tool in TOOLS_LIST])
if self.tools_list:
self.tools_list = list({}.fromkeys(self.tools_list).keys())
#lptlog.info("工具集合: %s" % ",".join(self.tools_list ))
#jobs_xml
self.jobs_xml = os.path.realpath(opts.jobs_xml)
try:
#创建job,可以指定jobs_xml,指定测试工具,指定job名称
if opts.mode == 0:
if opts.JobName:
self.jobName = opts.JobName
jobs.add_job(self.tools_list, jobs_xml=self.jobs_xml, parameter=parameterConfig, job_attrib={'name':'%s' % self.jobName})
#定义执行任务的方法
elif opts.mode == 1:
#开始测试
lptlog.info(START_MSG)
if not os.path.abspath(self.jobs_xml):
#lptlog.warning("缺失jobs文件,请核对jobs文件或者重新创建job")
raise optparse.OptionValueError("缺失jobs文件,请核对jobs文件或者重新创建job")
else:
Jobrun.run(job_id=opts.job_id, tools_list=self.tools_list,
jobs_xml=self.jobs_xml, format='txt', clean=opts.clean)
lptlog.info(STOP_MSG)
elif opts.mode == 2:
if not opts.resultdb or not os.path.abspath(opts.resultdb):
#lptlog.warning("请指定result databases(xml)")
raise optparse.OptionValueError("请指定result databases(xml)")
if not os.path.isdir(opts.reportdir):
lptlog.warning("%s 不是有效的目录" % opts.reportdir)
os.makedirs(opts.reportdir, mode=0o777)
Testreport.report(os.path.realpath(opts.resultdb), opts.reportdir, tools_list=self.tools_list,
reportname=opts.reportname, format=opts.format, chart=opts.genchart)
elif opts.mode == 3:
if opts.compareFiles:
xmls_list = [ os.path.realpath(str(xml)) for xml in opts.compareFiles.split(',') if os.path.abspath(xml)]
else:
xmls_list = []
if opts.compareNames:
names_list = [ str(name).strip() for name in opts.compareNames.split(',')]
else:
names_list = []
compare.compare(xmls_list, resultDir=opts.reportdir ,reportFile=opts.reportname, names=names_list,
tools=self.tools_list, reportType=opts.format, chart=opts.genchart)
else:
if opts.jobs_list:
#检查是否存在jobs xml文件
if not os.path.isfile(self.jobs_xml):
#lptlog.warning("缺失jobs文件,请核对jobs文件或者重新创建job")
#raise NameError("")
#raise ValueError, "缺失jobs文件,请核对jobs文件或者重新创建job"
pass
else:
self.list_jobs(self.jobs_xml)
#else:
# lptlog.warning("-h or --help show help message...")
except KeyboardInterrupt:
lptlog.warning("按下CTRL+C,将停止测试程序")
sys.exit()
except optparse.OptionValueError as e:
lptlog.error("Bad option or value: %s" % e)
except MissXML as e:
lptlog.error(e)
except TestOK:
lptlog.info("ALL Test OK")
sys.exit()
except Exception as e:
lptlog.exception('')
#lptlog.debug(e)
def main(argv=sys.argv):
lpt= Lpt()
lpt.parser_opts(argv)
if __name__ == '__main__':
main()