Skip to content

Instantly share code, notes, and snippets.

@iiey
Created July 11, 2019 10:02
Show Gist options
  • Save iiey/59cd183bac32b961d558dcd4732d8a21 to your computer and use it in GitHub Desktop.
Save iiey/59cd183bac32b961d558dcd4732d8a21 to your computer and use it in GitHub Desktop.
profiling bashrc: check for long loading time code
import argparse
import heapq
parser = argparse.ArgumentParser(description='Analyze bash start log for speed.')
parser.add_argument('filename', help='often /tmp/bashload.<PID>.log')
parser.add_argument('-n', default=20, help='number of results to show')
args = parser.parse_args()
filename, n = args.filename, int(args.n)
#add following to ~/.bashrc
###BEGIN PROFILING
#PS4='+ $(date "+%s.%N")\011 '
#exec 3>&2 2>/tmp/bashload.$$.log
#set -x
###END PROFILING
#set +x
#exec 2>&3 3>&-
with open(filename, 'r') as f:
q = []
prev_time = None
total = 0
for line in f.readlines():
#ignore empty/invalid lines
if not line or not line.strip():
continue
line = line.split()
if '+' not in line[0] or len(line) < 3:
continue
text = ' '.join(line[2:])
#trim out long lines
if len(text) > 100:
text = text[:100]
#compute diff
time = float(line[1])
diff = time - prev_time if prev_time is not None else 0
total += diff
prev_time = time
heapq.heappush(q, (diff, text))
for diff, text in heapq.nlargest(n, q):
print diff, 's:', text
print 'Total loading time[s]: ', total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment