blob: 534cc768c44a46ee5c31a6551f8f40fc0d226ee3 [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
Ernie Rael076de792023-03-16 21:43:15 +00009if has('prof_nsec')
10 let s:header = 'count total (s) self (s)'
11 let s:header_func = 'count total (s) self (s) function'
12else
13 let s:header = 'count total (s) self (s)'
14 let s:header_func = 'count total (s) self (s) function'
15endif
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000016
Bram Moolenaar296b1f22017-01-15 15:22:33 +010017func Test_profile_func()
Bram Moolenaarb2049902021-01-24 12:53:53 +010018 call RunProfileFunc('func', 'let', 'let')
19 call RunProfileFunc('def', 'var', '')
20endfunc
21
22func RunProfileFunc(command, declare, assign)
Bram Moolenaarc79745a2019-05-20 22:12:34 +020023 let lines =<< trim [CODE]
24 profile start Xprofile_func.log
25 profile func Foo*
Bram Moolenaarb2049902021-01-24 12:53:53 +010026 XXX Foo1()
27 endXXX
28 XXX Foo2()
29 DDD counter = 100
30 while counter > 0
31 AAA counter = counter - 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +020032 endwhile
Bram Moolenaara21df1d2020-03-17 20:57:09 +010033 sleep 1m
Bram Moolenaarb2049902021-01-24 12:53:53 +010034 endXXX
35 XXX Foo3()
36 endXXX
37 XXX Bar()
38 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +020039 call Foo1()
40 call Foo1()
41 profile pause
42 call Foo1()
43 profile continue
44 call Foo2()
45 call Foo3()
46 call Bar()
47 if !v:profiling
48 delfunc Foo2
49 endif
50 delfunc Foo3
51 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +010052
Bram Moolenaarb2049902021-01-24 12:53:53 +010053 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
54 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
55 call map(lines, {k, v -> substitute(v, 'AAA', a:assign, '') })
56
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010057 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +020058 call system(GetVimCommand()
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +020059 \ . ' -es --clean'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010060 \ . ' -c "so Xprofile_func.vim"'
61 \ . ' -c "qall!"')
62 call assert_equal(0, v:shell_error)
63
K.Takata0500e872022-09-08 12:28:02 +010064 sleep 50m
Bram Moolenaar296b1f22017-01-15 15:22:33 +010065 let lines = readfile('Xprofile_func.log')
66
Bram Moolenaar296b1f22017-01-15 15:22:33 +010067 " - Foo1() is called 3 times but should be reported as called twice
68 " since one call is in between "profile pause" .. "profile continue".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010069 " - Foo2() should come before Foo1() since Foo1() does much more work.
Bram Moolenaar296b1f22017-01-15 15:22:33 +010070 " - Foo3() is not reported because function is deleted.
71 " - Unlike Foo3(), Foo2() should not be deleted since there is a check
72 " for v:profiling.
73 " - Bar() is not reported since it does not match "profile func Foo*".
Bram Moolenaara21df1d2020-03-17 20:57:09 +010074 call assert_equal(31, len(lines))
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010075
76 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar181d4f52019-09-18 22:04:56 +020077 call assert_match('Defined:.*Xprofile_func.vim:3', lines[1])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020078 call assert_equal('Called 2 times', lines[2])
79 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
80 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
81 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000082 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +020083 call assert_equal('', lines[7])
84 call assert_equal('FUNCTION Foo2()', lines[8])
85 call assert_equal('Called 1 time', lines[10])
86 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[11])
87 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[12])
88 call assert_equal('', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000089 call assert_equal(s:header, lines[14])
Bram Moolenaarb2049902021-01-24 12:53:53 +010090 call assert_match('^\s*1\s\+.*\s\(let\|var\) counter = 100$', lines[15])
91 call assert_match('^\s*101\s\+.*\swhile counter > 0$', lines[16])
92 call assert_match('^\s*100\s\+.*\s \(let\)\= counter = counter - 1$', lines[17])
93 call assert_match('^\s*10[01]\s\+.*\sendwhile$', lines[18])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010094 call assert_match('^\s*1\s\+.\+sleep 1m$', lines[19])
95 call assert_equal('', lines[20])
96 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[21])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +000097 call assert_equal(s:header_func, lines[22])
Bram Moolenaara21df1d2020-03-17 20:57:09 +010098 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[23])
99 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[24])
100 call assert_equal('', lines[25])
101 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[26])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000102 call assert_equal(s:header_func, lines[27])
Bram Moolenaara21df1d2020-03-17 20:57:09 +0100103 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[28])
104 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[29])
105 call assert_equal('', lines[30])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100106
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100107 call delete('Xprofile_func.log')
108endfunc
109
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200110func Test_profile_func_with_ifelse()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100111 call Run_profile_func_with_ifelse('func', 'let')
112 call Run_profile_func_with_ifelse('def', 'var')
Bram Moolenaarced68a02021-01-24 17:53:47 +0100113endfunc
114
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100115func Run_profile_func_with_ifelse(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200116 let lines =<< trim [CODE]
Bram Moolenaarced68a02021-01-24 17:53:47 +0100117 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200118 if 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100119 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200120 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100121 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200122 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100123 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200124 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100125 endXXX
126 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200127 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100128 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200129 elseif 1
Bram Moolenaarced68a02021-01-24 17:53:47 +0100130 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200131 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100132 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200133 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100134 endXXX
135 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200136 if 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100137 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200138 elseif 0
Bram Moolenaarced68a02021-01-24 17:53:47 +0100139 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200140 else
Bram Moolenaarced68a02021-01-24 17:53:47 +0100141 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200142 endif
Bram Moolenaarced68a02021-01-24 17:53:47 +0100143 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200144 call Foo1()
145 call Foo2()
146 call Foo3()
147 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200148
Bram Moolenaarced68a02021-01-24 17:53:47 +0100149 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
150 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
Bram Moolenaarced68a02021-01-24 17:53:47 +0100151
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100152 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200153 call system(GetVimCommand()
154 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200155 \ . ' -c "profile start Xprofile_func.log"'
156 \ . ' -c "profile func Foo*"'
157 \ . ' -c "so Xprofile_func.vim"'
158 \ . ' -c "qall!"')
159 call assert_equal(0, v:shell_error)
160
161 let lines = readfile('Xprofile_func.log')
162
163 " - Foo1() should pass 'if' block.
164 " - Foo2() should pass 'elseif' block.
165 " - Foo3() should pass 'else' block.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200166 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200167
168 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200169 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
170 call assert_equal('Called 1 time', lines[2])
171 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
172 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
173 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000174 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200175 call assert_match('^\s*1\s\+.*\sif 1$', lines[7])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100176 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200177 call assert_match( '^\s\+elseif 1$', lines[9])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100178 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200179 call assert_match( '^\s\+else$', lines[11])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100180 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200181 call assert_match('^\s*1\s\+.*\sendif$', lines[13])
182 call assert_equal('', lines[14])
183 call assert_equal('FUNCTION Foo2()', lines[15])
184 call assert_equal('Called 1 time', lines[17])
185 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
186 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
187 call assert_equal('', lines[20])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000188 call assert_equal(s:header, lines[21])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200189 call assert_match('^\s*1\s\+.*\sif 0$', lines[22])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100190 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[23])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200191 call assert_match('^\s*1\s\+.*\selseif 1$', lines[24])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100192 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200193 call assert_match( '^\s\+else$', lines[26])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100194 call assert_match( '^\s\+\(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200195 call assert_match('^\s*1\s\+.*\sendif$', lines[28])
196 call assert_equal('', lines[29])
197 call assert_equal('FUNCTION Foo3()', lines[30])
198 call assert_equal('Called 1 time', lines[32])
199 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
200 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
201 call assert_equal('', lines[35])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000202 call assert_equal(s:header, lines[36])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200203 call assert_match('^\s*1\s\+.*\sif 0$', lines[37])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100204 call assert_match( '^\s\+\(let\|var\) x = 0$', lines[38])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200205 call assert_match('^\s*1\s\+.*\selseif 0$', lines[39])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100206 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[40])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200207 call assert_match('^\s*1\s\+.*\selse$', lines[41])
Bram Moolenaarced68a02021-01-24 17:53:47 +0100208 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200209 call assert_match('^\s*1\s\+.*\sendif$', lines[43])
210 call assert_equal('', lines[44])
211 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000212 call assert_equal(s:header_func, lines[46])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200213 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
214 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
215 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
216 call assert_equal('', lines[50])
217 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000218 call assert_equal(s:header_func, lines[52])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200219 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
220 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
221 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
222 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200223
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200224 call delete('Xprofile_func.log')
225endfunc
226
227func Test_profile_func_with_trycatch()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100228 call Run_profile_func_with_trycatch('func', 'let')
229 call Run_profile_func_with_trycatch('def', 'var')
230endfunc
231
232func Run_profile_func_with_trycatch(command, declare)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200233 let lines =<< trim [CODE]
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100234 XXX Foo1()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200235 try
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100236 DDD x = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200237 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100238 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200239 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100240 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200241 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100242 endXXX
243 XXX Foo2()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200244 try
245 throw 0
246 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100247 DDD x = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200248 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100249 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200250 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100251 endXXX
252 XXX Foo3()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200253 try
254 throw 0
255 catch
256 throw 1
257 finally
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100258 DDD x = 2
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200259 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100260 endXXX
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200261 call Foo1()
262 call Foo2()
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100263 let rethrown = 0
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200264 try
265 call Foo3()
266 catch
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100267 let rethrown = 1
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200268 endtry
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100269 if rethrown != 1
270 " call Foo1 again so that the test fails
271 call Foo1()
272 endif
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200273 [CODE]
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200274
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100275 call map(lines, {k, v -> substitute(v, 'XXX', a:command, '') })
276 call map(lines, {k, v -> substitute(v, 'DDD', a:declare, '') })
277
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100278 call writefile(lines, 'Xprofile_func.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200279 call system(GetVimCommand()
280 \ . ' -es -i NONE --noplugin'
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200281 \ . ' -c "profile start Xprofile_func.log"'
282 \ . ' -c "profile func Foo*"'
283 \ . ' -c "so Xprofile_func.vim"'
284 \ . ' -c "qall!"')
285 call assert_equal(0, v:shell_error)
286
287 let lines = readfile('Xprofile_func.log')
288
289 " - Foo1() should pass 'try' 'finally' blocks.
290 " - Foo2() should pass 'catch' 'finally' blocks.
291 " - Foo3() should not pass 'endtry'.
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200292 call assert_equal(57, len(lines))
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200293
294 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200295 call assert_match('Defined:.*Xprofile_func.vim', lines[1])
296 call assert_equal('Called 1 time', lines[2])
297 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
298 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
299 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000300 call assert_equal(s:header, lines[6])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200301 call assert_match('^\s*1\s\+.*\stry$', lines[7])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100302 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 0$', lines[8])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200303 call assert_match( '^\s\+catch$', lines[9])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100304 call assert_match( '^\s\+\(let\|var\) x = 1$', lines[10])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200305 call assert_match('^\s*1\s\+.*\sfinally$', lines[11])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100306 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[12])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200307 call assert_match('^\s*1\s\+.*\sendtry$', lines[13])
308 call assert_equal('', lines[14])
309 call assert_equal('FUNCTION Foo2()', lines[15])
310 call assert_equal('Called 1 time', lines[17])
311 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18])
312 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19])
313 call assert_equal('', lines[20])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000314 call assert_equal(s:header, lines[21])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200315 call assert_match('^\s*1\s\+.*\stry$', lines[22])
316 call assert_match('^\s*1\s\+.*\s throw 0$', lines[23])
317 call assert_match('^\s*1\s\+.*\scatch$', lines[24])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100318 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 1$', lines[25])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200319 call assert_match('^\s*1\s\+.*\sfinally$', lines[26])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100320 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[27])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200321 call assert_match('^\s*1\s\+.*\sendtry$', lines[28])
322 call assert_equal('', lines[29])
323 call assert_equal('FUNCTION Foo3()', lines[30])
324 call assert_equal('Called 1 time', lines[32])
325 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33])
326 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34])
327 call assert_equal('', lines[35])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000328 call assert_equal(s:header, lines[36])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200329 call assert_match('^\s*1\s\+.*\stry$', lines[37])
330 call assert_match('^\s*1\s\+.*\s throw 0$', lines[38])
331 call assert_match('^\s*1\s\+.*\scatch$', lines[39])
332 call assert_match('^\s*1\s\+.*\s throw 1$', lines[40])
333 call assert_match('^\s*1\s\+.*\sfinally$', lines[41])
Bram Moolenaar107e9ce2021-01-24 20:52:00 +0100334 call assert_match('^\s*1\s\+.*\s \(let\|var\) x = 2$', lines[42])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200335 call assert_match( '^\s\+endtry$', lines[43])
336 call assert_equal('', lines[44])
337 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000338 call assert_equal(s:header_func, lines[46])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200339 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47])
340 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48])
341 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49])
342 call assert_equal('', lines[50])
343 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000344 call assert_equal(s:header_func, lines[52])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200345 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53])
346 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54])
347 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55])
348 call assert_equal('', lines[56])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200349
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200350 call delete('Xprofile_func.log')
351endfunc
352
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100353func Test_profile_file()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200354 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200355 func! Foo()
356 endfunc
357 for i in range(10)
358 " a comment
359 call Foo()
360 endfor
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200361 call Foo()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200362 [CODE]
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100363
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100364 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200365 call system(GetVimCommandClean()
366 \ . ' -es'
Bram Moolenaarc011a3d2017-01-16 22:37:42 +0100367 \ . ' -c "profile start Xprofile_file.log"'
368 \ . ' -c "profile file Xprofile_file.vim"'
369 \ . ' -c "so Xprofile_file.vim"'
370 \ . ' -c "so Xprofile_file.vim"'
371 \ . ' -c "qall!"')
372 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100373
374 let lines = readfile('Xprofile_file.log')
375
376 call assert_equal(14, len(lines))
377
378 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
379 call assert_equal('Sourced 2 times', lines[1])
380 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
381 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
382 call assert_equal('', lines[4])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000383 call assert_equal(s:header, lines[5])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200384 call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100385 call assert_equal(' endfunc', lines[7])
386 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
387 " First line of loop executes one more time than body to detect end of loop.
388 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
389 call assert_equal(' " a comment', lines[9])
Bram Moolenaard21b16f2017-08-14 23:13:30 +0200390 " if self and total are equal we only get one number
391 call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10])
Bram Moolenaar7feb35e2018-08-21 17:49:54 +0200392 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100393 " if self and total are equal we only get one number
394 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100395 call assert_equal('', lines[13])
396
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100397 call delete('Xprofile_file.log')
398endfunc
399
Bram Moolenaar67435d92017-10-19 21:04:37 +0200400func Test_profile_file_with_cont()
401 let lines = [
402 \ 'echo "hello',
403 \ ' \ world"',
404 \ 'echo "foo ',
405 \ ' \bar"',
406 \ ]
407
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100408 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200409 call system(GetVimCommandClean()
410 \ . ' -es'
Bram Moolenaar67435d92017-10-19 21:04:37 +0200411 \ . ' -c "profile start Xprofile_file.log"'
412 \ . ' -c "profile file Xprofile_file.vim"'
413 \ . ' -c "so Xprofile_file.vim"'
414 \ . ' -c "qall!"')
415 call assert_equal(0, v:shell_error)
416
417 let lines = readfile('Xprofile_file.log')
418 call assert_equal(11, len(lines))
419
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200420 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
421 call assert_equal('Sourced 1 time', lines[1])
422 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
423 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
424 call assert_equal('', lines[4])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000425 call assert_equal(s:header, lines[5])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200426 call assert_match(' 1 0.\d\+ echo "hello', lines[6])
427 call assert_equal(' \ world"', lines[7])
428 call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
429 call assert_equal(' \bar"', lines[9])
430 call assert_equal('', lines[10])
Bram Moolenaar67435d92017-10-19 21:04:37 +0200431
Bram Moolenaar67435d92017-10-19 21:04:37 +0200432 call delete('Xprofile_file.log')
433endfunc
434
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100435" Test for ':profile stop' and ':profile dump' commands
436func Test_profile_stop_dump()
437 call delete('Xprof1.out')
438 call delete('Xprof2.out')
439 call delete('Xprof3.out')
440 func Xprof_test1()
441 return "Hello"
442 endfunc
443 func Xprof_test2()
444 return "World"
445 endfunc
446
447 " Test for ':profile stop'
448 profile start Xprof1.out
449 profile func Xprof_test1
450 call Xprof_test1()
451 profile stop
452
453 let lines = readfile('Xprof1.out')
454 call assert_equal(17, len(lines))
455 call assert_equal('FUNCTION Xprof_test1()', lines[0])
456 call assert_match('Defined:.*test_profile.vim:', lines[1])
457 call assert_equal('Called 1 time', lines[2])
458 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
459 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
460 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000461 call assert_equal(s:header, lines[6])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100462 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
463 call assert_equal('', lines[8])
464 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000465 call assert_equal(s:header_func, lines[10])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100466 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
467 call assert_equal('', lines[12])
468 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000469 call assert_equal(s:header_func, lines[14])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100470 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
471 call assert_equal('', lines[16])
472
473 " Test for ':profile stop' for a different function
474 profile start Xprof2.out
475 profile func Xprof_test2
476 call Xprof_test2()
477 profile stop
478 let lines = readfile('Xprof2.out')
479 call assert_equal(17, len(lines))
480 call assert_equal('FUNCTION Xprof_test2()', lines[0])
481 call assert_match('Defined:.*test_profile.vim:', lines[1])
482 call assert_equal('Called 1 time', lines[2])
483 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3])
484 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4])
485 call assert_equal('', lines[5])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000486 call assert_equal(s:header, lines[6])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100487 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[7])
488 call assert_equal('', lines[8])
489 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[9])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000490 call assert_equal(s:header_func, lines[10])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100491 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[11])
492 call assert_equal('', lines[12])
493 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[13])
Bram Moolenaarc0bdbfb2023-03-10 18:28:12 +0000494 call assert_equal(s:header_func, lines[14])
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100495 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test2()$', lines[15])
496 call assert_equal('', lines[16])
497
498 " Test for ':profile dump'
499 profile start Xprof3.out
500 profile func Xprof_test1
501 profile func Xprof_test2
502 call Xprof_test1()
503 profile dump
504 " dump the profile once and verify the contents
505 let lines = readfile('Xprof3.out')
506 call assert_equal(17, len(lines))
507 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
508 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[11])
509 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Xprof_test1()$', lines[15])
510 " dump the profile again and verify the contents
511 call Xprof_test2()
512 profile dump
513 profile stop
514 let lines = readfile('Xprof3.out')
515 call assert_equal(28, len(lines))
516 call assert_equal('FUNCTION Xprof_test1()', lines[0])
517 call assert_match('^\s*1\s\+.*\sreturn "Hello"$', lines[7])
518 call assert_equal('FUNCTION Xprof_test2()', lines[9])
519 call assert_match('^\s*1\s\+.*\sreturn "World"$', lines[16])
520
521 delfunc Xprof_test1
522 delfunc Xprof_test2
523 call delete('Xprof1.out')
524 call delete('Xprof2.out')
525 call delete('Xprof3.out')
526endfunc
527
528" Test for :profile sub-command completion
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100529func Test_profile_completion()
530 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100531 call assert_equal('"profile continue dump file func pause start stop', @:)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100532
533 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
534 call assert_match('^"profile start.* test_profile\.vim', @:)
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +0000535
536 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
537 call assert_match('"profile file test_profile\.vim', @:)
538 call feedkeys(":profile file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
539 call assert_match('"profile file test_profile\.vim', @:)
540 call feedkeys(":profile file test_prof \<Tab>\<C-B>\"\<CR>", 'tx')
541 call assert_match('"profile file test_prof ', @:)
542 call feedkeys(":profile file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
543 call assert_match('"profile file X1B2C3', @:)
544
545 func Xprof_test()
546 endfunc
547 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
548 call assert_equal('"profile func Xprof_test', @:)
549 call feedkeys(":profile func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
550 call assert_equal('"profile func Xprof_test', @:)
551 call feedkeys(":profile func Xprof \<Tab>\<C-B>\"\<CR>", 'tx')
552 call assert_equal('"profile func Xprof ', @:)
553 call feedkeys(":profile func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
554 call assert_equal('"profile func X1B2C3', @:)
555
556 call feedkeys(":profdel \<C-A>\<C-B>\"\<CR>", 'tx')
557 call assert_equal('"profdel file func', @:)
558 call feedkeys(":profdel fu\<Tab>\<C-B>\"\<CR>", 'tx')
559 call assert_equal('"profdel func', @:)
560 call feedkeys(":profdel he\<Tab>\<C-B>\"\<CR>", 'tx')
561 call assert_equal('"profdel he', @:)
562 call feedkeys(":profdel here \<Tab>\<C-B>\"\<CR>", 'tx')
563 call assert_equal('"profdel here ', @:)
564 call feedkeys(":profdel file test_prof\<Tab>\<C-B>\"\<CR>", 'tx')
565 call assert_equal('"profdel file test_profile.vim', @:)
566 call feedkeys(":profdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
567 call assert_equal('"profdel file X1B2C3', @:)
568 call feedkeys(":profdel func Xprof\<Tab>\<C-B>\"\<CR>", 'tx')
569 call assert_equal('"profdel func Xprof_test', @:)
570 call feedkeys(":profdel func Xprof_test \<Tab>\<C-B>\"\<CR>", 'tx')
571 call assert_equal('"profdel func Xprof_test ', @:)
572 call feedkeys(":profdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
573 call assert_equal('"profdel func X1B2C3', @:)
574
575 delfunc Xprof_test
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100576endfunc
577
578func Test_profile_errors()
579 call assert_fails("profile func Foo", 'E750:')
580 call assert_fails("profile pause", 'E750:')
581 call assert_fails("profile continue", 'E750:')
Yegappan Lakshmanan18ee0f62022-04-08 13:23:19 +0100582 call assert_fails("profile stop", 'E750:')
583 call assert_fails("profile dump", 'E750:')
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100584endfunc
Bram Moolenaarac112f02017-12-05 16:46:28 +0100585
586func Test_profile_truncate_mbyte()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100587 if &enc !=# 'utf-8'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100588 return
589 endif
590
591 let lines = [
592 \ 'scriptencoding utf-8',
593 \ 'func! Foo()',
594 \ ' return [',
595 \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",',
596 \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",',
597 \ ' \ ]',
598 \ 'endfunc',
599 \ 'call Foo()',
600 \ ]
601
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100602 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200603 call system(GetVimCommandClean()
604 \ . ' -es --cmd "set enc=utf-8"'
Bram Moolenaarac112f02017-12-05 16:46:28 +0100605 \ . ' -c "profile start Xprofile_file.log"'
606 \ . ' -c "profile file Xprofile_file.vim"'
607 \ . ' -c "so Xprofile_file.vim"'
608 \ . ' -c "qall!"')
609 call assert_equal(0, v:shell_error)
610
611 split Xprofile_file.log
612 if &fenc != ''
613 call assert_equal('utf-8', &fenc)
614 endif
615 /func! Foo()
616 let lnum = line('.')
617 call assert_match('^\s*return \[$', getline(lnum + 1))
618 call assert_match("\u4F52$", getline(lnum + 2))
619 call assert_match("\u5052$", getline(lnum + 3))
620 call assert_match('^\s*\\ \]$', getline(lnum + 4))
621 bwipe!
622
Bram Moolenaarac112f02017-12-05 16:46:28 +0100623 call delete('Xprofile_file.log')
624endfunc
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200625
626func Test_profdel_func()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200627 let lines =<< trim [CODE]
628 profile start Xprofile_file.log
629 func! Foo1()
630 endfunc
631 func! Foo2()
632 endfunc
633 func! Foo3()
634 endfunc
635
636 profile func Foo1
637 profile func Foo2
638 call Foo1()
639 call Foo2()
640
641 profile func Foo3
642 profdel func Foo2
643 profdel func Foo3
644 call Foo1()
645 call Foo2()
646 call Foo3()
647 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100648 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200649 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200650 call assert_equal(0, v:shell_error)
651
652 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200653 call assert_equal(26, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200654
655 " Check that:
656 " - Foo1() is called twice (profdel not invoked)
657 " - Foo2() is called once (profdel invoked after it was called)
658 " - Foo3() is not called (profdel invoked before it was called)
659 call assert_equal('FUNCTION Foo1()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200660 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
661 call assert_equal('Called 2 times', lines[2])
662 call assert_equal('FUNCTION Foo2()', lines[8])
663 call assert_equal('Called 1 time', lines[10])
664 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16])
665 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200666
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200667 call delete('Xprofile_file.log')
668endfunc
669
670func Test_profdel_star()
671 " Foo() is invoked once before and once after 'profdel *'.
672 " So profiling should report it only once.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200673 let lines =<< trim [CODE]
674 profile start Xprofile_file.log
675 func! Foo()
676 endfunc
677 profile func Foo
678 call Foo()
679 profdel *
680 call Foo()
681 [CODE]
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100682 call writefile(lines, 'Xprofile_file.vim', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200683 call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q')
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200684 call assert_equal(0, v:shell_error)
685
686 let lines = readfile('Xprofile_file.log')
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200687 call assert_equal(16, len(lines))
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200688
689 call assert_equal('FUNCTION Foo()', lines[0])
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +0200690 call assert_match('Defined:.*Xprofile_file.vim', lines[1])
691 call assert_equal('Called 1 time', lines[2])
692 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8])
693 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12])
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200694
Bram Moolenaar1fbfe7c2018-06-30 21:18:13 +0200695 call delete('Xprofile_file.log')
696endfunc
Bram Moolenaar16358802019-08-30 18:37:26 +0200697
698" When typing the function it won't have a script ID, test that this works.
699func Test_profile_typed_func()
700 CheckScreendump
701
702 let lines =<< trim END
703 profile start XprofileTypedFunc
704 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100705 call writefile(lines, 'XtestProfile', 'D')
Bram Moolenaar16358802019-08-30 18:37:26 +0200706 let buf = RunVimInTerminal('-S XtestProfile', #{})
707
708 call term_sendkeys(buf, ":func DoSomething()\<CR>"
709 \ .. "echo 'hello'\<CR>"
710 \ .. "endfunc\<CR>")
711 call term_sendkeys(buf, ":profile func DoSomething\<CR>")
712 call term_sendkeys(buf, ":call DoSomething()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200713 call TermWait(buf, 100)
Bram Moolenaar16358802019-08-30 18:37:26 +0200714 call StopVimInTerminal(buf)
715 let lines = readfile('XprofileTypedFunc')
716 call assert_equal("FUNCTION DoSomething()", lines[0])
717 call assert_equal("Called 1 time", lines[1])
718
719 " clean up
720 call delete('XprofileTypedFunc')
Bram Moolenaar16358802019-08-30 18:37:26 +0200721endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200722
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100723func Test_vim9_profiling()
724 " only tests that compiling and calling functions doesn't crash
725 let lines =<< trim END
726 vim9script
727 def Func()
728 Crash()
729 enddef
730 def Crash()
731 enddef
Bram Moolenaar98989a02021-01-26 12:06:30 +0100732 prof start Xprofile_crash.log
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100733 prof func Func
734 Func()
735 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100736 call writefile(lines, 'Xprofile_crash.vim', 'D')
Bram Moolenaar98989a02021-01-26 12:06:30 +0100737 call system(GetVimCommandClean() . ' -es -c "so Xprofile_crash.vim" -c q')
738 call assert_equal(0, v:shell_error)
Bram Moolenaar8c801b32021-03-05 20:58:22 +0100739 call assert_true(readfile('Xprofile_crash.log')->len() > 10)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100740
Bram Moolenaar98989a02021-01-26 12:06:30 +0100741 call delete('Xprofile_crash.log')
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100742endfunc
743
Bram Moolenaar12d26532021-02-19 19:13:21 +0100744func Test_vim9_nested_call()
745 let lines =<< trim END
746 vim9script
747 var total = 0
748 def One(Ref: func(number))
749 for i in range(3)
750 Ref(i)
751 endfor
752 enddef
753 def Two(nr: number)
754 total += nr
755 enddef
756 prof start Xprofile_nested.log
757 prof func One
758 prof func Two
759 One((nr) => Two(nr))
760 assert_equal(3, total)
761 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100762 call writefile(lines, 'Xprofile_nested.vim', 'D')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100763 call system(GetVimCommandClean() . ' -es -c "so Xprofile_nested.vim" -c q')
764 call assert_equal(0, v:shell_error)
765
766 let prof_lines = readfile('Xprofile_nested.log')->join('#')
767 call assert_match('FUNCTION <SNR>\d\+_One().*'
768 \ .. '#Called 1 time.*'
769 \ .. '# 1 \s*[0-9.]\+ for i in range(3)'
770 \ .. '# 3 \s*[0-9.]\+ \s*[0-9.]\+ Ref(i)'
771 \ .. '# 3 \s*[0-9.]\+ endfor', prof_lines)
772 call assert_match('FUNCTION <SNR>\d\+_Two().*'
773 \ .. '#Called 3 times.*'
774 \ .. '# 3 \s*[0-9.]\+ total += nr', prof_lines)
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100775
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100776 call delete('Xprofile_nested.log')
Bram Moolenaar12d26532021-02-19 19:13:21 +0100777endfunc
778
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100779
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200780" vim: shiftwidth=2 sts=2 expandtab