blob: d95cbddcba288bfefbe44436276397db360b523e [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 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 Moolenaar7feb35e2018-08-21 17:49:54 +0200144 call writefile(lines, 'Xprofile_func.vim')
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
216 call delete('Xprofile_func.vim')
217 call delete('Xprofile_func.log')
218endfunc
219
220func Test_profile_func_with_trycatch()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100221 call Run_profile_func_with_trycatch('func', 'let')
222 call Run_profile_func_with_trycatch('def', 'var')
223endfunc
224
225func Run_profile_func_with_trycatch(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200226 let lines =<< trim [CODE]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100227 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200228 try
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100229 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200230 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100231 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200232 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100233 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200234 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100235 endXXX
236 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200237 try
238 throw 0
239 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100240 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200241 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100242 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200243 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100244 endXXX
245 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200246 try
247 throw 0
248 catch
249 throw 1
250 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100251 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200252 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100253 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200254 call Foo1()
255 call Foo2()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100256 let rethrown = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200257 try
258 call Foo3()
259 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100260 let rethrown = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200261 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100262 if rethrown != 1
263 " call Foo1 again so that the test fails
264 call Foo1()
265 endif
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200266 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200267
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100268 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
269 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
270
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200271 call writefile(lines, 'Xprofile_func.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200272 call system(GetVimCommand()
273 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200274 \ . ' -c "profile start Xprofile_func.log"'
275 \ . ' -c "profile func Foo*"'
276 \ . ' -c "so Xprofile_func.vim"'
277 \ . ' -c "qall!"')
278 call assert_equal(0, v:shell_error)
279
280 let lines = readfile('Xprofile_func.log')
281
282 " - Foo1() should pass 'try' 'finally' blocks.
283 " - Foo2() should pass 'catch' 'finally' blocks.
284 " - Foo3() should not pass 'endtry'.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200285 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200286
287 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200288 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
289 call assert_equal('Called 1 time', lines[2])
290 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
291 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
292 call assert_equal('', lines[5])
293 call assert_equal('count total (s) self (s)', lines[6])
294 call assert_match('^\s*1\s\+.*\stry$', lines[7])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100295 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200296 call assert_match( '^\s\+catch$', lines[9])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100297 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200298 call assert_match('^\s*1\s\+.*\sfinally$', lines[11])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100299 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200300 call assert_match('^\s*1\s\+.*\sendtry$', lines[13])
301 call assert_equal('', lines[14])
302 call assert_equal('FUNCTION Foo2()', lines[15])
303 call assert_equal('Called 1 time', lines[17])
304 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
305 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
306 call assert_equal('', lines[20])
307 call assert_equal('count total (s) self (s)', lines[21])
308 call assert_match('^\s*1\s\+.*\stry$', lines[22])
309 call assert_match('^\s*1\s\+.*\s throw 0$', lines[23])
310 call assert_match('^\s*1\s\+.*\scatch$', lines[24])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100311 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200312 call assert_match('^\s*1\s\+.*\sfinally$', lines[26])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100313 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200314 call assert_match('^\s*1\s\+.*\sendtry$', lines[28])
315 call assert_equal('', lines[29])
316 call assert_equal('FUNCTION Foo3()', lines[30])
317 call assert_equal('Called 1 time', lines[32])
318 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
319 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
320 call assert_equal('', lines[35])
321 call assert_equal('count total (s) self (s)', lines[36])
322 call assert_match('^\s*1\s\+.*\stry$', lines[37])
323 call assert_match('^\s*1\s\+.*\s throw 0$', lines[38])
324 call assert_match('^\s*1\s\+.*\scatch$', lines[39])
325 call assert_match('^\s*1\s\+.*\s throw 1$', lines[40])
326 call assert_match('^\s*1\s\+.*\sfinally$', lines[41])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100327 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200328 call assert_match( '^\s\+endtry$', lines[43])
329 call assert_equal('', lines[44])
330 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
331 call assert_equal('count total (s) self (s) function', lines[46])
332 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
333 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
334 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
335 call assert_equal('', lines[50])
336 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
337 call assert_equal('count total (s) self (s) function', lines[52])
338 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
339 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
340 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
341 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200342
343 call delete('Xprofile_func.vim')
344 call delete('Xprofile_func.log')
345endfunc
346
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100347func Test_profile_file()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200348 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200349 func! Foo()
350 endfunc
351 for i in range(10)
352 " a comment
353 call Foo()
354 endfor
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200355 call Foo()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200356 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100357
358 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200359 call system(GetVimCommandClean()
360 \ . ' -es'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +0100361 \ . ' -c "profile start Xprofile_file.log"'
362 \ . ' -c "profile file Xprofile_file.vim"'
363 \ . ' -c "so Xprofile_file.vim"'
364 \ . ' -c "so Xprofile_file.vim"'
365 \ . ' -c "qall!"')
366 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100367
368 let lines = readfile('Xprofile_file.log')
369
370 call assert_equal(14, len(lines))
371
372 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
373 call assert_equal('Sourced 2 times', lines[1])
374 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
375 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
376 call assert_equal('', lines[4])
377 call assert_equal('count total (s) self (s)', lines[5])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200378 call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100379 call assert_equal(' endfunc', lines[7])
380 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
381 " First line of loop executes one more time than body to detect end of loop.
382 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
383 call assert_equal(' " a comment', lines[9])
Bram Moolenaard21b16f2017-08-14 23:13:30 +0200384 " if self and total are equal we only get one number
385 call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200386 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100387 " if self and total are equal we only get one number
388 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100389 call assert_equal('', lines[13])
390
391 call delete('Xprofile_file.vim')
392 call delete('Xprofile_file.log')
393endfunc
394
Bram Moolenaar67435d92017-10-19 21:04:37 +0200395func Test_profile_file_with_cont()
396 let lines = [
397 \ 'echo "hello',
398 \ ' \ world"',
399 \ 'echo "foo ',
400 \ ' \bar"',
401 \ ]
402
403 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200404 call system(GetVimCommandClean()
405 \ . ' -es'
Bram Moolenaar67435d92017-10-19 21:04:37 +0200406 \ . ' -c "profile start Xprofile_file.log"'
407 \ . ' -c "profile file Xprofile_file.vim"'
408 \ . ' -c "so Xprofile_file.vim"'
409 \ . ' -c "qall!"')
410 call assert_equal(0, v:shell_error)
411
412 let lines = readfile('Xprofile_file.log')
413 call assert_equal(11, len(lines))
414
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200415 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
416 call assert_equal('Sourced 1 time', lines[1])
417 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
418 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
419 call assert_equal('', lines[4])
420 call assert_equal('count total (s) self (s)', lines[5])
421 call assert_match(' 1 0.\d\+ echo "hello', lines[6])
422 call assert_equal(' \ world"', lines[7])
423 call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
424 call assert_equal(' \bar"', lines[9])
425 call assert_equal('', lines[10])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200426
427 call delete('Xprofile_file.vim')
428 call delete('Xprofile_file.log')
429endfunc
430
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100431func Test_profile_completion()
432 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
433 call assert_equal('"profile continue file func pause start', @:)
434
435 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
436 call assert_match('^"profile start.* test_profile\.vim', @:)
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000437
438 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
439 call assert_match('"profile file test_profile\.vim', @:)
440 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
441 call assert_match('"profile file test_profile\.vim', @:)
442 call feedkeys(":profile file test_prof \<Tab>\<C-B>\"\<CR>", 'tx')
443 call assert_match('"profile file test_prof ', @:)
444 call feedkeys(":profile file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
445 call assert_match('"profile file X1B2C3', @:)
446
447 func Xprof_test()
448 endfunc
449 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
450 call assert_equal('"profile func Xprof_test', @:)
451 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
452 call assert_equal('"profile func Xprof_test', @:)
453 call feedkeys(":profile func Xprof \<Tab>\<C-B>\"\<CR>", 'tx')
454 call assert_equal('"profile func Xprof ', @:)
455 call feedkeys(":profile func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
456 call assert_equal('"profile func X1B2C3', @:)
457
458 call feedkeys(":profdel \<C-A>\<C-B>\"\<CR>", 'tx')
459 call assert_equal('"profdel file func', @:)
460 call feedkeys(":profdel fu\<Tab>\<C-B>\"\<CR>", 'tx')
461 call assert_equal('"profdel func', @:)
462 call feedkeys(":profdel he\<Tab>\<C-B>\"\<CR>", 'tx')
463 call assert_equal('"profdel he', @:)
464 call feedkeys(":profdel here \<Tab>\<C-B>\"\<CR>", 'tx')
465 call assert_equal('"profdel here ', @:)
466 call feedkeys(":profdel file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
467 call assert_equal('"profdel file test_profile.vim', @:)
468 call feedkeys(":profdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
469 call assert_equal('"profdel file X1B2C3', @:)
470 call feedkeys(":profdel func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
471 call assert_equal('"profdel func Xprof_test', @:)
472 call feedkeys(":profdel func Xprof_test \<Tab>\<C-B>\"\<CR>", 'tx')
473 call assert_equal('"profdel func Xprof_test ', @:)
474 call feedkeys(":profdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
475 call assert_equal('"profdel func X1B2C3', @:)
476
477 delfunc Xprof_test
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100478endfunc
479
480func Test_profile_errors()
481 call assert_fails("profile func Foo", 'E750:')
482 call assert_fails("profile pause", 'E750:')
483 call assert_fails("profile continue", 'E750:')
484endfunc
Bram Moolenaarac112f02017-12-05 16:46:28 +0100485
486func Test_profile_truncate_mbyte()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100487 if &enc !=# 'utf-8'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100488 return
489 endif
490
491 let lines = [
492 \ 'scriptencoding utf-8',
493 \ 'func! Foo()',
494 \ ' return [',
495 \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
496 \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
497 \ ' \ ]',
498 \ 'endfunc',
499 \ 'call Foo()',
500 \ ]
501
502 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200503 call system(GetVimCommandClean()
504 \ . ' -es --cmd "set enc=utf-8"'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100505 \ . ' -c "profile start Xprofile_file.log"'
506 \ . ' -c "profile file Xprofile_file.vim"'
507 \ . ' -c "so Xprofile_file.vim"'
508 \ . ' -c "qall!"')
509 call assert_equal(0, v:shell_error)
510
511 split Xprofile_file.log
512 if &fenc != ''
513 call assert_equal('utf-8', &fenc)
514 endif
515 /func! Foo()
516 let lnum = line('.')
517 call assert_match('^\s*return \[$', getline(lnum + 1))
518 call assert_match("\u4F52$", getline(lnum + 2))
519 call assert_match("\u5052$", getline(lnum + 3))
520 call assert_match('^\s*\\ \]$', getline(lnum + 4))
521 bwipe!
522
523 call delete('Xprofile_file.vim')
524 call delete('Xprofile_file.log')
525endfunc
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200526
527func Test_profdel_func()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200528 let lines =<< trim [CODE]
529 profile start Xprofile_file.log
530 func! Foo1()
531 endfunc
532 func! Foo2()
533 endfunc
534 func! Foo3()
535 endfunc
536
537 profile func Foo1
538 profile func Foo2
539 call Foo1()
540 call Foo2()
541
542 profile func Foo3
543 profdel func Foo2
544 profdel func Foo3
545 call Foo1()
546 call Foo2()
547 call Foo3()
548 [CODE]
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200549 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200550 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200551 call assert_equal(0, v:shell_error)
552
553 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200554 call assert_equal(26, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200555
556 " Check that:
557 " - Foo1() is called twice (profdel not invoked)
558 " - Foo2() is called once (profdel invoked after it was called)
559 " - Foo3() is not called (profdel invoked before it was called)
560 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200561 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
562 call assert_equal('Called 2 times', lines[2])
563 call assert_equal('FUNCTION Foo2()', lines[8])
564 call assert_equal('Called 1 time', lines[10])
565 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16])
566 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200567
568 call delete('Xprofile_file.vim')
569 call delete('Xprofile_file.log')
570endfunc
571
572func Test_profdel_star()
573 " Foo() is invoked once before and once after 'profdel *'.
574 " So profiling should report it only once.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200575 let lines =<< trim [CODE]
576 profile start Xprofile_file.log
577 func! Foo()
578 endfunc
579 profile func Foo
580 call Foo()
581 profdel *
582 call Foo()
583 [CODE]
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200584 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200585 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200586 call assert_equal(0, v:shell_error)
587
588 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200589 call assert_equal(16, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200590
591 call assert_equal('FUNCTION Foo()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200592 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
593 call assert_equal('Called 1 time', lines[2])
594 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8])
595 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200596
597 call delete('Xprofile_file.vim')
598 call delete('Xprofile_file.log')
599endfunc
Bram Moolenaar16358802019-08-30 18:37:26 +0200600
601" When typing the function it won't have a script ID, test that this works.
602func Test_profile_typed_func()
603 CheckScreendump
604
605 let lines =<< trim END
606 profile start XprofileTypedFunc
607 END
608 call writefile(lines, 'XtestProfile')
609 let buf = RunVimInTerminal('-S XtestProfile', #{})
610
611 call term_sendkeys(buf, ":func DoSomething()\<CR>"
612 \ .. "echo 'hello'\<CR>"
613 \ .. "endfunc\<CR>")
614 call term_sendkeys(buf, ":profile func DoSomething\<CR>")
615 call term_sendkeys(buf, ":call DoSomething()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200616 call TermWait(buf, 100)
Bram Moolenaar16358802019-08-30 18:37:26 +0200617 call StopVimInTerminal(buf)
618 let lines = readfile('XprofileTypedFunc')
619 call assert_equal("FUNCTION DoSomething()", lines[0])
620 call assert_equal("Called 1 time", lines[1])
621
622 " clean up
623 call delete('XprofileTypedFunc')
624 call delete('XtestProfile')
625endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200626
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100627func Test_vim9_profiling()
628 " only tests that compiling and calling functions doesn't crash
629 let lines =<< trim END
630 vim9script
631 def Func()
632 Crash()
633 enddef
634 def Crash()
635 enddef
Bram Moolenaar98989a02021-01-26 12:06:30 +0100636 prof start Xprofile_crash.log
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100637 prof func Func
638 Func()
639 END
Bram Moolenaar98989a02021-01-26 12:06:30 +0100640 call writefile(lines, 'Xprofile_crash.vim')
641 call system(GetVimCommandClean() . ' -es -c "so Xprofile_crash.vim" -c q')
642 call assert_equal(0, v:shell_error)
Bram Moolenaar8c801b32021-03-05 20:58:22 +0100643 call assert_true(readfile('Xprofile_crash.log')->len() > 10)
Bram Moolenaar98989a02021-01-26 12:06:30 +0100644 call delete('Xprofile_crash.vim')
645 call delete('Xprofile_crash.log')
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100646endfunc
647
Bram Moolenaar12d26532021-02-19 19:13:21 +0100648func Test_vim9_nested_call()
649 let lines =<< trim END
650 vim9script
651 var total = 0
652 def One(Ref: func(number))
653 for i in range(3)
654 Ref(i)
655 endfor
656 enddef
657 def Two(nr: number)
658 total += nr
659 enddef
660 prof start Xprofile_nested.log
661 prof func One
662 prof func Two
663 One((nr) => Two(nr))
664 assert_equal(3, total)
665 END
666 call writefile(lines, 'Xprofile_nested.vim')
667 call system(GetVimCommandClean() . ' -es -c "so Xprofile_nested.vim" -c q')
668 call assert_equal(0, v:shell_error)
669
670 let prof_lines = readfile('Xprofile_nested.log')->join('#')
671 call assert_match('FUNCTION <SNR>\d\+_One().*'
672 \ .. '#Called 1 time.*'
673 \ .. '# 1 \s*[0-9.]\+ for i in range(3)'
674 \ .. '# 3 \s*[0-9.]\+ \s*[0-9.]\+ Ref(i)'
675 \ .. '# 3 \s*[0-9.]\+ endfor', prof_lines)
676 call assert_match('FUNCTION <SNR>\d\+_Two().*'
677 \ .. '#Called 3 times.*'
678 \ .. '# 3 \s*[0-9.]\+ total += nr', prof_lines)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100679 call delete('Xprofile_nested.vim')
680 call delete('Xprofile_nested.log')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100681endfunc
682
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100683
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200684" vim: shiftwidth=2 sts=2 expandtab