-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGtPyinstrumentProfiler.class.st
More file actions
101 lines (92 loc) · 2.75 KB
/
GtPyinstrumentProfiler.class.st
File metadata and controls
101 lines (92 loc) · 2.75 KB
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
Class {
#name : #GtPyinstrumentProfiler,
#superclass : #Object,
#instVars : [
'projectDirectory',
'model',
'fileCache'
],
#category : #GToolkit4Python
}
{ #category : #accessing }
GtPyinstrumentProfiler class >> profile: program withArgs: anArray in: aDirectory [
^ self
profile: program
withArgs: anArray
workingDirectory: aDirectory
sourceDirectory: aDirectory
]
{ #category : #accessing }
GtPyinstrumentProfiler class >> profile: program withArgs: anArray workingDirectory: aDirectory sourceDirectory: sourceDirectory [
| filename |
filename := (UUID new asString36 , '.prof') asFileReference.
GtSubprocessWithInMemoryOutput new
workingDirectory: aDirectory;
command: 'pyinstrument';
arguments: {'--show-all'.
'--outfile=' , filename pathString.
'--renderer=json'.
program} , anArray;
errorBlock: [ :theProcess | self error: 'Execution failed' ];
runAndWait.
^ [ self new importProfile: filename forProject: sourceDirectory ]
ensure: [ filename delete ]
]
{ #category : #accessing }
GtPyinstrumentProfiler >> createTally: aDictionary [
| class method tally |
class := self fileFor: aDictionary.
method := class
methodNamed: (aDictionary at: 'function')
line: (aDictionary at: 'line_no').
tally := GtTally new
class: class method: method;
bumpBy: ((aDictionary at: 'time') * 1000000) rounded;
yourself.
(aDictionary at: 'children')
do: [ :each |
(each at: 'function') = '[self]'
ifFalse: [ tally addReceiver: (self createTally: each) ] ].
^ tally
]
{ #category : #accessing }
GtPyinstrumentProfiler >> fileFor: aDictionary [
| fullname |
fullname := aDictionary at: 'file_path'.
fullname trim isEmpty ifTrue: [ fullname := aDictionary at: 'file_path_short' ].
^ fileCache
at: fullname
ifAbsentPut: [ GtTallyPythonFile new
fullName: fullname;
name: (aDictionary at: 'file_path_short');
file: (self lookupFileInModel: fullname);
yourself ]
]
{ #category : #accessing }
GtPyinstrumentProfiler >> importProfile: aFileRef forProject: projFileRef [
| json |
model := GtLSPPythonModel onDirectory: projFileRef asFileReference.
json := STONJSON fromString: aFileRef asFileReference contents.
^ self createTally: (json at: 'root_frame')
]
{ #category : #accessing }
GtPyinstrumentProfiler >> initialize [
super initialize.
fileCache := Dictionary new
]
{ #category : #accessing }
GtPyinstrumentProfiler >> lookupFileInModel: aString [
| ref |
ref := (aString beginsWith: '.')
ifTrue: [ model filename / aString ]
ifFalse: [ aString asFileReference ].
^ (model fileFor: ref) ifNil: [ model fileFor: model filename / aString ]
]
{ #category : #accessing }
GtPyinstrumentProfiler >> model [
^ model
]
{ #category : #accessing }
GtPyinstrumentProfiler >> model: anObject [
model := anObject
]