Skip to content

Instantly share code, notes, and snippets.

@dnordberg
Created June 9, 2020 17:32
Show Gist options
  • Save dnordberg/e706c4e61432bd626b8a1f20c63b7e20 to your computer and use it in GitHub Desktop.
Save dnordberg/e706c4e61432bd626b8a1f20c63b7e20 to your computer and use it in GitHub Desktop.
"""
Requires python3 and pandas.
Add this to a file:
PS4='+ $EPOCHREALTIME\011 '
exec 3>&2 2>/tmp/bashstart.$$.log
set -x
...
set +x
exec 2>&3 3>&-
Then profile the output with this script, e.g.:
python3 view_bash_profile_output.py /tmp/bashstart.35464.log 0.0221729
cmds time_diff
1 ++SHELL_SESSION_HISTORY=0 0.088930
2 ++HISTCONTROL=ignoreboth:erasedups 0.022173
4 ++shopt -s histappend 0.026941
5 ++shopt -s cmdhist 0.026941
6 ++test -e /Users/danielnordberg/.iterm2_shell_... 0.044107
... ... ...
7187 +++export PYENV_VIRTUALENV_INIT=1 0.032187
7189 +++[[ trap '__bp_preexec_invoke_exec "$_"' DEB... 0.093222
7190 +++PROMPT_COMMAND='_pyenv_virtualenv_hook; tra... 0.063896
7191 ++alias 'git-pull-all=find . -maxdepth 3 -name... 0.051975
7193 ++'[' 1 == 1 ']' 0.028133
"""
import pandas as pd
import sys
pd.set_option('display.max_rows', None)
def clean(text):
return text.replace('\n', '').replace('\t', '')
def main():
if len(sys.argv) < 2:
print(
"Usage: python3 view_bash_profile_output.py {file} {microseconds_time_diff_greater_than:optional}")
sys.exit()
logfile = sys.argv[1]
time_diff_greater_than = None
if len(sys.argv) > 2:
time_diff_greater_than = float(sys.argv[2])
profile_info = []
with open(logfile) as fp:
counter = 0
last_time = None
for line in fp:
if line.startswith('+'):
counter += 1
level, time, *cmds = line.split()
# get time diff in milliseconds
profile_info.append({
'cmds': level + " ".join([
clean(cmd)
for cmd in cmds]),
'time_diff': last_time and (
(float(time) - float(last_time)) * 10 ** 3) or 0
})
last_time = time
else:
# append to last command
profile_info[counter - 1]['cmds'] += clean(line)
df = pd.DataFrame(profile_info)
if time_diff_greater_than:
print(df[df.time_diff > time_diff_greater_than])
else:
print(df)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment