Skip to content

Commit 754acb3

Browse files
committed
Add --gzip option to cli
1 parent 0a8f27e commit 754acb3

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

vmprof/__main__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import os
12
import runpy
2-
import sys, os
3+
import subprocess
4+
import sys
35
import tempfile
46

57
import vmprof
@@ -40,6 +42,7 @@ def upload_stats(stats, forest, args):
4042

4143
def main():
4244
args = vmprof.cli.parse_args(sys.argv[1:])
45+
proc = None
4346

4447
if args.web:
4548
output_mode = OUTPUT_WEB
@@ -51,14 +54,24 @@ def main():
5154
if output_mode == OUTPUT_FILE:
5255
prof_file = args.output
5356
prof_name = prof_file.name
57+
fileno = prof_file.fileno()
58+
print("gzip: ", args)
59+
if args.gzip:
60+
cmd = ['/usr/bin/gzip', "-" + str(args.gzip)]
61+
proc = subprocess.Popen(cmd, bufsize=-1,
62+
stdin=subprocess.PIPE,
63+
stdout=prof_file.fileno(),
64+
close_fds=True)
65+
fileno = proc.stdin.fileno()
5466
else:
5567
prof_file = tempfile.NamedTemporaryFile(delete=False)
5668
prof_name = prof_file.name
69+
fileno = prof_file.fileno()
5770

5871
if args.jitlog:
5972
assert hasattr(vmprof, 'enable_jitlog'), "note: jitlog is only available on pypy"
6073

61-
vmprof.enable(prof_file.fileno(), args.period, args.mem)
74+
vmprof.enable(fileno, args.period, args.mem)
6275
if args.jitlog:
6376
# note that this file descr is then handled by jitlog
6477
fd = os.open(prof_name + '.jitlog', os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
@@ -74,6 +87,9 @@ def main():
7487
vmprof.disable()
7588
if args.jitlog and hasattr(vmprof, 'disable_jitlog'):
7689
vmprof.disable_jitlog(fd)
90+
if proc:
91+
proc.stdin.close()
92+
proc.wait()
7793
prof_file.close()
7894
show_stats(prof_name, output_mode, args)
7995
if output_mode != OUTPUT_FILE:

vmprof/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def build_argparser():
5353
action='store_true',
5454
help='Upload the jitlog to remote server (defaults to vmprof.com)',
5555
)
56+
parser.add_argument(
57+
'--gzip',
58+
nargs='?',
59+
type=int,
60+
const=4,
61+
metavar='level',
62+
help="Compress output file with gzip with specified level",
63+
)
5664
output_mode_args = parser.add_mutually_exclusive_group()
5765
output_mode_args.add_argument(
5866
'--web',
@@ -62,8 +70,8 @@ def build_argparser():
6270
output_mode_args.add_argument(
6371
'--output', '-o',
6472
metavar='file.prof',
65-
type=argparse.FileType('w+b'),
66-
help='Save profiling data to file'
73+
type=argparse.FileType('wb'),
74+
help='Save profiling data to file',
6775
)
6876

6977
return parser

0 commit comments

Comments
 (0)