blob: fe35e49c2ac60f6babd32848be4ff8eca38504a2 [file] [log] [blame]
Bram Moolenaar296b1f22017-01-15 15:22:33 +01001" Test Vim profiler
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02002
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003CheckFeature profile
Bram Moolenaar296b1f22017-01-15 15:22:33 +01004
Christian Brabandteb380b92025-07-07 20:53:55 +02005source util/screendump.vim
Bram Moolenaar93344c22019-08-14 21:12:05 +02006
Ernie Rael076de792023-03-16 21:43:15 +00007if has('prof_nsec')
8 let s:header = 'count total (s) self (s)'
9 let s:header_func = 'count total (s) self (s) function'
10else
11 let s:header = 'count total (s) self (s)'
12 let s:header_func = 'count total (s) self (s) function'
13endif
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000014
Bram Moolenaar296b1f22017-01-15 15:22:33 +010015func Test_profile_func()
Bram Moolenaarb2049902021-01-24 12:53:53 +010016 call RunProfileFunc('func', 'let', 'let')
17 call RunProfileFunc('def', 'var', '')
18endfunc
19
20func RunProfileFunc(command, declare, assign)
Bram Moolenaarc79745a2019-05-20 22:12:34 +020021 let lines =<< trim [CODE]
22 profile start Xprofile_func.log
23 profile func Foo*
Bram Moolenaarb2049902021-01-24 12:53:53 +010024 XXX Foo1()
25 endXXX
26 XXX Foo2()
27 DDD counter = 100
28 while counter > 0
29 AAA counter = counter - 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +020030 endwhile
Bram Moolenaara21df1d2020-03-17 20:57:09 +010031 sleep 1m
Bram Moolenaarb2049902021-01-24 12:53:53 +010032 endXXX
33 XXX Foo3()
34 endXXX
35 XXX Bar()
36 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +020037 call Foo1()
38 call Foo1()
39 profile pause
40 call Foo1()
41 profile continue
42 call Foo2()
43 call Foo3()
44 call Bar()
45 if !v:profiling
46 delfunc Foo2
47 endif
48 delfunc Foo3
49 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +010050
Bram Moolenaarb2049902021-01-24 12:53:53 +010051 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
52 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
53 call map(lines, {k, v -> substitute(v, 'AAA', a:assign, '') })
54
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010055 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +020056 call system(GetVimCommand()
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +020057 \ . ' -es --clean'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010058 \ . ' -c "so Xprofile_func.vim"'
59 \ . ' -c "qall!"')
60 call assert_equal(0, v:shell_error)
61
K.Takata0500e872022-09-08 12:28:02 +010062 sleep 50m
Bram Moolenaar296b1f22017-01-15 15:22:33 +010063 let lines = readfile('Xprofile_func.log')
64
Bram Moolenaar296b1f22017-01-15 15:22:33 +010065 " - Foo1() is called 3 times but should be reported as called twice
66 " since one call is in between "profile pause" .. "profile continue".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010067 " - Foo2() should come before Foo1() since Foo1() does much more work.
Bram Moolenaar296b1f22017-01-15 15:22:33 +010068 " - Foo3() is not reported because function is deleted.
69 " - Unlike Foo3(), Foo2() should not be deleted since there is a check
70 " for v:profiling.
71 " - Bar() is not reported since it does not match "profile func Foo*".
Bram Moolenaara21df1d2020-03-17 20:57:09 +010072 call assert_equal(31, len(lines))
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010073
74 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar181d4f52019-09-18 22:04:56 +020075 call assert_match('Defined:.*Xprofile_func.vim:3', lines[1])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020076 call assert_equal('Called 2 times', lines[2])
77 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
78 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
79 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000080 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020081 call assert_equal('', lines[7])
82 call assert_equal('FUNCTION Foo2()', lines[8])
83 call assert_equal('Called 1 time', lines[10])
84 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[11])
85 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[12])
86 call assert_equal('', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000087 call assert_equal(s:header, lines[14])
Bram Moolenaarb2049902021-01-24 12:53:53 +010088 call assert_match('^\s*1\s\+.*\s\(let\|var\) counter = 100$', lines[15])
89 call assert_match('^\s*101\s\+.*\swhile counter > 0$', lines[16])
90 call assert_match('^\s*100\s\+.*\s \(let\)\= counter = counter - 1$', lines[17])
91 call assert_match('^\s*10[01]\s\+.*\sendwhile$', lines[18])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010092 call assert_match('^\s*1\s\+.\+sleep 1m$', lines[19])
93 call assert_equal('', lines[20])
94 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[21])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000095 call assert_equal(s:header_func, lines[22])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010096 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[23])
97 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[24])
98 call assert_equal('', lines[25])
99 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[26])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000100 call assert_equal(s:header_func, lines[27])
Bram Moolenaara21df1d2020-03-17 20:57:09 +0100101 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[28])
102 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[29])
103 call assert_equal('', lines[30])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100104
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100105 call delete('Xprofile_func.log')
106endfunc
107
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200108func Test_profile_func_with_ifelse()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100109 call Run_profile_func_with_ifelse('func', 'let')
110 call Run_profile_func_with_ifelse('def', 'var')
Bram Moolenaarced68a02021-01-24 17:53:47 +0100111endfunc
112
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100113func Run_profile_func_with_ifelse(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200114 let lines =<< trim [CODE]
Bram Moolenaarced68a02021-01-24 17:53:47 +0100115 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200116 if 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100117 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200118 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100119 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200120 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100121 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200122 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100123 endXXX
124 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200125 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100126 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200127 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100128 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200129 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100130 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200131 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100132 endXXX
133 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200134 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100135 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200136 elseif 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100137 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200138 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100139 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200140 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100141 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200142 call Foo1()
143 call Foo2()
144 call Foo3()
145 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200146
Bram Moolenaarced68a02021-01-24 17:53:47 +0100147 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
148 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
Bram Moolenaarced68a02021-01-24 17:53:47 +0100149
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100150 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200151 call system(GetVimCommand()
152 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200153 \ . ' -c "profile start Xprofile_func.log"'
154 \ . ' -c "profile func Foo*"'
155 \ . ' -c "so Xprofile_func.vim"'
156 \ . ' -c "qall!"')
157 call assert_equal(0, v:shell_error)
158
159 let lines = readfile('Xprofile_func.log')
160
161 " - Foo1() should pass 'if' block.
162 " - Foo2() should pass 'elseif' block.
163 " - Foo3() should pass 'else' block.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200164 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200165
166 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200167 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
168 call assert_equal('Called 1 time', lines[2])
169 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
170 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
171 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000172 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200173 call assert_match('^\s*1\s\+.*\sif 1$', lines[7])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100174 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200175 call assert_match( '^\s\+elseif 1$', lines[9])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100176 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200177 call assert_match( '^\s\+else$', lines[11])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100178 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200179 call assert_match('^\s*1\s\+.*\sendif$', lines[13])
180 call assert_equal('', lines[14])
181 call assert_equal('FUNCTION Foo2()', lines[15])
182 call assert_equal('Called 1 time', lines[17])
183 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
184 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
185 call assert_equal('', lines[20])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000186 call assert_equal(s:header, lines[21])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200187 call assert_match('^\s*1\s\+.*\sif 0$', lines[22])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100188 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[23])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200189 call assert_match('^\s*1\s\+.*\selseif 1$', lines[24])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100190 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200191 call assert_match( '^\s\+else$', lines[26])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100192 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200193 call assert_match('^\s*1\s\+.*\sendif$', lines[28])
194 call assert_equal('', lines[29])
195 call assert_equal('FUNCTION Foo3()', lines[30])
196 call assert_equal('Called 1 time', lines[32])
197 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
198 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
199 call assert_equal('', lines[35])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000200 call assert_equal(s:header, lines[36])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200201 call assert_match('^\s*1\s\+.*\sif 0$', lines[37])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100202 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[38])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200203 call assert_match('^\s*1\s\+.*\selseif 0$', lines[39])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100204 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[40])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200205 call assert_match('^\s*1\s\+.*\selse$', lines[41])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100206 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200207 call assert_match('^\s*1\s\+.*\sendif$', lines[43])
208 call assert_equal('', lines[44])
209 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000210 call assert_equal(s:header_func, lines[46])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200211 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
212 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
213 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
214 call assert_equal('', lines[50])
215 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000216 call assert_equal(s:header_func, lines[52])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200217 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
218 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
219 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
220 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200221
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200222 call delete('Xprofile_func.log')
223endfunc
224
225func Test_profile_func_with_trycatch()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100226 call Run_profile_func_with_trycatch('func', 'let')
227 call Run_profile_func_with_trycatch('def', 'var')
228endfunc
229
230func Run_profile_func_with_trycatch(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200231 let lines =<< trim [CODE]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100232 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200233 try
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100234 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200235 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100236 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200237 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100238 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200239 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100240 endXXX
241 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200242 try
243 throw 0
244 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100245 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200246 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100247 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200248 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100249 endXXX
250 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200251 try
252 throw 0
253 catch
254 throw 1
255 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100256 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200257 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100258 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200259 call Foo1()
260 call Foo2()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100261 let rethrown = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200262 try
263 call Foo3()
264 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100265 let rethrown = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200266 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100267 if rethrown != 1
268 " call Foo1 again so that the test fails
269 call Foo1()
270 endif
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200271 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200272
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100273 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
274 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
275
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100276 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200277 call system(GetVimCommand()
278 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200279 \ . ' -c "profile start Xprofile_func.log"'
280 \ . ' -c "profile func Foo*"'
281 \ . ' -c "so Xprofile_func.vim"'
282 \ . ' -c "qall!"')
283 call assert_equal(0, v:shell_error)
284
285 let lines = readfile('Xprofile_func.log')
286
287 " - Foo1() should pass 'try' 'finally' blocks.
288 " - Foo2() should pass 'catch' 'finally' blocks.
289 " - Foo3() should not pass 'endtry'.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200290 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200291
292 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200293 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
294 call assert_equal('Called 1 time', lines[2])
295 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
296 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
297 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000298 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200299 call assert_match('^\s*1\s\+.*\stry$', lines[7])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100300 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200301 call assert_match( '^\s\+catch$', lines[9])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100302 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200303 call assert_match('^\s*1\s\+.*\sfinally$', lines[11])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100304 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200305 call assert_match('^\s*1\s\+.*\sendtry$', lines[13])
306 call assert_equal('', lines[14])
307 call assert_equal('FUNCTION Foo2()', lines[15])
308 call assert_equal('Called 1 time', lines[17])
309 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
310 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
311 call assert_equal('', lines[20])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000312 call assert_equal(s:header, lines[21])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200313 call assert_match('^\s*1\s\+.*\stry$', lines[22])
314 call assert_match('^\s*1\s\+.*\s throw 0$', lines[23])
315 call assert_match('^\s*1\s\+.*\scatch$', lines[24])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100316 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200317 call assert_match('^\s*1\s\+.*\sfinally$', lines[26])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100318 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200319 call assert_match('^\s*1\s\+.*\sendtry$', lines[28])
320 call assert_equal('', lines[29])
321 call assert_equal('FUNCTION Foo3()', lines[30])
322 call assert_equal('Called 1 time', lines[32])
323 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
324 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
325 call assert_equal('', lines[35])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000326 call assert_equal(s:header, lines[36])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200327 call assert_match('^\s*1\s\+.*\stry$', lines[37])
328 call assert_match('^\s*1\s\+.*\s throw 0$', lines[38])
329 call assert_match('^\s*1\s\+.*\scatch$', lines[39])
330 call assert_match('^\s*1\s\+.*\s throw 1$', lines[40])
331 call assert_match('^\s*1\s\+.*\sfinally$', lines[41])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100332 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200333 call assert_match( '^\s\+endtry$', lines[43])
334 call assert_equal('', lines[44])
335 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000336 call assert_equal(s:header_func, lines[46])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200337 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
338 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
339 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
340 call assert_equal('', lines[50])
341 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000342 call assert_equal(s:header_func, lines[52])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200343 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
344 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
345 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
346 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200347
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200348 call delete('Xprofile_func.log')
349endfunc
350
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100351func Test_profile_file()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200352 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200353 func! Foo()
354 endfunc
355 for i in range(10)
356 " a comment
357 call Foo()
358 endfor
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200359 call Foo()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200360 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100361
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100362 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200363 call system(GetVimCommandClean()
364 \ . ' -es'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +0100365 \ . ' -c "profile start Xprofile_file.log"'
366 \ . ' -c "profile file Xprofile_file.vim"'
367 \ . ' -c "so Xprofile_file.vim"'
368 \ . ' -c "so Xprofile_file.vim"'
369 \ . ' -c "qall!"')
370 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100371
372 let lines = readfile('Xprofile_file.log')
373
374 call assert_equal(14, len(lines))
375
376 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
377 call assert_equal('Sourced 2 times', lines[1])
378 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
379 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
380 call assert_equal('', lines[4])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000381 call assert_equal(s:header, lines[5])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200382 call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100383 call assert_equal(' endfunc', lines[7])
384 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
385 " First line of loop executes one more time than body to detect end of loop.
386 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
387 call assert_equal(' " a comment', lines[9])
Bram Moolenaard21b16f2017-08-14 23:13:30 +0200388 " if self and total are equal we only get one number
389 call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200390 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100391 " if self and total are equal we only get one number
392 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100393 call assert_equal('', lines[13])
394
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100395 call delete('Xprofile_file.log')
396endfunc
397
Bram Moolenaar67435d92017-10-19 21:04:37 +0200398func Test_profile_file_with_cont()
399 let lines = [
400 \ 'echo "hello',
401 \ ' \ world"',
402 \ 'echo "foo ',
403 \ ' \bar"',
404 \ ]
405
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100406 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200407 call system(GetVimCommandClean()
408 \ . ' -es'
Bram Moolenaar67435d92017-10-19 21:04:37 +0200409 \ . ' -c "profile start Xprofile_file.log"'
410 \ . ' -c "profile file Xprofile_file.vim"'
411 \ . ' -c "so Xprofile_file.vim"'
412 \ . ' -c "qall!"')
413 call assert_equal(0, v:shell_error)
414
415 let lines = readfile('Xprofile_file.log')
416 call assert_equal(11, len(lines))
417
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200418 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
419 call assert_equal('Sourced 1 time', lines[1])
420 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
421 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
422 call assert_equal('', lines[4])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000423 call assert_equal(s:header, lines[5])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200424 call assert_match(' 1 0.\d\+ echo "hello', lines[6])
425 call assert_equal(' \ world"', lines[7])
426 call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
427 call assert_equal(' \bar"', lines[9])
428 call assert_equal('', lines[10])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200429
Bram Moolenaar67435d92017-10-19 21:04:37 +0200430 call delete('Xprofile_file.log')
431endfunc
432
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100433" Test for ':profile stop' and ':profile dump' commands
434func Test_profile_stop_dump()
435 call delete('Xprof1.out')
436 call delete('Xprof2.out')
437 call delete('Xprof3.out')
438 func Xprof_test1()
439 return "Hello"
440 endfunc
441 func Xprof_test2()
442 return "World"
443 endfunc
444
445 " Test for ':profile stop'
446 profile start Xprof1.out
447 profile func Xprof_test1
448 call Xprof_test1()
449 profile stop
450
451 let lines = readfile('Xprof1.out')
452 call assert_equal(17, len(lines))
453 call assert_equal('FUNCTION Xprof_test1()', lines[0])
454 call assert_match('Defined:.*test_profile.vim:', lines[1])
455 call assert_equal('Called 1 time', lines[2])
456 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
457 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
458 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000459 call assert_equal(s:header, lines[6])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100460 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
461 call assert_equal('', lines[8])
462 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000463 call assert_equal(s:header_func, lines[10])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100464 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
465 call assert_equal('', lines[12])
466 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000467 call assert_equal(s:header_func, lines[14])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100468 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
469 call assert_equal('', lines[16])
470
471 " Test for ':profile stop' for a different function
472 profile start Xprof2.out
473 profile func Xprof_test2
474 call Xprof_test2()
475 profile stop
476 let lines = readfile('Xprof2.out')
477 call assert_equal(17, len(lines))
478 call assert_equal('FUNCTION Xprof_test2()', lines[0])
479 call assert_match('Defined:.*test_profile.vim:', lines[1])
480 call assert_equal('Called 1 time', lines[2])
481 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
482 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
483 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000484 call assert_equal(s:header, lines[6])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100485 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[7])
486 call assert_equal('', lines[8])
487 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000488 call assert_equal(s:header_func, lines[10])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100489 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[11])
490 call assert_equal('', lines[12])
491 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000492 call assert_equal(s:header_func, lines[14])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100493 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[15])
494 call assert_equal('', lines[16])
495
496 " Test for ':profile dump'
497 profile start Xprof3.out
498 profile func Xprof_test1
499 profile func Xprof_test2
500 call Xprof_test1()
501 profile dump
502 " dump the profile once and verify the contents
503 let lines = readfile('Xprof3.out')
504 call assert_equal(17, len(lines))
505 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
506 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
507 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
508 " dump the profile again and verify the contents
509 call Xprof_test2()
510 profile dump
511 profile stop
512 let lines = readfile('Xprof3.out')
513 call assert_equal(28, len(lines))
514 call assert_equal('FUNCTION Xprof_test1()', lines[0])
515 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
516 call assert_equal('FUNCTION Xprof_test2()', lines[9])
517 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[16])
518
519 delfunc Xprof_test1
520 delfunc Xprof_test2
521 call delete('Xprof1.out')
522 call delete('Xprof2.out')
523 call delete('Xprof3.out')
524endfunc
525
526" Test for :profile sub-command completion
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100527func Test_profile_completion()
528 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100529 call assert_equal('"profile continue dump file func pause start stop', @:)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100530
531 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
532 call assert_match('^"profile start.* test_profile\.vim', @:)
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000533
534 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
535 call assert_match('"profile file test_profile\.vim', @:)
536 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
537 call assert_match('"profile file test_profile\.vim', @:)
538 call feedkeys(":profile file test_prof \<Tab>\<C-B>\"\<CR>", 'tx')
539 call assert_match('"profile file test_prof ', @:)
540 call feedkeys(":profile file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
541 call assert_match('"profile file X1B2C3', @:)
542
543 func Xprof_test()
544 endfunc
545 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
546 call assert_equal('"profile func Xprof_test', @:)
547 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
548 call assert_equal('"profile func Xprof_test', @:)
549 call feedkeys(":profile func Xprof \<Tab>\<C-B>\"\<CR>", 'tx')
550 call assert_equal('"profile func Xprof ', @:)
551 call feedkeys(":profile func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
552 call assert_equal('"profile func X1B2C3', @:)
553
554 call feedkeys(":profdel \<C-A>\<C-B>\"\<CR>", 'tx')
555 call assert_equal('"profdel file func', @:)
556 call feedkeys(":profdel fu\<Tab>\<C-B>\"\<CR>", 'tx')
557 call assert_equal('"profdel func', @:)
558 call feedkeys(":profdel he\<Tab>\<C-B>\"\<CR>", 'tx')
559 call assert_equal('"profdel he', @:)
560 call feedkeys(":profdel here \<Tab>\<C-B>\"\<CR>", 'tx')
561 call assert_equal('"profdel here ', @:)
562 call feedkeys(":profdel file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
563 call assert_equal('"profdel file test_profile.vim', @:)
564 call feedkeys(":profdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
565 call assert_equal('"profdel file X1B2C3', @:)
566 call feedkeys(":profdel func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
567 call assert_equal('"profdel func Xprof_test', @:)
568 call feedkeys(":profdel func Xprof_test \<Tab>\<C-B>\"\<CR>", 'tx')
569 call assert_equal('"profdel func Xprof_test ', @:)
570 call feedkeys(":profdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
571 call assert_equal('"profdel func X1B2C3', @:)
572
573 delfunc Xprof_test
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100574endfunc
575
576func Test_profile_errors()
577 call assert_fails("profile func Foo", 'E750:')
578 call assert_fails("profile pause", 'E750:')
579 call assert_fails("profile continue", 'E750:')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100580 call assert_fails("profile stop", 'E750:')
581 call assert_fails("profile dump", 'E750:')
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100582endfunc
Bram Moolenaarac112f02017-12-05 16:46:28 +0100583
584func Test_profile_truncate_mbyte()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100585 if &enc !=# 'utf-8'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100586 return
587 endif
588
589 let lines = [
590 \ 'scriptencoding utf-8',
591 \ 'func! Foo()',
592 \ ' return [',
593 \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
594 \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
595 \ ' \ ]',
596 \ 'endfunc',
597 \ 'call Foo()',
598 \ ]
599
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100600 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200601 call system(GetVimCommandClean()
602 \ . ' -es --cmd "set enc=utf-8"'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100603 \ . ' -c "profile start Xprofile_file.log"'
604 \ . ' -c "profile file Xprofile_file.vim"'
605 \ . ' -c "so Xprofile_file.vim"'
606 \ . ' -c "qall!"')
607 call assert_equal(0, v:shell_error)
608
609 split Xprofile_file.log
610 if &fenc != ''
611 call assert_equal('utf-8', &fenc)
612 endif
613 /func! Foo()
614 let lnum = line('.')
615 call assert_match('^\s*return \[$', getline(lnum + 1))
616 call assert_match("\u4F52$", getline(lnum + 2))
617 call assert_match("\u5052$", getline(lnum + 3))
618 call assert_match('^\s*\\ \]$', getline(lnum + 4))
619 bwipe!
620
Bram Moolenaarac112f02017-12-05 16:46:28 +0100621 call delete('Xprofile_file.log')
622endfunc
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200623
624func Test_profdel_func()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200625 let lines =<< trim [CODE]
626 profile start Xprofile_file.log
627 func! Foo1()
628 endfunc
629 func! Foo2()
630 endfunc
631 func! Foo3()
632 endfunc
633
634 profile func Foo1
635 profile func Foo2
636 call Foo1()
637 call Foo2()
638
639 profile func Foo3
640 profdel func Foo2
641 profdel func Foo3
642 call Foo1()
643 call Foo2()
644 call Foo3()
645 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100646 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200647 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200648 call assert_equal(0, v:shell_error)
649
650 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200651 call assert_equal(26, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200652
653 " Check that:
654 " - Foo1() is called twice (profdel not invoked)
655 " - Foo2() is called once (profdel invoked after it was called)
656 " - Foo3() is not called (profdel invoked before it was called)
657 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200658 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
659 call assert_equal('Called 2 times', lines[2])
660 call assert_equal('FUNCTION Foo2()', lines[8])
661 call assert_equal('Called 1 time', lines[10])
662 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16])
663 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200664
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200665 call delete('Xprofile_file.log')
666endfunc
667
668func Test_profdel_star()
669 " Foo() is invoked once before and once after 'profdel *'.
670 " So profiling should report it only once.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200671 let lines =<< trim [CODE]
672 profile start Xprofile_file.log
673 func! Foo()
674 endfunc
675 profile func Foo
676 call Foo()
677 profdel *
678 call Foo()
679 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100680 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200681 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200682 call assert_equal(0, v:shell_error)
683
684 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200685 call assert_equal(16, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200686
687 call assert_equal('FUNCTION Foo()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200688 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
689 call assert_equal('Called 1 time', lines[2])
690 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8])
691 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200692
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200693 call delete('Xprofile_file.log')
694endfunc
Bram Moolenaar16358802019-08-30 18:37:26 +0200695
696" When typing the function it won't have a script ID, test that this works.
697func Test_profile_typed_func()
698 CheckScreendump
699
700 let lines =<< trim END
701 profile start XprofileTypedFunc
702 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100703 call writefile(lines, 'XtestProfile', 'D')
Bram Moolenaar16358802019-08-30 18:37:26 +0200704 let buf = RunVimInTerminal('-S XtestProfile', #{})
705
706 call term_sendkeys(buf, ":func DoSomething()\<CR>"
707 \ .. "echo 'hello'\<CR>"
708 \ .. "endfunc\<CR>")
709 call term_sendkeys(buf, ":profile func DoSomething\<CR>")
710 call term_sendkeys(buf, ":call DoSomething()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200711 call TermWait(buf, 100)
Bram Moolenaar16358802019-08-30 18:37:26 +0200712 call StopVimInTerminal(buf)
713 let lines = readfile('XprofileTypedFunc')
714 call assert_equal("FUNCTION DoSomething()", lines[0])
715 call assert_equal("Called 1 time", lines[1])
716
717 " clean up
718 call delete('XprofileTypedFunc')
Bram Moolenaar16358802019-08-30 18:37:26 +0200719endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200720
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100721func Test_vim9_profiling()
722 " only tests that compiling and calling functions doesn't crash
723 let lines =<< trim END
724 vim9script
725 def Func()
726 Crash()
727 enddef
728 def Crash()
729 enddef
Bram Moolenaar98989a02021-01-26 12:06:30 +0100730 prof start Xprofile_crash.log
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100731 prof func Func
732 Func()
733 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100734 call writefile(lines, 'Xprofile_crash.vim', 'D')
Bram Moolenaar98989a02021-01-26 12:06:30 +0100735 call system(GetVimCommandClean() . ' -es -c "so Xprofile_crash.vim" -c q')
736 call assert_equal(0, v:shell_error)
Bram Moolenaar8c801b32021-03-05 20:58:22 +0100737 call assert_true(readfile('Xprofile_crash.log')->len() > 10)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100738
Bram Moolenaar98989a02021-01-26 12:06:30 +0100739 call delete('Xprofile_crash.log')
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100740endfunc
741
Bram Moolenaar12d26532021-02-19 19:13:21 +0100742func Test_vim9_nested_call()
743 let lines =<< trim END
744 vim9script
745 var total = 0
746 def One(Ref: func(number))
747 for i in range(3)
748 Ref(i)
749 endfor
750 enddef
751 def Two(nr: number)
752 total += nr
753 enddef
754 prof start Xprofile_nested.log
755 prof func One
756 prof func Two
757 One((nr) => Two(nr))
758 assert_equal(3, total)
759 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100760 call writefile(lines, 'Xprofile_nested.vim', 'D')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100761 call system(GetVimCommandClean() . ' -es -c "so Xprofile_nested.vim" -c q')
762 call assert_equal(0, v:shell_error)
763
764 let prof_lines = readfile('Xprofile_nested.log')->join('#')
765 call assert_match('FUNCTION <SNR>\d\+_One().*'
766 \ .. '#Called 1 time.*'
767 \ .. '# 1 \s*[0-9.]\+ for i in range(3)'
768 \ .. '# 3 \s*[0-9.]\+ \s*[0-9.]\+ Ref(i)'
769 \ .. '# 3 \s*[0-9.]\+ endfor', prof_lines)
770 call assert_match('FUNCTION <SNR>\d\+_Two().*'
771 \ .. '#Called 3 times.*'
772 \ .. '# 3 \s*[0-9.]\+ total += nr', prof_lines)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100773
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100774 call delete('Xprofile_nested.log')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100775endfunc
776
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100777
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200778" vim: shiftwidth=2 sts=2 expandtab