-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraffiti.py
executable file
·118 lines (98 loc) · 3.82 KB
/
graffiti.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
#!/usr/bin/env python
# coding: utf8
"""
Graffiti, a map server performance reporter.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Paul Blottiere"
__contact__ = "[email protected]"
__copyright__ = "Copyright 2019, Paul Blottiere"
__date__ = "2019/06/10"
__email__ = "[email protected]"
__license__ = "GPLv3"
import argparse
import os
import time
import sys
import subprocess
from tqdm import trange
from graffiti import (Config,
Request,
Graph,
Database,
Report,
Style)
SPLASH = (''
' _____ _____.__ __ .__\n'
' ________________ _/ ____\/ ____\__|/ |_|__|\n'
' / ___\_ __ \__ \\\\ __\\\\ __\| \ __\ |\n'
' / /_/ > | \// __ \| | | | | || | | |\n'
' \___ /|__| (____ /__| |__| |__||__| |__|\n'
'/_____/ \/\n\n')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Graffiti')
parser.add_argument('-c', '--cfg', type=str, help='YAML configuration file')
parser.add_argument('-d', '--db', action='store_true', help='Open the database')
parser.add_argument('-s', '--style', type=str, help='YAML Style file for histograms')
args = parser.parse_args()
if not args.cfg:
parser.print_help()
elif (os.path.isfile(args.cfg)):
cfg = Config(args.cfg, args.style)
# subcommands
if args.db:
if not cfg.database:
sys.stdout.write('Cannot open the database!\n')
sys.exit(1)
subprocess.run(['sqlite3', Database.path(cfg.database)])
sys.exit(0)
# scenario
sys.stdout.write(SPLASH)
report = Report(cfg.title, cfg.date, cfg.logo, cfg.css)
database = Database(cfg.database)
style = Style(cfg.styles)
errors = []
start = time.time()
for i in trange(len(cfg.requests), desc='Requests'):
req = Request.build(cfg.requests[i])
req.run()
database.log(req)
if req.errors:
errors += req.errors
graph = Graph(req, style)
graph.draw(cfg.imdir)
report.add(graph)
report.write(cfg.html, cfg.desc)
database.close()
# final log
dur = round(time.time() - start, 1)
n = 0
for req in cfg.requests:
n += req.iterations * len(req.hosts)
if errors:
errlog = os.path.join(cfg.logdir, 'errors.log')
with open(errlog, 'w') as f:
for error in errors:
f.write('--------\n')
f.write(error.tostr())
sys.stdout.write('\nTerminated with some errors (see {}) in {} sec'
' for {} requests!\n'
.format(errlog, dur, n))
sys.exit(1)
else:
sys.stdout.write('\nTerminated without errors in {} sec for {} '
'requests!\n'
.format(dur, n))
else:
sys.stderr.write('Error: \'{}\' is not a valid configuration file.'
.format(args.cfg))
sys.exit(1)
sys.exit(0)