blob: e427179eae7788767d429d7d0517be31a0b7b79b [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 Moolenaarc0bdbfb2023-03-10 18:28:12 +00009let s:header = 'count total (s) self (s)'
10let s:header_func = 'count total (s) self (s) function'
11
Bram Moolenaar296b1f22017-01-15 15:22:33 +010012func Test_profile_func()
Bram Moolenaarb2049902021-01-24 12:53:53 +010013 call RunProfileFunc('func', 'let', 'let')
14 call RunProfileFunc('def', 'var', '')
15endfunc
16
17func RunProfileFunc(command, declare, assign)
Bram Moolenaarc79745a2019-05-20 22:12:34 +020018 let lines =<< trim [CODE]
19 profile start Xprofile_func.log
20 profile func Foo*
Bram Moolenaarb2049902021-01-24 12:53:53 +010021 XXX Foo1()
22 endXXX
23 XXX Foo2()
24 DDD counter = 100
25 while counter > 0
26 AAA counter = counter - 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +020027 endwhile
Bram Moolenaara21df1d2020-03-17 20:57:09 +010028 sleep 1m
Bram Moolenaarb2049902021-01-24 12:53:53 +010029 endXXX
30 XXX Foo3()
31 endXXX
32 XXX Bar()
33 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +020034 call Foo1()
35 call Foo1()
36 profile pause
37 call Foo1()
38 profile continue
39 call Foo2()
40 call Foo3()
41 call Bar()
42 if !v:profiling
43 delfunc Foo2
44 endif
45 delfunc Foo3
46 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +010047
Bram Moolenaarb2049902021-01-24 12:53:53 +010048 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
49 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
50 call map(lines, {k, v -> substitute(v, 'AAA', a:assign, '') })
51
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010052 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +020053 call system(GetVimCommand()
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +020054 \ . ' -es --clean'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010055 \ . ' -c "so Xprofile_func.vim"'
56 \ . ' -c "qall!"')
57 call assert_equal(0, v:shell_error)
58
K.Takata0500e872022-09-08 12:28:02 +010059 sleep 50m
Bram Moolenaar296b1f22017-01-15 15:22:33 +010060 let lines = readfile('Xprofile_func.log')
61
Bram Moolenaar296b1f22017-01-15 15:22:33 +010062 " - Foo1() is called 3 times but should be reported as called twice
63 " since one call is in between "profile pause" .. "profile continue".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010064 " - Foo2() should come before Foo1() since Foo1() does much more work.
Bram Moolenaar296b1f22017-01-15 15:22:33 +010065 " - Foo3() is not reported because function is deleted.
66 " - Unlike Foo3(), Foo2() should not be deleted since there is a check
67 " for v:profiling.
68 " - Bar() is not reported since it does not match "profile func Foo*".
Bram Moolenaara21df1d2020-03-17 20:57:09 +010069 call assert_equal(31, len(lines))
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010070
71 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar181d4f52019-09-18 22:04:56 +020072 call assert_match('Defined:.*Xprofile_func.vim:3', lines[1])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020073 call assert_equal('Called 2 times', lines[2])
74 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
75 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
76 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000077 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020078 call assert_equal('', lines[7])
79 call assert_equal('FUNCTION Foo2()', lines[8])
80 call assert_equal('Called 1 time', lines[10])
81 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[11])
82 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[12])
83 call assert_equal('', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000084 call assert_equal(s:header, lines[14])
Bram Moolenaarb2049902021-01-24 12:53:53 +010085 call assert_match('^\s*1\s\+.*\s\(let\|var\) counter = 100$', lines[15])
86 call assert_match('^\s*101\s\+.*\swhile counter > 0$', lines[16])
87 call assert_match('^\s*100\s\+.*\s \(let\)\= counter = counter - 1$', lines[17])
88 call assert_match('^\s*10[01]\s\+.*\sendwhile$', lines[18])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010089 call assert_match('^\s*1\s\+.\+sleep 1m$', lines[19])
90 call assert_equal('', lines[20])
91 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[21])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000092 call assert_equal(s:header_func, lines[22])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010093 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[23])
94 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[24])
95 call assert_equal('', lines[25])
96 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[26])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000097 call assert_equal(s:header_func, lines[27])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010098 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[28])
99 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[29])
100 call assert_equal('', lines[30])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100101
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100102 call delete('Xprofile_func.log')
103endfunc
104
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200105func Test_profile_func_with_ifelse()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100106 call Run_profile_func_with_ifelse('func', 'let')
107 call Run_profile_func_with_ifelse('def', 'var')
Bram Moolenaarced68a02021-01-24 17:53:47 +0100108endfunc
109
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100110func Run_profile_func_with_ifelse(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200111 let lines =<< trim [CODE]
Bram Moolenaarced68a02021-01-24 17:53:47 +0100112 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200113 if 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100114 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200115 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100116 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200117 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100118 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200119 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100120 endXXX
121 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200122 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100123 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200124 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100125 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200126 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100127 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200128 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100129 endXXX
130 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200131 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100132 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200133 elseif 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100134 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200135 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100136 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200137 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100138 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200139 call Foo1()
140 call Foo2()
141 call Foo3()
142 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200143
Bram Moolenaarced68a02021-01-24 17:53:47 +0100144 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
145 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
Bram Moolenaarced68a02021-01-24 17:53:47 +0100146
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100147 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200148 call system(GetVimCommand()
149 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200150 \ . ' -c "profile start Xprofile_func.log"'
151 \ . ' -c "profile func Foo*"'
152 \ . ' -c "so Xprofile_func.vim"'
153 \ . ' -c "qall!"')
154 call assert_equal(0, v:shell_error)
155
156 let lines = readfile('Xprofile_func.log')
157
158 " - Foo1() should pass 'if' block.
159 " - Foo2() should pass 'elseif' block.
160 " - Foo3() should pass 'else' block.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200161 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200162
163 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200164 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
165 call assert_equal('Called 1 time', lines[2])
166 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
167 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
168 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000169 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200170 call assert_match('^\s*1\s\+.*\sif 1$', lines[7])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100171 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200172 call assert_match( '^\s\+elseif 1$', lines[9])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100173 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200174 call assert_match( '^\s\+else$', lines[11])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100175 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200176 call assert_match('^\s*1\s\+.*\sendif$', lines[13])
177 call assert_equal('', lines[14])
178 call assert_equal('FUNCTION Foo2()', lines[15])
179 call assert_equal('Called 1 time', lines[17])
180 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
181 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
182 call assert_equal('', lines[20])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000183 call assert_equal(s:header, lines[21])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200184 call assert_match('^\s*1\s\+.*\sif 0$', lines[22])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100185 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[23])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200186 call assert_match('^\s*1\s\+.*\selseif 1$', lines[24])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100187 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200188 call assert_match( '^\s\+else$', lines[26])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100189 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200190 call assert_match('^\s*1\s\+.*\sendif$', lines[28])
191 call assert_equal('', lines[29])
192 call assert_equal('FUNCTION Foo3()', lines[30])
193 call assert_equal('Called 1 time', lines[32])
194 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
195 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
196 call assert_equal('', lines[35])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000197 call assert_equal(s:header, lines[36])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200198 call assert_match('^\s*1\s\+.*\sif 0$', lines[37])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100199 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[38])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200200 call assert_match('^\s*1\s\+.*\selseif 0$', lines[39])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100201 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[40])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200202 call assert_match('^\s*1\s\+.*\selse$', lines[41])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100203 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200204 call assert_match('^\s*1\s\+.*\sendif$', lines[43])
205 call assert_equal('', lines[44])
206 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000207 call assert_equal(s:header_func, lines[46])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200208 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
209 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
210 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
211 call assert_equal('', lines[50])
212 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000213 call assert_equal(s:header_func, lines[52])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200214 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
215 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
216 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
217 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200218
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200219 call delete('Xprofile_func.log')
220endfunc
221
222func Test_profile_func_with_trycatch()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100223 call Run_profile_func_with_trycatch('func', 'let')
224 call Run_profile_func_with_trycatch('def', 'var')
225endfunc
226
227func Run_profile_func_with_trycatch(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200228 let lines =<< trim [CODE]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100229 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200230 try
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100231 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200232 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100233 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200234 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100235 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200236 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100237 endXXX
238 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200239 try
240 throw 0
241 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100242 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200243 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100244 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200245 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100246 endXXX
247 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200248 try
249 throw 0
250 catch
251 throw 1
252 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100253 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200254 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100255 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200256 call Foo1()
257 call Foo2()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100258 let rethrown = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200259 try
260 call Foo3()
261 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100262 let rethrown = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200263 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100264 if rethrown != 1
265 " call Foo1 again so that the test fails
266 call Foo1()
267 endif
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200268 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200269
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100270 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
271 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
272
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100273 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200274 call system(GetVimCommand()
275 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200276 \ . ' -c "profile start Xprofile_func.log"'
277 \ . ' -c "profile func Foo*"'
278 \ . ' -c "so Xprofile_func.vim"'
279 \ . ' -c "qall!"')
280 call assert_equal(0, v:shell_error)
281
282 let lines = readfile('Xprofile_func.log')
283
284 " - Foo1() should pass 'try' 'finally' blocks.
285 " - Foo2() should pass 'catch' 'finally' blocks.
286 " - Foo3() should not pass 'endtry'.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200287 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200288
289 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200290 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
291 call assert_equal('Called 1 time', lines[2])
292 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
293 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
294 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000295 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200296 call assert_match('^\s*1\s\+.*\stry$', lines[7])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100297 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200298 call assert_match( '^\s\+catch$', lines[9])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100299 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200300 call assert_match('^\s*1\s\+.*\sfinally$', lines[11])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100301 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200302 call assert_match('^\s*1\s\+.*\sendtry$', lines[13])
303 call assert_equal('', lines[14])
304 call assert_equal('FUNCTION Foo2()', lines[15])
305 call assert_equal('Called 1 time', lines[17])
306 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
307 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
308 call assert_equal('', lines[20])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000309 call assert_equal(s:header, lines[21])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200310 call assert_match('^\s*1\s\+.*\stry$', lines[22])
311 call assert_match('^\s*1\s\+.*\s throw 0$', lines[23])
312 call assert_match('^\s*1\s\+.*\scatch$', lines[24])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100313 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200314 call assert_match('^\s*1\s\+.*\sfinally$', lines[26])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100315 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200316 call assert_match('^\s*1\s\+.*\sendtry$', lines[28])
317 call assert_equal('', lines[29])
318 call assert_equal('FUNCTION Foo3()', lines[30])
319 call assert_equal('Called 1 time', lines[32])
320 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
321 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
322 call assert_equal('', lines[35])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000323 call assert_equal(s:header, lines[36])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200324 call assert_match('^\s*1\s\+.*\stry$', lines[37])
325 call assert_match('^\s*1\s\+.*\s throw 0$', lines[38])
326 call assert_match('^\s*1\s\+.*\scatch$', lines[39])
327 call assert_match('^\s*1\s\+.*\s throw 1$', lines[40])
328 call assert_match('^\s*1\s\+.*\sfinally$', lines[41])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100329 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200330 call assert_match( '^\s\+endtry$', lines[43])
331 call assert_equal('', lines[44])
332 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000333 call assert_equal(s:header_func, lines[46])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200334 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
335 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
336 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
337 call assert_equal('', lines[50])
338 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000339 call assert_equal(s:header_func, lines[52])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200340 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
341 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
342 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
343 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200344
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200345 call delete('Xprofile_func.log')
346endfunc
347
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100348func Test_profile_file()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200349 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200350 func! Foo()
351 endfunc
352 for i in range(10)
353 " a comment
354 call Foo()
355 endfor
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200356 call Foo()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200357 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100358
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100359 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200360 call system(GetVimCommandClean()
361 \ . ' -es'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +0100362 \ . ' -c "profile start Xprofile_file.log"'
363 \ . ' -c "profile file Xprofile_file.vim"'
364 \ . ' -c "so Xprofile_file.vim"'
365 \ . ' -c "so Xprofile_file.vim"'
366 \ . ' -c "qall!"')
367 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100368
369 let lines = readfile('Xprofile_file.log')
370
371 call assert_equal(14, len(lines))
372
373 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
374 call assert_equal('Sourced 2 times', lines[1])
375 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
376 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
377 call assert_equal('', lines[4])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000378 call assert_equal(s:header, lines[5])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200379 call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100380 call assert_equal(' endfunc', lines[7])
381 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
382 " First line of loop executes one more time than body to detect end of loop.
383 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
384 call assert_equal(' " a comment', lines[9])
Bram Moolenaard21b16f2017-08-14 23:13:30 +0200385 " if self and total are equal we only get one number
386 call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200387 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100388 " if self and total are equal we only get one number
389 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100390 call assert_equal('', lines[13])
391
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100392 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
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100403 call writefile(lines, 'Xprofile_file.vim', 'D')
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])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000420 call assert_equal(s:header, lines[5])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200421 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
Bram Moolenaar67435d92017-10-19 21:04:37 +0200427 call delete('Xprofile_file.log')
428endfunc
429
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100430" Test for ':profile stop' and ':profile dump' commands
431func Test_profile_stop_dump()
432 call delete('Xprof1.out')
433 call delete('Xprof2.out')
434 call delete('Xprof3.out')
435 func Xprof_test1()
436 return "Hello"
437 endfunc
438 func Xprof_test2()
439 return "World"
440 endfunc
441
442 " Test for ':profile stop'
443 profile start Xprof1.out
444 profile func Xprof_test1
445 call Xprof_test1()
446 profile stop
447
448 let lines = readfile('Xprof1.out')
449 call assert_equal(17, len(lines))
450 call assert_equal('FUNCTION Xprof_test1()', lines[0])
451 call assert_match('Defined:.*test_profile.vim:', lines[1])
452 call assert_equal('Called 1 time', lines[2])
453 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
454 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
455 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000456 call assert_equal(s:header, lines[6])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100457 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
458 call assert_equal('', lines[8])
459 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000460 call assert_equal(s:header_func, lines[10])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100461 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
462 call assert_equal('', lines[12])
463 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000464 call assert_equal(s:header_func, lines[14])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100465 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
466 call assert_equal('', lines[16])
467
468 " Test for ':profile stop' for a different function
469 profile start Xprof2.out
470 profile func Xprof_test2
471 call Xprof_test2()
472 profile stop
473 let lines = readfile('Xprof2.out')
474 call assert_equal(17, len(lines))
475 call assert_equal('FUNCTION Xprof_test2()', lines[0])
476 call assert_match('Defined:.*test_profile.vim:', lines[1])
477 call assert_equal('Called 1 time', lines[2])
478 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
479 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
480 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000481 call assert_equal(s:header, lines[6])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100482 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[7])
483 call assert_equal('', lines[8])
484 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000485 call assert_equal(s:header_func, lines[10])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100486 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[11])
487 call assert_equal('', lines[12])
488 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000489 call assert_equal(s:header_func, lines[14])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100490 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[15])
491 call assert_equal('', lines[16])
492
493 " Test for ':profile dump'
494 profile start Xprof3.out
495 profile func Xprof_test1
496 profile func Xprof_test2
497 call Xprof_test1()
498 profile dump
499 " dump the profile once and verify the contents
500 let lines = readfile('Xprof3.out')
501 call assert_equal(17, len(lines))
502 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
503 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
504 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
505 " dump the profile again and verify the contents
506 call Xprof_test2()
507 profile dump
508 profile stop
509 let lines = readfile('Xprof3.out')
510 call assert_equal(28, len(lines))
511 call assert_equal('FUNCTION Xprof_test1()', lines[0])
512 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
513 call assert_equal('FUNCTION Xprof_test2()', lines[9])
514 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[16])
515
516 delfunc Xprof_test1
517 delfunc Xprof_test2
518 call delete('Xprof1.out')
519 call delete('Xprof2.out')
520 call delete('Xprof3.out')
521endfunc
522
523" Test for :profile sub-command completion
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100524func Test_profile_completion()
525 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100526 call assert_equal('"profile continue dump file func pause start stop', @:)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100527
528 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
529 call assert_match('^"profile start.* test_profile\.vim', @:)
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000530
531 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
532 call assert_match('"profile file test_profile\.vim', @:)
533 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
534 call assert_match('"profile file test_profile\.vim', @:)
535 call feedkeys(":profile file test_prof \<Tab>\<C-B>\"\<CR>", 'tx')
536 call assert_match('"profile file test_prof ', @:)
537 call feedkeys(":profile file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
538 call assert_match('"profile file X1B2C3', @:)
539
540 func Xprof_test()
541 endfunc
542 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
543 call assert_equal('"profile func Xprof_test', @:)
544 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
545 call assert_equal('"profile func Xprof_test', @:)
546 call feedkeys(":profile func Xprof \<Tab>\<C-B>\"\<CR>", 'tx')
547 call assert_equal('"profile func Xprof ', @:)
548 call feedkeys(":profile func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
549 call assert_equal('"profile func X1B2C3', @:)
550
551 call feedkeys(":profdel \<C-A>\<C-B>\"\<CR>", 'tx')
552 call assert_equal('"profdel file func', @:)
553 call feedkeys(":profdel fu\<Tab>\<C-B>\"\<CR>", 'tx')
554 call assert_equal('"profdel func', @:)
555 call feedkeys(":profdel he\<Tab>\<C-B>\"\<CR>", 'tx')
556 call assert_equal('"profdel he', @:)
557 call feedkeys(":profdel here \<Tab>\<C-B>\"\<CR>", 'tx')
558 call assert_equal('"profdel here ', @:)
559 call feedkeys(":profdel file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
560 call assert_equal('"profdel file test_profile.vim', @:)
561 call feedkeys(":profdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
562 call assert_equal('"profdel file X1B2C3', @:)
563 call feedkeys(":profdel func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
564 call assert_equal('"profdel func Xprof_test', @:)
565 call feedkeys(":profdel func Xprof_test \<Tab>\<C-B>\"\<CR>", 'tx')
566 call assert_equal('"profdel func Xprof_test ', @:)
567 call feedkeys(":profdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
568 call assert_equal('"profdel func X1B2C3', @:)
569
570 delfunc Xprof_test
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100571endfunc
572
573func Test_profile_errors()
574 call assert_fails("profile func Foo", 'E750:')
575 call assert_fails("profile pause", 'E750:')
576 call assert_fails("profile continue", 'E750:')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100577 call assert_fails("profile stop", 'E750:')
578 call assert_fails("profile dump", 'E750:')
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100579endfunc
Bram Moolenaarac112f02017-12-05 16:46:28 +0100580
581func Test_profile_truncate_mbyte()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100582 if &enc !=# 'utf-8'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100583 return
584 endif
585
586 let lines = [
587 \ 'scriptencoding utf-8',
588 \ 'func! Foo()',
589 \ ' return [',
590 \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
591 \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
592 \ ' \ ]',
593 \ 'endfunc',
594 \ 'call Foo()',
595 \ ]
596
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100597 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200598 call system(GetVimCommandClean()
599 \ . ' -es --cmd "set enc=utf-8"'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100600 \ . ' -c "profile start Xprofile_file.log"'
601 \ . ' -c "profile file Xprofile_file.vim"'
602 \ . ' -c "so Xprofile_file.vim"'
603 \ . ' -c "qall!"')
604 call assert_equal(0, v:shell_error)
605
606 split Xprofile_file.log
607 if &fenc != ''
608 call assert_equal('utf-8', &fenc)
609 endif
610 /func! Foo()
611 let lnum = line('.')
612 call assert_match('^\s*return \[$', getline(lnum + 1))
613 call assert_match("\u4F52$", getline(lnum + 2))
614 call assert_match("\u5052$", getline(lnum + 3))
615 call assert_match('^\s*\\ \]$', getline(lnum + 4))
616 bwipe!
617
Bram Moolenaarac112f02017-12-05 16:46:28 +0100618 call delete('Xprofile_file.log')
619endfunc
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200620
621func Test_profdel_func()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200622 let lines =<< trim [CODE]
623 profile start Xprofile_file.log
624 func! Foo1()
625 endfunc
626 func! Foo2()
627 endfunc
628 func! Foo3()
629 endfunc
630
631 profile func Foo1
632 profile func Foo2
633 call Foo1()
634 call Foo2()
635
636 profile func Foo3
637 profdel func Foo2
638 profdel func Foo3
639 call Foo1()
640 call Foo2()
641 call Foo3()
642 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100643 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200644 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200645 call assert_equal(0, v:shell_error)
646
647 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200648 call assert_equal(26, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200649
650 " Check that:
651 " - Foo1() is called twice (profdel not invoked)
652 " - Foo2() is called once (profdel invoked after it was called)
653 " - Foo3() is not called (profdel invoked before it was called)
654 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200655 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
656 call assert_equal('Called 2 times', lines[2])
657 call assert_equal('FUNCTION Foo2()', lines[8])
658 call assert_equal('Called 1 time', lines[10])
659 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16])
660 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200661
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200662 call delete('Xprofile_file.log')
663endfunc
664
665func Test_profdel_star()
666 " Foo() is invoked once before and once after 'profdel *'.
667 " So profiling should report it only once.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200668 let lines =<< trim [CODE]
669 profile start Xprofile_file.log
670 func! Foo()
671 endfunc
672 profile func Foo
673 call Foo()
674 profdel *
675 call Foo()
676 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100677 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200678 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200679 call assert_equal(0, v:shell_error)
680
681 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200682 call assert_equal(16, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200683
684 call assert_equal('FUNCTION Foo()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200685 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
686 call assert_equal('Called 1 time', lines[2])
687 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8])
688 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200689
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200690 call delete('Xprofile_file.log')
691endfunc
Bram Moolenaar16358802019-08-30 18:37:26 +0200692
693" When typing the function it won't have a script ID, test that this works.
694func Test_profile_typed_func()
695 CheckScreendump
696
697 let lines =<< trim END
698 profile start XprofileTypedFunc
699 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100700 call writefile(lines, 'XtestProfile', 'D')
Bram Moolenaar16358802019-08-30 18:37:26 +0200701 let buf = RunVimInTerminal('-S XtestProfile', #{})
702
703 call term_sendkeys(buf, ":func DoSomething()\<CR>"
704 \ .. "echo 'hello'\<CR>"
705 \ .. "endfunc\<CR>")
706 call term_sendkeys(buf, ":profile func DoSomething\<CR>")
707 call term_sendkeys(buf, ":call DoSomething()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200708 call TermWait(buf, 100)
Bram Moolenaar16358802019-08-30 18:37:26 +0200709 call StopVimInTerminal(buf)
710 let lines = readfile('XprofileTypedFunc')
711 call assert_equal("FUNCTION DoSomething()", lines[0])
712 call assert_equal("Called 1 time", lines[1])
713
714 " clean up
715 call delete('XprofileTypedFunc')
Bram Moolenaar16358802019-08-30 18:37:26 +0200716endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200717
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100718func Test_vim9_profiling()
719 " only tests that compiling and calling functions doesn't crash
720 let lines =<< trim END
721 vim9script
722 def Func()
723 Crash()
724 enddef
725 def Crash()
726 enddef
Bram Moolenaar98989a02021-01-26 12:06:30 +0100727 prof start Xprofile_crash.log
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100728 prof func Func
729 Func()
730 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100731 call writefile(lines, 'Xprofile_crash.vim', 'D')
Bram Moolenaar98989a02021-01-26 12:06:30 +0100732 call system(GetVimCommandClean() . ' -es -c "so Xprofile_crash.vim" -c q')
733 call assert_equal(0, v:shell_error)
Bram Moolenaar8c801b32021-03-05 20:58:22 +0100734 call assert_true(readfile('Xprofile_crash.log')->len() > 10)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100735
Bram Moolenaar98989a02021-01-26 12:06:30 +0100736 call delete('Xprofile_crash.log')
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100737endfunc
738
Bram Moolenaar12d26532021-02-19 19:13:21 +0100739func Test_vim9_nested_call()
740 let lines =<< trim END
741 vim9script
742 var total = 0
743 def One(Ref: func(number))
744 for i in range(3)
745 Ref(i)
746 endfor
747 enddef
748 def Two(nr: number)
749 total += nr
750 enddef
751 prof start Xprofile_nested.log
752 prof func One
753 prof func Two
754 One((nr) => Two(nr))
755 assert_equal(3, total)
756 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100757 call writefile(lines, 'Xprofile_nested.vim', 'D')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100758 call system(GetVimCommandClean() . ' -es -c "so Xprofile_nested.vim" -c q')
759 call assert_equal(0, v:shell_error)
760
761 let prof_lines = readfile('Xprofile_nested.log')->join('#')
762 call assert_match('FUNCTION <SNR>\d\+_One().*'
763 \ .. '#Called 1 time.*'
764 \ .. '# 1 \s*[0-9.]\+ for i in range(3)'
765 \ .. '# 3 \s*[0-9.]\+ \s*[0-9.]\+ Ref(i)'
766 \ .. '# 3 \s*[0-9.]\+ endfor', prof_lines)
767 call assert_match('FUNCTION <SNR>\d\+_Two().*'
768 \ .. '#Called 3 times.*'
769 \ .. '# 3 \s*[0-9.]\+ total += nr', prof_lines)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100770
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100771 call delete('Xprofile_nested.log')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100772endfunc
773
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100774
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200775" vim: shiftwidth=2 sts=2 expandtab