blob: 9c7cb5cde6f421f18e221a74e73532fc01265f0b [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 Moolenaar296b1f22017-01-15 15:22:33 +010049 call writefile(lines, 'Xprofile_func.vim')
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
Bram Moolenaar296b1f22017-01-15 15:22:33 +010056 let lines = readfile('Xprofile_func.log')
57
Bram Moolenaar296b1f22017-01-15 15:22:33 +010058 " - Foo1() is called 3 times but should be reported as called twice
59 " since one call is in between "profile pause" .. "profile continue".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010060 " - Foo2() should come before Foo1() since Foo1() does much more work.
Bram Moolenaar296b1f22017-01-15 15:22:33 +010061 " - Foo3() is not reported because function is deleted.
62 " - Unlike Foo3(), Foo2() should not be deleted since there is a check
63 " for v:profiling.
64 " - Bar() is not reported since it does not match "profile func Foo*".
Bram Moolenaara21df1d2020-03-17 20:57:09 +010065 call assert_equal(31, len(lines))
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010066
67 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar181d4f52019-09-18 22:04:56 +020068 call assert_match('Defined:.*Xprofile_func.vim:3', lines[1])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020069 call assert_equal('Called 2 times', lines[2])
70 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
71 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
72 call assert_equal('', lines[5])
73 call assert_equal('count total (s) self (s)', lines[6])
74 call assert_equal('', lines[7])
75 call assert_equal('FUNCTION Foo2()', lines[8])
76 call assert_equal('Called 1 time', lines[10])
77 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[11])
78 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[12])
79 call assert_equal('', lines[13])
80 call assert_equal('count total (s) self (s)', lines[14])
Bram Moolenaarb2049902021-01-24 12:53:53 +010081 call assert_match('^\s*1\s\+.*\s\(let\|var\) counter = 100$', lines[15])
82 call assert_match('^\s*101\s\+.*\swhile counter > 0$', lines[16])
83 call assert_match('^\s*100\s\+.*\s \(let\)\= counter = counter - 1$', lines[17])
84 call assert_match('^\s*10[01]\s\+.*\sendwhile$', lines[18])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010085 call assert_match('^\s*1\s\+.\+sleep 1m$', lines[19])
86 call assert_equal('', lines[20])
87 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[21])
88 call assert_equal('count total (s) self (s) function', lines[22])
89 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[23])
90 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[24])
91 call assert_equal('', lines[25])
92 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[26])
93 call assert_equal('count total (s) self (s) function', lines[27])
94 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[28])
95 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[29])
96 call assert_equal('', lines[30])
Bram Moolenaar296b1f22017-01-15 15:22:33 +010097
98 call delete('Xprofile_func.vim')
99 call delete('Xprofile_func.log')
100endfunc
101
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200102func Test_profile_func_with_ifelse()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200103 let lines =<< trim [CODE]
104 func! Foo1()
105 if 1
106 let x = 0
107 elseif 1
108 let x = 1
109 else
110 let x = 2
111 endif
112 endfunc
113 func! Foo2()
114 if 0
115 let x = 0
116 elseif 1
117 let x = 1
118 else
119 let x = 2
120 endif
121 endfunc
122 func! Foo3()
123 if 0
124 let x = 0
125 elseif 0
126 let x = 1
127 else
128 let x = 2
129 endif
130 endfunc
131 call Foo1()
132 call Foo2()
133 call Foo3()
134 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200135
136 call writefile(lines, 'Xprofile_func.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200137 call system(GetVimCommand()
138 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200139 \ . ' -c "profile start Xprofile_func.log"'
140 \ . ' -c "profile func Foo*"'
141 \ . ' -c "so Xprofile_func.vim"'
142 \ . ' -c "qall!"')
143 call assert_equal(0, v:shell_error)
144
145 let lines = readfile('Xprofile_func.log')
146
147 " - Foo1() should pass 'if' block.
148 " - Foo2() should pass 'elseif' block.
149 " - Foo3() should pass 'else' block.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200150 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200151
152 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200153 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
154 call assert_equal('Called 1 time', lines[2])
155 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
156 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
157 call assert_equal('', lines[5])
158 call assert_equal('count total (s) self (s)', lines[6])
159 call assert_match('^\s*1\s\+.*\sif 1$', lines[7])
160 call assert_match('^\s*1\s\+.*\s let x = 0$', lines[8])
161 call assert_match( '^\s\+elseif 1$', lines[9])
162 call assert_match( '^\s\+let x = 1$', lines[10])
163 call assert_match( '^\s\+else$', lines[11])
164 call assert_match( '^\s\+let x = 2$', lines[12])
165 call assert_match('^\s*1\s\+.*\sendif$', lines[13])
166 call assert_equal('', lines[14])
167 call assert_equal('FUNCTION Foo2()', lines[15])
168 call assert_equal('Called 1 time', lines[17])
169 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
170 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
171 call assert_equal('', lines[20])
172 call assert_equal('count total (s) self (s)', lines[21])
173 call assert_match('^\s*1\s\+.*\sif 0$', lines[22])
174 call assert_match( '^\s\+let x = 0$', lines[23])
175 call assert_match('^\s*1\s\+.*\selseif 1$', lines[24])
176 call assert_match('^\s*1\s\+.*\s let x = 1$', lines[25])
177 call assert_match( '^\s\+else$', lines[26])
178 call assert_match( '^\s\+let x = 2$', lines[27])
179 call assert_match('^\s*1\s\+.*\sendif$', lines[28])
180 call assert_equal('', lines[29])
181 call assert_equal('FUNCTION Foo3()', lines[30])
182 call assert_equal('Called 1 time', lines[32])
183 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
184 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
185 call assert_equal('', lines[35])
186 call assert_equal('count total (s) self (s)', lines[36])
187 call assert_match('^\s*1\s\+.*\sif 0$', lines[37])
188 call assert_match( '^\s\+let x = 0$', lines[38])
189 call assert_match('^\s*1\s\+.*\selseif 0$', lines[39])
190 call assert_match( '^\s\+let x = 1$', lines[40])
191 call assert_match('^\s*1\s\+.*\selse$', lines[41])
192 call assert_match('^\s*1\s\+.*\s let x = 2$', lines[42])
193 call assert_match('^\s*1\s\+.*\sendif$', lines[43])
194 call assert_equal('', lines[44])
195 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
196 call assert_equal('count total (s) self (s) function', lines[46])
197 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
198 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
199 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
200 call assert_equal('', lines[50])
201 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
202 call assert_equal('count total (s) self (s) function', lines[52])
203 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
204 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
205 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
206 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200207
208 call delete('Xprofile_func.vim')
209 call delete('Xprofile_func.log')
210endfunc
211
212func Test_profile_func_with_trycatch()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200213 let lines =<< trim [CODE]
214 func! Foo1()
215 try
216 let x = 0
217 catch
218 let x = 1
219 finally
220 let x = 2
221 endtry
222 endfunc
223 func! Foo2()
224 try
225 throw 0
226 catch
227 let x = 1
228 finally
229 let x = 2
230 endtry
231 endfunc
232 func! Foo3()
233 try
234 throw 0
235 catch
236 throw 1
237 finally
238 let x = 2
239 endtry
240 endfunc
241 call Foo1()
242 call Foo2()
243 try
244 call Foo3()
245 catch
246 endtry
247 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200248
249 call writefile(lines, 'Xprofile_func.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200250 call system(GetVimCommand()
251 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200252 \ . ' -c "profile start Xprofile_func.log"'
253 \ . ' -c "profile func Foo*"'
254 \ . ' -c "so Xprofile_func.vim"'
255 \ . ' -c "qall!"')
256 call assert_equal(0, v:shell_error)
257
258 let lines = readfile('Xprofile_func.log')
259
260 " - Foo1() should pass 'try' 'finally' blocks.
261 " - Foo2() should pass 'catch' 'finally' blocks.
262 " - Foo3() should not pass 'endtry'.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200263 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200264
265 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200266 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
267 call assert_equal('Called 1 time', lines[2])
268 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
269 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
270 call assert_equal('', lines[5])
271 call assert_equal('count total (s) self (s)', lines[6])
272 call assert_match('^\s*1\s\+.*\stry$', lines[7])
273 call assert_match('^\s*1\s\+.*\s let x = 0$', lines[8])
274 call assert_match( '^\s\+catch$', lines[9])
275 call assert_match( '^\s\+let x = 1$', lines[10])
276 call assert_match('^\s*1\s\+.*\sfinally$', lines[11])
277 call assert_match('^\s*1\s\+.*\s let x = 2$', lines[12])
278 call assert_match('^\s*1\s\+.*\sendtry$', lines[13])
279 call assert_equal('', lines[14])
280 call assert_equal('FUNCTION Foo2()', lines[15])
281 call assert_equal('Called 1 time', lines[17])
282 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
283 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
284 call assert_equal('', lines[20])
285 call assert_equal('count total (s) self (s)', lines[21])
286 call assert_match('^\s*1\s\+.*\stry$', lines[22])
287 call assert_match('^\s*1\s\+.*\s throw 0$', lines[23])
288 call assert_match('^\s*1\s\+.*\scatch$', lines[24])
289 call assert_match('^\s*1\s\+.*\s let x = 1$', lines[25])
290 call assert_match('^\s*1\s\+.*\sfinally$', lines[26])
291 call assert_match('^\s*1\s\+.*\s let x = 2$', lines[27])
292 call assert_match('^\s*1\s\+.*\sendtry$', lines[28])
293 call assert_equal('', lines[29])
294 call assert_equal('FUNCTION Foo3()', lines[30])
295 call assert_equal('Called 1 time', lines[32])
296 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
297 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
298 call assert_equal('', lines[35])
299 call assert_equal('count total (s) self (s)', lines[36])
300 call assert_match('^\s*1\s\+.*\stry$', lines[37])
301 call assert_match('^\s*1\s\+.*\s throw 0$', lines[38])
302 call assert_match('^\s*1\s\+.*\scatch$', lines[39])
303 call assert_match('^\s*1\s\+.*\s throw 1$', lines[40])
304 call assert_match('^\s*1\s\+.*\sfinally$', lines[41])
305 call assert_match('^\s*1\s\+.*\s let x = 2$', lines[42])
306 call assert_match( '^\s\+endtry$', lines[43])
307 call assert_equal('', lines[44])
308 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
309 call assert_equal('count total (s) self (s) function', lines[46])
310 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
311 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
312 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
313 call assert_equal('', lines[50])
314 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
315 call assert_equal('count total (s) self (s) function', lines[52])
316 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
317 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
318 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
319 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200320
321 call delete('Xprofile_func.vim')
322 call delete('Xprofile_func.log')
323endfunc
324
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100325func Test_profile_file()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200326 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200327 func! Foo()
328 endfunc
329 for i in range(10)
330 " a comment
331 call Foo()
332 endfor
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200333 call Foo()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200334 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100335
336 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200337 call system(GetVimCommandClean()
338 \ . ' -es'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +0100339 \ . ' -c "profile start Xprofile_file.log"'
340 \ . ' -c "profile file Xprofile_file.vim"'
341 \ . ' -c "so Xprofile_file.vim"'
342 \ . ' -c "so Xprofile_file.vim"'
343 \ . ' -c "qall!"')
344 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100345
346 let lines = readfile('Xprofile_file.log')
347
348 call assert_equal(14, len(lines))
349
350 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
351 call assert_equal('Sourced 2 times', lines[1])
352 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
353 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
354 call assert_equal('', lines[4])
355 call assert_equal('count total (s) self (s)', lines[5])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200356 call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100357 call assert_equal(' endfunc', lines[7])
358 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
359 " First line of loop executes one more time than body to detect end of loop.
360 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
361 call assert_equal(' " a comment', lines[9])
Bram Moolenaard21b16f2017-08-14 23:13:30 +0200362 " if self and total are equal we only get one number
363 call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200364 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100365 " if self and total are equal we only get one number
366 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100367 call assert_equal('', lines[13])
368
369 call delete('Xprofile_file.vim')
370 call delete('Xprofile_file.log')
371endfunc
372
Bram Moolenaar67435d92017-10-19 21:04:37 +0200373func Test_profile_file_with_cont()
374 let lines = [
375 \ 'echo "hello',
376 \ ' \ world"',
377 \ 'echo "foo ',
378 \ ' \bar"',
379 \ ]
380
381 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200382 call system(GetVimCommandClean()
383 \ . ' -es'
Bram Moolenaar67435d92017-10-19 21:04:37 +0200384 \ . ' -c "profile start Xprofile_file.log"'
385 \ . ' -c "profile file Xprofile_file.vim"'
386 \ . ' -c "so Xprofile_file.vim"'
387 \ . ' -c "qall!"')
388 call assert_equal(0, v:shell_error)
389
390 let lines = readfile('Xprofile_file.log')
391 call assert_equal(11, len(lines))
392
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200393 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
394 call assert_equal('Sourced 1 time', lines[1])
395 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
396 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
397 call assert_equal('', lines[4])
398 call assert_equal('count total (s) self (s)', lines[5])
399 call assert_match(' 1 0.\d\+ echo "hello', lines[6])
400 call assert_equal(' \ world"', lines[7])
401 call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
402 call assert_equal(' \bar"', lines[9])
403 call assert_equal('', lines[10])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200404
405 call delete('Xprofile_file.vim')
406 call delete('Xprofile_file.log')
407endfunc
408
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100409func Test_profile_completion()
410 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
411 call assert_equal('"profile continue file func pause start', @:)
412
413 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
414 call assert_match('^"profile start.* test_profile\.vim', @:)
415endfunc
416
417func Test_profile_errors()
418 call assert_fails("profile func Foo", 'E750:')
419 call assert_fails("profile pause", 'E750:')
420 call assert_fails("profile continue", 'E750:')
421endfunc
Bram Moolenaarac112f02017-12-05 16:46:28 +0100422
423func Test_profile_truncate_mbyte()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100424 if &enc !=# 'utf-8'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100425 return
426 endif
427
428 let lines = [
429 \ 'scriptencoding utf-8',
430 \ 'func! Foo()',
431 \ ' return [',
432 \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
433 \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
434 \ ' \ ]',
435 \ 'endfunc',
436 \ 'call Foo()',
437 \ ]
438
439 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200440 call system(GetVimCommandClean()
441 \ . ' -es --cmd "set enc=utf-8"'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100442 \ . ' -c "profile start Xprofile_file.log"'
443 \ . ' -c "profile file Xprofile_file.vim"'
444 \ . ' -c "so Xprofile_file.vim"'
445 \ . ' -c "qall!"')
446 call assert_equal(0, v:shell_error)
447
448 split Xprofile_file.log
449 if &fenc != ''
450 call assert_equal('utf-8', &fenc)
451 endif
452 /func! Foo()
453 let lnum = line('.')
454 call assert_match('^\s*return \[$', getline(lnum + 1))
455 call assert_match("\u4F52$", getline(lnum + 2))
456 call assert_match("\u5052$", getline(lnum + 3))
457 call assert_match('^\s*\\ \]$', getline(lnum + 4))
458 bwipe!
459
460 call delete('Xprofile_file.vim')
461 call delete('Xprofile_file.log')
462endfunc
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200463
464func Test_profdel_func()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200465 let lines =<< trim [CODE]
466 profile start Xprofile_file.log
467 func! Foo1()
468 endfunc
469 func! Foo2()
470 endfunc
471 func! Foo3()
472 endfunc
473
474 profile func Foo1
475 profile func Foo2
476 call Foo1()
477 call Foo2()
478
479 profile func Foo3
480 profdel func Foo2
481 profdel func Foo3
482 call Foo1()
483 call Foo2()
484 call Foo3()
485 [CODE]
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200486 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200487 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200488 call assert_equal(0, v:shell_error)
489
490 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200491 call assert_equal(26, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200492
493 " Check that:
494 " - Foo1() is called twice (profdel not invoked)
495 " - Foo2() is called once (profdel invoked after it was called)
496 " - Foo3() is not called (profdel invoked before it was called)
497 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200498 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
499 call assert_equal('Called 2 times', lines[2])
500 call assert_equal('FUNCTION Foo2()', lines[8])
501 call assert_equal('Called 1 time', lines[10])
502 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16])
503 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200504
505 call delete('Xprofile_file.vim')
506 call delete('Xprofile_file.log')
507endfunc
508
509func Test_profdel_star()
510 " Foo() is invoked once before and once after 'profdel *'.
511 " So profiling should report it only once.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200512 let lines =<< trim [CODE]
513 profile start Xprofile_file.log
514 func! Foo()
515 endfunc
516 profile func Foo
517 call Foo()
518 profdel *
519 call Foo()
520 [CODE]
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200521 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200522 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200523 call assert_equal(0, v:shell_error)
524
525 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200526 call assert_equal(16, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200527
528 call assert_equal('FUNCTION Foo()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200529 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
530 call assert_equal('Called 1 time', lines[2])
531 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8])
532 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200533
534 call delete('Xprofile_file.vim')
535 call delete('Xprofile_file.log')
536endfunc
Bram Moolenaar16358802019-08-30 18:37:26 +0200537
538" When typing the function it won't have a script ID, test that this works.
539func Test_profile_typed_func()
540 CheckScreendump
541
542 let lines =<< trim END
543 profile start XprofileTypedFunc
544 END
545 call writefile(lines, 'XtestProfile')
546 let buf = RunVimInTerminal('-S XtestProfile', #{})
547
548 call term_sendkeys(buf, ":func DoSomething()\<CR>"
549 \ .. "echo 'hello'\<CR>"
550 \ .. "endfunc\<CR>")
551 call term_sendkeys(buf, ":profile func DoSomething\<CR>")
552 call term_sendkeys(buf, ":call DoSomething()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200553 call TermWait(buf, 100)
Bram Moolenaar16358802019-08-30 18:37:26 +0200554 call StopVimInTerminal(buf)
555 let lines = readfile('XprofileTypedFunc')
556 call assert_equal("FUNCTION DoSomething()", lines[0])
557 call assert_equal("Called 1 time", lines[1])
558
559 " clean up
560 call delete('XprofileTypedFunc')
561 call delete('XtestProfile')
562endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200563
564" vim: shiftwidth=2 sts=2 expandtab