-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.py
256 lines (202 loc) · 5.95 KB
/
performance.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
# -*- coding: utf-8 -*-
"""
py3status profiler.
run py3status for a set amount of time
a special test config is used tests/perf.conf
a mainly a testing module found in tests/perf.py
"""
from __future__ import print_function, division
import argparse
import os
import subprocess
import shlex
import sys
from time import sleep, time
from signal import SIGTERM
try:
import psutil
except ImportError:
psutil = None
import py3status
BASE_COMMAND = '{root}/__init__.py {split} -c {config} -i {module_dir} -l {log}'
PROFILERS = {
'none': '{python} {cmd}',
'cprofile': '{python} -m cProfile -o {output} {cmd}',
'yappi': '{python} -m yappi_profiler {output} {cmd}',
'pprofile': 'pprofile --out {output} {cmd}',
'vmprof': '{python} -m vmprof -o {output} {cmd}',
}
BLOCKS = u' ▏▎▍▌▋▊▉█'
FORMAT = u'\r{bar} {percent:6.2f}% {output_time} ({p_time:.2f})'
FORMAT_NO_PSUTIL = u'\r{bar} {percent:6.2f}% {output_time}'
def profile(options):
run_duration = options.time
py3status_root = os.path.dirname(os.path.abspath(py3status.__file__))
root = os.path.dirname(os.path.abspath(__file__))
config = options.config
if not config:
config = os.path.join(root, 'configs', 'perf.conf')
module_dir = os.path.join(root, 'modules')
if options.profiler in ['none', 'cprofile', 'yappi']:
split = ''
else:
split = '--'
command = BASE_COMMAND.format(
split=split,
config=config,
module_dir=module_dir,
log=options.log,
root=py3status_root,
)
profiler = PROFILERS[options.profiler]
cmd = profiler.format(
python=options.python,
output=options.output,
cmd=command
)
try:
# turn off cursor
print("\033[?25l", end='')
mins = run_duration // 60
if mins:
mins = ' %d minutes' % mins
else:
mins = ''
seconds = run_duration % 60
if seconds:
seconds = ' %d seconds' % seconds
else:
seconds = ''
print('Running tests for{mins}{seconds}.'.format(
mins=mins, seconds=seconds
))
fnull = open(os.devnull, 'w')
# start process and pass to psutil to get timings
p = subprocess.Popen(shlex.split(cmd),
stdout=fnull,
stderr=subprocess.PIPE)
if psutil:
ps = psutil.Process(p.pid)
format = FORMAT
else:
format = FORMAT_NO_PSUTIL
# get the tty width so we can size the progress bar
columns = int(os.popen('stty size', 'r').read().split()[1])
start = time()
p_time = None
bar_len = columns - 30
while True:
t = time() - start
t_int = int(t)
if psutil:
try:
cpu_time = ps.cpu_times()
except psutil.NoSuchProcess:
break
p_time = cpu_time.user + cpu_time.system
# time as mins:secs
output_time = '%3d:%02d' % (t_int // 60, t_int % 60)
# percent of time done
t_perc = t / run_duration
percent = min(t_perc * 100, 100)
# build the progress bar
b = bar_len * t_perc
b_int = int(b)
partial = int(8 * ((b - b_int)))
partial = BLOCKS[partial]
bar = '[' + (u'█' * b_int) + partial + (' ' * (bar_len - b_int)) + ']'
output = format.format(
bar=bar,
percent=percent,
output_time=output_time,
p_time=p_time,
)
# output and flush so visible to user
print(output, end='')
sys.__stdout__.flush()
# check if we are done
if t_int >= run_duration:
break
if p.poll():
print('An error occured')
print(p.stderr.read())
break
# take things easy
sleep(0.2)
except KeyboardInterrupt:
pass
finally:
# turn cursor back on
print("\033[?25h")
if psutil:
try:
cpu_time = ps.cpu_times()
print('user {}s'.format(cpu_time.user))
print('system {}s'.format(cpu_time.system))
print('total {}s'.format(cpu_time.system + cpu_time.user))
# put py3status to bed
ps.send_signal(SIGTERM)
except psutil.NoSuchProcess:
# process terminated
print('Test terminated')
def main():
parser = argparse.ArgumentParser(
description='Run py3status for testing')
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument(
'-c',
'--config',
action="store",
dest="config",
type=str,
default='',
help="config file."
)
parser.add_argument(
'-l',
'--log',
action="store",
dest="log",
type=str,
default='/tmp/py3status_test.log',
help="log file."
)
parser.add_argument(
'-p',
'--profiler',
action="store",
dest="profiler",
type=str,
choices=PROFILERS.keys(),
default='none',
help="type of profiler"
)
parser.add_argument(
'-t',
'--time',
action="store",
dest="time",
type=int,
default=600,
help="number of seconds to run test for."
)
parser.add_argument(
'--python',
action="store",
dest="python",
type=str,
default='python3',
help="python version to use"
)
parser.add_argument(
'-o',
'--output',
action="store",
dest="output",
default='/tmp/py3status.test.output',
help="output file."
)
options = parser.parse_args()
profile(options)
if __name__ == '__main__':
main()