qutebrowser/scripts/dev/run_profile.py

89 lines
2.8 KiB
Python
Raw Normal View History

2014-09-22 20:21:00 +02:00
#!/usr/bin/env python3
2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-06-19 08:28:05 +02:00
#
# This file is part of qutebrowser.
#
# qutebrowser 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 3 of the License, or
# (at your option) any later version.
#
# qutebrowser 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 qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2014-02-26 21:18:53 +01:00
"""Profile qutebrowser."""
import sys
import cProfile
import os.path
2014-08-26 19:10:14 +02:00
import os
import tempfile
import subprocess
import shutil
import argparse
import shlex
2014-02-26 21:18:53 +01:00
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir,
2016-04-27 18:30:54 +02:00
os.pardir))
import qutebrowser.qutebrowser
2014-02-26 21:18:53 +01:00
def parse_args():
2016-02-11 07:17:00 +01:00
"""Parse commandline arguments.
Return:
A (namespace, remaining_args) tuple from argparse.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--profile-tool', metavar='TOOL',
action='store', choices=['kcachegrind', 'snakeviz',
'gprof2dot', 'none'],
default='snakeviz',
help="The tool to use to view the profiling data")
parser.add_argument('--profile-file', metavar='FILE', action='store',
help="The filename to use with --profile-tool=none")
return parser.parse_known_args()
def main():
args, remaining = parse_args()
tempdir = tempfile.mkdtemp()
if args.profile_tool == 'none':
profilefile = os.path.join(os.getcwd(), args.profile_file)
else:
profilefile = os.path.join(tempdir, 'profile')
sys.argv = [sys.argv[0]] + remaining
profiler = cProfile.Profile()
profiler.runcall(qutebrowser.qutebrowser.main)
profiler.dump_stats(profilefile)
if args.profile_tool == 'none':
pass
elif args.profile_tool == 'gprof2dot':
# yep, shell=True. I know what I'm doing.
subprocess.call('gprof2dot -f pstats {} | dot -Tpng | feh -F -'.format(
shlex.quote(profilefile)), shell=True)
elif args.profile_tool == 'kcachegrind':
callgraphfile = os.path.join(tempdir, 'callgraph')
subprocess.call(['pyprof2calltree', '-k', '-i', profilefile,
2016-04-27 18:30:54 +02:00
'-o', callgraphfile])
elif args.profile_tool == 'snakeviz':
subprocess.call(['snakeviz', profilefile])
shutil.rmtree(tempdir)
if __name__ == '__main__':
main()