blob: d24acd30d4107736247e3527ef48806430ff62b1 [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 +02003source check.vim
4CheckFeature profile
Bram Moolenaar296b1f22017-01-15 15:22:33 +01005
Bram Moolenaar93344c22019-08-14 21:12:05 +02006source shared.vim
Bram Moolenaar16358802019-08-30 18:37:26 +02007source screendump.vim
Bram Moolenaar93344c22019-08-14 21:12:05 +02008
Bram Moolenaar296b1f22017-01-15 15:22:33 +01009func Test_profile_func()
Bram Moolenaarb2049902021-01-24 12:53:53 +010010 call RunProfileFunc('func', 'let', 'let')
11 call RunProfileFunc('def', 'var', '')
12endfunc
13
14func RunProfileFunc(command, declare, assign)
Bram Moolenaarc79745a2019-05-20 22:12:34 +020015 let lines =<< trim [CODE]
16 profile start Xprofile_func.log
17 profile func Foo*
Bram Moolenaarb2049902021-01-24 12:53:53 +010018 XXX Foo1()
19 endXXX
20 XXX Foo2()
21 DDD counter = 100
22 while counter > 0
23 AAA counter = counter - 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +020024 endwhile
Bram Moolenaara21df1d2020-03-17 20:57:09 +010025 sleep 1m
Bram Moolenaarb2049902021-01-24 12:53:53 +010026 endXXX
27 XXX Foo3()
28 endXXX
29 XXX Bar()
30 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +020031 call Foo1()
32 call Foo1()
33 profile pause
34 call Foo1()
35 profile continue
36 call Foo2()
37 call Foo3()
38 call Bar()
39 if !v:profiling
40 delfunc Foo2
41 endif
42 delfunc Foo3
43 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +010044
Bram Moolenaarb2049902021-01-24 12:53:53 +010045 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
46 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
47 call map(lines, {k, v -> substitute(v, 'AAA', a:assign, '') })
48
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010049 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +020050 call system(GetVimCommand()
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +020051 \ . ' -es --clean'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010052 \ . ' -c "so Xprofile_func.vim"'
53 \ . ' -c "qall!"')
54 call assert_equal(0, v:shell_error)
55
K.Takata0500e872022-09-08 12:28:02 +010056 sleep 50m
Bram Moolenaar296b1f22017-01-15 15:22:33 +010057 let lines = readfile('Xprofile_func.log')
58
Bram Moolenaar296b1f22017-01-15 15:22:33 +010059 " - Foo1() is called 3 times but should be reported as called twice
60 " since one call is in between "profile pause" .. "profile continue".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010061 " - Foo2() should come before Foo1() since Foo1() does much more work.
Bram Moolenaar296b1f22017-01-15 15:22:33 +010062 " - Foo3() is not reported because function is deleted.
63 " - Unlike Foo3(), Foo2() should not be deleted since there is a check
64 " for v:profiling.
65 " - Bar() is not reported since it does not match "profile func Foo*".
Bram Moolenaara21df1d2020-03-17 20:57:09 +010066 call assert_equal(31, len(lines))
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010067
68 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar181d4f52019-09-18 22:04:56 +020069 call assert_match('Defined:.*Xprofile_func.vim:3', lines[1])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020070 call assert_equal('Called 2 times', lines[2])
71 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
72 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
73 call assert_equal('', lines[5])
74 call assert_equal('count total (s) self (s)', lines[6])
75 call assert_equal('', lines[7])
76 call assert_equal('FUNCTION Foo2()', lines[8])
77 call assert_equal('Called 1 time', lines[10])
78 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[11])
79 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[12])
80 call assert_equal('', lines[13])
81 call assert_equal('count total (s) self (s)', lines[14])
Bram Moolenaarb2049902021-01-24 12:53:53 +010082 call assert_match('^\s*1\s\+.*\s\(let\|var\) counter = 100$', lines[15])
83 call assert_match('^\s*101\s\+.*\swhile counter > 0$', lines[16])
84 call assert_match('^\s*100\s\+.*\s \(let\)\= counter = counter - 1$', lines[17])
85 call assert_match('^\s*10[01]\s\+.*\sendwhile$', lines[18])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010086 call assert_match('^\s*1\s\+.\+sleep 1m$', lines[19])
87 call assert_equal('', lines[20])
88 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[21])
89 call assert_equal('count total (s) self (s) function', lines[22])
90 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[23])
91 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[24])
92 call assert_equal('', lines[25])
93 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[26])
94 call assert_equal('count total (s) self (s) function', lines[27])
95 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[28])
96 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[29])
97 call assert_equal('', lines[30])
Bram Moolenaar296b1f22017-01-15 15:22:33 +010098
Bram Moolenaar296b1f22017-01-15 15:22:33 +010099 call delete('Xprofile_func.log')
100endfunc
101
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200102func Test_profile_func_with_ifelse()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100103 call Run_profile_func_with_ifelse('func', 'let')
104 call Run_profile_func_with_ifelse('def', 'var')
Bram Moolenaarced68a02021-01-24 17:53:47 +0100105endfunc
106
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100107func Run_profile_func_with_ifelse(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200108 let lines =<< trim [CODE]
Bram Moolenaarced68a02021-01-24 17:53:47 +0100109 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200110 if 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100111 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200112 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100113 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200114 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100115 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200116 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100117 endXXX
118 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200119 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100120 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200121 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100122 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200123 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100124 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200125 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100126 endXXX
127 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200128 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100129 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200130 elseif 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100131 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200132 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100133 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200134 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100135 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200136 call Foo1()
137 call Foo2()
138 call Foo3()
139 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200140
Bram Moolenaarced68a02021-01-24 17:53:47 +0100141 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
142 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
Bram Moolenaarced68a02021-01-24 17:53:47 +0100143
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100144 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200145 call system(GetVimCommand()
146 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200147 \ . ' -c "profile start Xprofile_func.log"'
148 \ . ' -c "profile func Foo*"'
149 \ . ' -c "so Xprofile_func.vim"'
150 \ . ' -c "qall!"')
151 call assert_equal(0, v:shell_error)
152
153 let lines = readfile('Xprofile_func.log')
154
155 " - Foo1() should pass 'if' block.
156 " - Foo2() should pass 'elseif' block.
157 " - Foo3() should pass 'else' block.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200158 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200159
160 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200161 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
162 call assert_equal('Called 1 time', lines[2])
163 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
164 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
165 call assert_equal('', lines[5])
166 call assert_equal('count total (s) self (s)', lines[6])
167 call assert_match('^\s*1\s\+.*\sif 1$', lines[7])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100168 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200169 call assert_match( '^\s\+elseif 1$', lines[9])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100170 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200171 call assert_match( '^\s\+else$', lines[11])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100172 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200173 call assert_match('^\s*1\s\+.*\sendif$', lines[13])
174 call assert_equal('', lines[14])
175 call assert_equal('FUNCTION Foo2()', lines[15])
176 call assert_equal('Called 1 time', lines[17])
177 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
178 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
179 call assert_equal('', lines[20])
180 call assert_equal('count total (s) self (s)', lines[21])
181 call assert_match('^\s*1\s\+.*\sif 0$', lines[22])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100182 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[23])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200183 call assert_match('^\s*1\s\+.*\selseif 1$', lines[24])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100184 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200185 call assert_match( '^\s\+else$', lines[26])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100186 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200187 call assert_match('^\s*1\s\+.*\sendif$', lines[28])
188 call assert_equal('', lines[29])
189 call assert_equal('FUNCTION Foo3()', lines[30])
190 call assert_equal('Called 1 time', lines[32])
191 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
192 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
193 call assert_equal('', lines[35])
194 call assert_equal('count total (s) self (s)', lines[36])
195 call assert_match('^\s*1\s\+.*\sif 0$', lines[37])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100196 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[38])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200197 call assert_match('^\s*1\s\+.*\selseif 0$', lines[39])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100198 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[40])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200199 call assert_match('^\s*1\s\+.*\selse$', lines[41])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100200 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200201 call assert_match('^\s*1\s\+.*\sendif$', lines[43])
202 call assert_equal('', lines[44])
203 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
204 call assert_equal('count total (s) self (s) function', lines[46])
205 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
206 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
207 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
208 call assert_equal('', lines[50])
209 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
210 call assert_equal('count total (s) self (s) function', lines[52])
211 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
212 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
213 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
214 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200215
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200216 call delete('Xprofile_func.log')
217endfunc
218
219func Test_profile_func_with_trycatch()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100220 call Run_profile_func_with_trycatch('func', 'let')
221 call Run_profile_func_with_trycatch('def', 'var')
222endfunc
223
224func Run_profile_func_with_trycatch(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200225 let lines =<< trim [CODE]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100226 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200227 try
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100228 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200229 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100230 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200231 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100232 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200233 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100234 endXXX
235 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200236 try
237 throw 0
238 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100239 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200240 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100241 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200242 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100243 endXXX
244 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200245 try
246 throw 0
247 catch
248 throw 1
249 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100250 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200251 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100252 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200253 call Foo1()
254 call Foo2()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100255 let rethrown = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200256 try
257 call Foo3()
258 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100259 let rethrown = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200260 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100261 if rethrown != 1
262 " call Foo1 again so that the test fails
263 call Foo1()
264 endif
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200265 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200266
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100267 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
268 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
269
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100270 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200271 call system(GetVimCommand()
272 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200273 \ . ' -c "profile start Xprofile_func.log"'
274 \ . ' -c "profile func Foo*"'
275 \ . ' -c "so Xprofile_func.vim"'
276 \ . ' -c "qall!"')
277 call assert_equal(0, v:shell_error)
278
279 let lines = readfile('Xprofile_func.log')
280
281 " - Foo1() should pass 'try' 'finally' blocks.
282 " - Foo2() should pass 'catch' 'finally' blocks.
283 " - Foo3() should not pass 'endtry'.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200284 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200285
286 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200287 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
288 call assert_equal('Called 1 time', lines[2])
289 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
290 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
291 call assert_equal('', lines[5])
292 call assert_equal('count total (s) self (s)', lines[6])
293 call assert_match('^\s*1\s\+.*\stry$', lines[7])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100294 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200295 call assert_match( '^\s\+catch$', lines[9])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100296 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200297 call assert_match('^\s*1\s\+.*\sfinally$', lines[11])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100298 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200299 call assert_match('^\s*1\s\+.*\sendtry$', lines[13])
300 call assert_equal('', lines[14])
301 call assert_equal('FUNCTION Foo2()', lines[15])
302 call assert_equal('Called 1 time', lines[17])
303 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
304 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
305 call assert_equal('', lines[20])
306 call assert_equal('count total (s) self (s)', lines[21])
307 call assert_match('^\s*1\s\+.*\stry$', lines[22])
308 call assert_match('^\s*1\s\+.*\s throw 0$', lines[23])
309 call assert_match('^\s*1\s\+.*\scatch$', lines[24])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100310 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200311 call assert_match('^\s*1\s\+.*\sfinally$', lines[26])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100312 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200313 call assert_match('^\s*1\s\+.*\sendtry$', lines[28])
314 call assert_equal('', lines[29])
315 call assert_equal('FUNCTION Foo3()', lines[30])
316 call assert_equal('Called 1 time', lines[32])
317 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
318 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
319 call assert_equal('', lines[35])
320 call assert_equal('count total (s) self (s)', lines[36])
321 call assert_match('^\s*1\s\+.*\stry$', lines[37])
322 call assert_match('^\s*1\s\+.*\s throw 0$', lines[38])
323 call assert_match('^\s*1\s\+.*\scatch$', lines[39])
324 call assert_match('^\s*1\s\+.*\s throw 1$', lines[40])
325 call assert_match('^\s*1\s\+.*\sfinally$', lines[41])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100326 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200327 call assert_match( '^\s\+endtry$', lines[43])
328 call assert_equal('', lines[44])
329 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
330 call assert_equal('count total (s) self (s) function', lines[46])
331 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
332 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
333 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
334 call assert_equal('', lines[50])
335 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
336 call assert_equal('count total (s) self (s) function', lines[52])
337 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
338 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
339 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
340 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200341
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200342 call delete('Xprofile_func.log')
343endfunc
344
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100345func Test_profile_file()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200346 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200347 func! Foo()
348 endfunc
349 for i in range(10)
350 " a comment
351 call Foo()
352 endfor
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200353 call Foo()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200354 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100355
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100356 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200357 call system(GetVimCommandClean()
358 \ . ' -es'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +0100359 \ . ' -c "profile start Xprofile_file.log"'
360 \ . ' -c "profile file Xprofile_file.vim"'
361 \ . ' -c "so Xprofile_file.vim"'
362 \ . ' -c "so Xprofile_file.vim"'
363 \ . ' -c "qall!"')
364 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100365
366 let lines = readfile('Xprofile_file.log')
367
368 call assert_equal(14, len(lines))
369
370 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
371 call assert_equal('Sourced 2 times', lines[1])
372 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
373 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
374 call assert_equal('', lines[4])
375 call assert_equal('count total (s) self (s)', lines[5])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200376 call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100377 call assert_equal(' endfunc', lines[7])
378 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
379 " First line of loop executes one more time than body to detect end of loop.
380 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
381 call assert_equal(' " a comment', lines[9])
Bram Moolenaard21b16f2017-08-14 23:13:30 +0200382 " if self and total are equal we only get one number
383 call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200384 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100385 " if self and total are equal we only get one number
386 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100387 call assert_equal('', lines[13])
388
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100389 call delete('Xprofile_file.log')
390endfunc
391
Bram Moolenaar67435d92017-10-19 21:04:37 +0200392func Test_profile_file_with_cont()
393 let lines = [
394 \ 'echo "hello',
395 \ ' \ world"',
396 \ 'echo "foo ',
397 \ ' \bar"',
398 \ ]
399
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100400 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200401 call system(GetVimCommandClean()
402 \ . ' -es'
Bram Moolenaar67435d92017-10-19 21:04:37 +0200403 \ . ' -c "profile start Xprofile_file.log"'
404 \ . ' -c "profile file Xprofile_file.vim"'
405 \ . ' -c "so Xprofile_file.vim"'
406 \ . ' -c "qall!"')
407 call assert_equal(0, v:shell_error)
408
409 let lines = readfile('Xprofile_file.log')
410 call assert_equal(11, len(lines))
411
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200412 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
413 call assert_equal('Sourced 1 time', lines[1])
414 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
415 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
416 call assert_equal('', lines[4])
417 call assert_equal('count total (s) self (s)', lines[5])
418 call assert_match(' 1 0.\d\+ echo "hello', lines[6])
419 call assert_equal(' \ world"', lines[7])
420 call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
421 call assert_equal(' \bar"', lines[9])
422 call assert_equal('', lines[10])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200423
Bram Moolenaar67435d92017-10-19 21:04:37 +0200424 call delete('Xprofile_file.log')
425endfunc
426
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100427" Test for ':profile stop' and ':profile dump' commands
428func Test_profile_stop_dump()
429 call delete('Xprof1.out')
430 call delete('Xprof2.out')
431 call delete('Xprof3.out')
432 func Xprof_test1()
433 return "Hello"
434 endfunc
435 func Xprof_test2()
436 return "World"
437 endfunc
438
439 " Test for ':profile stop'
440 profile start Xprof1.out
441 profile func Xprof_test1
442 call Xprof_test1()
443 profile stop
444
445 let lines = readfile('Xprof1.out')
446 call assert_equal(17, len(lines))
447 call assert_equal('FUNCTION Xprof_test1()', lines[0])
448 call assert_match('Defined:.*test_profile.vim:', lines[1])
449 call assert_equal('Called 1 time', lines[2])
450 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
451 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
452 call assert_equal('', lines[5])
453 call assert_equal('count total (s) self (s)', lines[6])
454 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
455 call assert_equal('', lines[8])
456 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
457 call assert_equal('count total (s) self (s) function', lines[10])
458 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
459 call assert_equal('', lines[12])
460 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
461 call assert_equal('count total (s) self (s) function', lines[14])
462 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
463 call assert_equal('', lines[16])
464
465 " Test for ':profile stop' for a different function
466 profile start Xprof2.out
467 profile func Xprof_test2
468 call Xprof_test2()
469 profile stop
470 let lines = readfile('Xprof2.out')
471 call assert_equal(17, len(lines))
472 call assert_equal('FUNCTION Xprof_test2()', lines[0])
473 call assert_match('Defined:.*test_profile.vim:', lines[1])
474 call assert_equal('Called 1 time', lines[2])
475 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
476 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
477 call assert_equal('', lines[5])
478 call assert_equal('count total (s) self (s)', lines[6])
479 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[7])
480 call assert_equal('', lines[8])
481 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
482 call assert_equal('count total (s) self (s) function', lines[10])
483 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[11])
484 call assert_equal('', lines[12])
485 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
486 call assert_equal('count total (s) self (s) function', lines[14])
487 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[15])
488 call assert_equal('', lines[16])
489
490 " Test for ':profile dump'
491 profile start Xprof3.out
492 profile func Xprof_test1
493 profile func Xprof_test2
494 call Xprof_test1()
495 profile dump
496 " dump the profile once and verify the contents
497 let lines = readfile('Xprof3.out')
498 call assert_equal(17, len(lines))
499 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
500 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
501 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
502 " dump the profile again and verify the contents
503 call Xprof_test2()
504 profile dump
505 profile stop
506 let lines = readfile('Xprof3.out')
507 call assert_equal(28, len(lines))
508 call assert_equal('FUNCTION Xprof_test1()', lines[0])
509 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
510 call assert_equal('FUNCTION Xprof_test2()', lines[9])
511 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[16])
512
513 delfunc Xprof_test1
514 delfunc Xprof_test2
515 call delete('Xprof1.out')
516 call delete('Xprof2.out')
517 call delete('Xprof3.out')
518endfunc
519
520" Test for :profile sub-command completion
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100521func Test_profile_completion()
522 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100523 call assert_equal('"profile continue dump file func pause start stop', @:)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100524
525 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
526 call assert_match('^"profile start.* test_profile\.vim', @:)
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000527
528 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
529 call assert_match('"profile file test_profile\.vim', @:)
530 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
531 call assert_match('"profile file test_profile\.vim', @:)
532 call feedkeys(":profile file test_prof \<Tab>\<C-B>\"\<CR>", 'tx')
533 call assert_match('"profile file test_prof ', @:)
534 call feedkeys(":profile file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
535 call assert_match('"profile file X1B2C3', @:)
536
537 func Xprof_test()
538 endfunc
539 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
540 call assert_equal('"profile func Xprof_test', @:)
541 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
542 call assert_equal('"profile func Xprof_test', @:)
543 call feedkeys(":profile func Xprof \<Tab>\<C-B>\"\<CR>", 'tx')
544 call assert_equal('"profile func Xprof ', @:)
545 call feedkeys(":profile func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
546 call assert_equal('"profile func X1B2C3', @:)
547
548 call feedkeys(":profdel \<C-A>\<C-B>\"\<CR>", 'tx')
549 call assert_equal('"profdel file func', @:)
550 call feedkeys(":profdel fu\<Tab>\<C-B>\"\<CR>", 'tx')
551 call assert_equal('"profdel func', @:)
552 call feedkeys(":profdel he\<Tab>\<C-B>\"\<CR>", 'tx')
553 call assert_equal('"profdel he', @:)
554 call feedkeys(":profdel here \<Tab>\<C-B>\"\<CR>", 'tx')
555 call assert_equal('"profdel here ', @:)
556 call feedkeys(":profdel file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
557 call assert_equal('"profdel file test_profile.vim', @:)
558 call feedkeys(":profdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
559 call assert_equal('"profdel file X1B2C3', @:)
560 call feedkeys(":profdel func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
561 call assert_equal('"profdel func Xprof_test', @:)
562 call feedkeys(":profdel func Xprof_test \<Tab>\<C-B>\"\<CR>", 'tx')
563 call assert_equal('"profdel func Xprof_test ', @:)
564 call feedkeys(":profdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
565 call assert_equal('"profdel func X1B2C3', @:)
566
567 delfunc Xprof_test
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100568endfunc
569
570func Test_profile_errors()
571 call assert_fails("profile func Foo", 'E750:')
572 call assert_fails("profile pause", 'E750:')
573 call assert_fails("profile continue", 'E750:')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100574 call assert_fails("profile stop", 'E750:')
575 call assert_fails("profile dump", 'E750:')
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100576endfunc
Bram Moolenaarac112f02017-12-05 16:46:28 +0100577
578func Test_profile_truncate_mbyte()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100579 if &enc !=# 'utf-8'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100580 return
581 endif
582
583 let lines = [
584 \ 'scriptencoding utf-8',
585 \ 'func! Foo()',
586 \ ' return [',
587 \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
588 \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
589 \ ' \ ]',
590 \ 'endfunc',
591 \ 'call Foo()',
592 \ ]
593
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100594 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200595 call system(GetVimCommandClean()
596 \ . ' -es --cmd "set enc=utf-8"'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100597 \ . ' -c "profile start Xprofile_file.log"'
598 \ . ' -c "profile file Xprofile_file.vim"'
599 \ . ' -c "so Xprofile_file.vim"'
600 \ . ' -c "qall!"')
601 call assert_equal(0, v:shell_error)
602
603 split Xprofile_file.log
604 if &fenc != ''
605 call assert_equal('utf-8', &fenc)
606 endif
607 /func! Foo()
608 let lnum = line('.')
609 call assert_match('^\s*return \[$', getline(lnum + 1))
610 call assert_match("\u4F52$", getline(lnum + 2))
611 call assert_match("\u5052$", getline(lnum + 3))
612 call assert_match('^\s*\\ \]$', getline(lnum + 4))
613 bwipe!
614
Bram Moolenaarac112f02017-12-05 16:46:28 +0100615 call delete('Xprofile_file.log')
616endfunc
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200617
618func Test_profdel_func()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200619 let lines =<< trim [CODE]
620 profile start Xprofile_file.log
621 func! Foo1()
622 endfunc
623 func! Foo2()
624 endfunc
625 func! Foo3()
626 endfunc
627
628 profile func Foo1
629 profile func Foo2
630 call Foo1()
631 call Foo2()
632
633 profile func Foo3
634 profdel func Foo2
635 profdel func Foo3
636 call Foo1()
637 call Foo2()
638 call Foo3()
639 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100640 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200641 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200642 call assert_equal(0, v:shell_error)
643
644 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200645 call assert_equal(26, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200646
647 " Check that:
648 " - Foo1() is called twice (profdel not invoked)
649 " - Foo2() is called once (profdel invoked after it was called)
650 " - Foo3() is not called (profdel invoked before it was called)
651 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200652 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
653 call assert_equal('Called 2 times', lines[2])
654 call assert_equal('FUNCTION Foo2()', lines[8])
655 call assert_equal('Called 1 time', lines[10])
656 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16])
657 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200658
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200659 call delete('Xprofile_file.log')
660endfunc
661
662func Test_profdel_star()
663 " Foo() is invoked once before and once after 'profdel *'.
664 " So profiling should report it only once.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200665 let lines =<< trim [CODE]
666 profile start Xprofile_file.log
667 func! Foo()
668 endfunc
669 profile func Foo
670 call Foo()
671 profdel *
672 call Foo()
673 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100674 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200675 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200676 call assert_equal(0, v:shell_error)
677
678 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200679 call assert_equal(16, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200680
681 call assert_equal('FUNCTION Foo()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200682 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
683 call assert_equal('Called 1 time', lines[2])
684 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8])
685 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200686
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200687 call delete('Xprofile_file.log')
688endfunc
Bram Moolenaar16358802019-08-30 18:37:26 +0200689
690" When typing the function it won't have a script ID, test that this works.
691func Test_profile_typed_func()
692 CheckScreendump
693
694 let lines =<< trim END
695 profile start XprofileTypedFunc
696 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100697 call writefile(lines, 'XtestProfile', 'D')
Bram Moolenaar16358802019-08-30 18:37:26 +0200698 let buf = RunVimInTerminal('-S XtestProfile', #{})
699
700 call term_sendkeys(buf, ":func DoSomething()\<CR>"
701 \ .. "echo 'hello'\<CR>"
702 \ .. "endfunc\<CR>")
703 call term_sendkeys(buf, ":profile func DoSomething\<CR>")
704 call term_sendkeys(buf, ":call DoSomething()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200705 call TermWait(buf, 100)
Bram Moolenaar16358802019-08-30 18:37:26 +0200706 call StopVimInTerminal(buf)
707 let lines = readfile('XprofileTypedFunc')
708 call assert_equal("FUNCTION DoSomething()", lines[0])
709 call assert_equal("Called 1 time", lines[1])
710
711 " clean up
712 call delete('XprofileTypedFunc')
Bram Moolenaar16358802019-08-30 18:37:26 +0200713endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200714
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100715func Test_vim9_profiling()
716 " only tests that compiling and calling functions doesn't crash
717 let lines =<< trim END
718 vim9script
719 def Func()
720 Crash()
721 enddef
722 def Crash()
723 enddef
Bram Moolenaar98989a02021-01-26 12:06:30 +0100724 prof start Xprofile_crash.log
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100725 prof func Func
726 Func()
727 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100728 call writefile(lines, 'Xprofile_crash.vim', 'D')
Bram Moolenaar98989a02021-01-26 12:06:30 +0100729 call system(GetVimCommandClean() . ' -es -c "so Xprofile_crash.vim" -c q')
730 call assert_equal(0, v:shell_error)
Bram Moolenaar8c801b32021-03-05 20:58:22 +0100731 call assert_true(readfile('Xprofile_crash.log')->len() > 10)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100732
Bram Moolenaar98989a02021-01-26 12:06:30 +0100733 call delete('Xprofile_crash.log')
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100734endfunc
735
Bram Moolenaar12d26532021-02-19 19:13:21 +0100736func Test_vim9_nested_call()
737 let lines =<< trim END
738 vim9script
739 var total = 0
740 def One(Ref: func(number))
741 for i in range(3)
742 Ref(i)
743 endfor
744 enddef
745 def Two(nr: number)
746 total += nr
747 enddef
748 prof start Xprofile_nested.log
749 prof func One
750 prof func Two
751 One((nr) => Two(nr))
752 assert_equal(3, total)
753 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100754 call writefile(lines, 'Xprofile_nested.vim', 'D')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100755 call system(GetVimCommandClean() . ' -es -c "so Xprofile_nested.vim" -c q')
756 call assert_equal(0, v:shell_error)
757
758 let prof_lines = readfile('Xprofile_nested.log')->join('#')
759 call assert_match('FUNCTION <SNR>\d\+_One().*'
760 \ .. '#Called 1 time.*'
761 \ .. '# 1 \s*[0-9.]\+ for i in range(3)'
762 \ .. '# 3 \s*[0-9.]\+ \s*[0-9.]\+ Ref(i)'
763 \ .. '# 3 \s*[0-9.]\+ endfor', prof_lines)
764 call assert_match('FUNCTION <SNR>\d\+_Two().*'
765 \ .. '#Called 3 times.*'
766 \ .. '# 3 \s*[0-9.]\+ total += nr', prof_lines)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100767
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100768 call delete('Xprofile_nested.log')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100769endfunc
770
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100771
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200772" vim: shiftwidth=2 sts=2 expandtab