File: gtkprof.py

package info (click to toggle)
python-gtk 0.5.3-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,604 kB
  • ctags: 4,295
  • sloc: ansic: 19,390; python: 8,220; makefile: 91; sh: 26
file content (133 lines) | stat: -rwxr-xr-x 3,046 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

import profile, pstats, fpformat

from Gtkinter import *

class PStatWindow(GtkWindow):
	def __init__(self, stats):
		GtkWindow.__init__(self)
		self.connect("destroy", self.quit)
		self.connect("delete_event", self.quit)
		self.set_title("Profile Statistics")

		self.stats = stats

		box1 = GtkVBox()
		self.add(box1)
		box1.show()

		text = `stats.total_calls` + " function calls "
		if stats.total_calls != stats.prim_calls:
			text = text + "( " + `stats.prim_calls` + \
			       " primitive calls) "
		text = text + "in " + fpformat.fix(stats.total_tt, 3) + \
		       " CPU seconds"
		label = GtkLabel(text)
		label.set_padding(2, 2)
		box1.pack_start(label, expand=FALSE)
		label.show()

		titles = ['ncalls', 'tottime', 'percall', 'cumtime',
			  'percall', 'filename:lineno(function)']
		clist = GtkCList(len(titles), titles)
		clist.set_column_width(0, 40)
		clist.set_column_width(1, 50)
		clist.set_column_width(2, 50)
		clist.set_column_width(3, 50)
		clist.set_column_width(4, 50)
		clist.set_usize(500, 200)
		clist.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
		self.clist = clist
		clist.border_width(10)
		box1.pack_start(clist)
		clist.show()

		for i in range(5):
			clist.set_column_justification(i, JUSTIFY_RIGHT)

		self.insert_stats()

		separator = GtkHSeparator()
		box1.pack_start(separator, expand=FALSE)
		separator.show()

		box2 = GtkVBox(spacing=10)
		box2.border_width(10)
		box1.pack_start(box2, expand=FALSE)
		box2.show()

		button = GtkButton("close")
		button.connect("clicked", self.quit)
		self.close_button = button
		box2.pack_start(button)
		button.set_flags(CAN_DEFAULT)
		button.grab_default()
		button.show()

	def quit(self, *args):
		self.hide()
		self.destroy()
		mainquit()

	def get_stats_list(self):
		if self.stats.fcn_list:
			return self.stats.fcn_list[:]
		else:
			return sekf.stats.stats.keys()

	def insert_stats(self):
		list = self.get_stats_list()
		if list:
			row = [None] * 6
			self.clist.clear()
			self.clist.freeze()
			for func in list:
				cc,nc,tt,ct,callers = self.stats.stats[func]
				row[0] = `nc`
				if nc != cc:
					row[0] = row[0] + '/' + `cc`
				row[1] = fpformat.fix(tt, 3)
				if nc == 0:
					row[2] = ''
				else:
					row[2] = fpformat.fix(tt/nc, 3)
				row[3] = fpformat.fix(ct, 3)
				if cc == 0:
					row[4] = ''
				else:
					row[4] = fpformat.fix(ct/cc, 3)
				file,line,name = func
				row[5] = file + ":" + `line` + "(" + name + \
					 ")"
				self.clist.append(row)
			self.clist.thaw()
				

def run(cmd):
        prof = profile.Profile()
	try:
		stats = pstats.Stats(prof.run(cmd))
	except SystemExit:
		pass
	stats.strip_dirs().sort_stats("time", "module", "name")
	win = PStatWindow(stats)
	win.show()
	mainloop()

def run_file(file):
	return run('execfile("' + file + '")')


if __name__ == '__main__':
	import sys, os
	if not sys.argv[1:]:
		print "usage: gtkprof.py scriptfile [args ...]"
		sys.exit(2)
	filename = sys.argv[1]
	del sys.argv[0]
	sys.path.insert(0, os.path.dirname(filename))

	run_file(filename)