Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 1 | " Test Vim profiler |
Bram Moolenaar | b0f94c1 | 2019-06-13 22:19:53 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature profile |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 5 | |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 6 | source shared.vim |
Bram Moolenaar | 1635880 | 2019-08-30 18:37:26 +0200 | [diff] [blame] | 7 | source screendump.vim |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 8 | |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 9 | func Test_profile_func() |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 10 | call RunProfileFunc('func', 'let', 'let') |
| 11 | call RunProfileFunc('def', 'var', '') |
| 12 | endfunc |
| 13 | |
| 14 | func RunProfileFunc(command, declare, assign) |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 15 | let lines =<< trim [CODE] |
| 16 | profile start Xprofile_func.log |
| 17 | profile func Foo* |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 18 | XXX Foo1() |
| 19 | endXXX |
| 20 | XXX Foo2() |
| 21 | DDD counter = 100 |
| 22 | while counter > 0 |
| 23 | AAA counter = counter - 1 |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 24 | endwhile |
Bram Moolenaar | a21df1d | 2020-03-17 20:57:09 +0100 | [diff] [blame] | 25 | sleep 1m |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 26 | endXXX |
| 27 | XXX Foo3() |
| 28 | endXXX |
| 29 | XXX Bar() |
| 30 | endXXX |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 31 | 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 Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 44 | |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 45 | 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 Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 49 | call writefile(lines, 'Xprofile_func.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 50 | call system(GetVimCommand() |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 51 | \ . ' -es --clean' |
Bram Moolenaar | c011a3d | 2017-01-16 22:37:42 +0100 | [diff] [blame] | 52 | \ . ' -c "so Xprofile_func.vim"' |
| 53 | \ . ' -c "qall!"') |
| 54 | call assert_equal(0, v:shell_error) |
| 55 | |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 56 | let lines = readfile('Xprofile_func.log') |
| 57 | |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 58 | " - Foo1() is called 3 times but should be reported as called twice |
| 59 | " since one call is in between "profile pause" .. "profile continue". |
Bram Moolenaar | c011a3d | 2017-01-16 22:37:42 +0100 | [diff] [blame] | 60 | " - Foo2() should come before Foo1() since Foo1() does much more work. |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 61 | " - 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 Moolenaar | a21df1d | 2020-03-17 20:57:09 +0100 | [diff] [blame] | 65 | call assert_equal(31, len(lines)) |
Bram Moolenaar | c011a3d | 2017-01-16 22:37:42 +0100 | [diff] [blame] | 66 | |
| 67 | call assert_equal('FUNCTION Foo1()', lines[0]) |
Bram Moolenaar | 181d4f5 | 2019-09-18 22:04:56 +0200 | [diff] [blame] | 68 | call assert_match('Defined:.*Xprofile_func.vim:3', lines[1]) |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 69 | 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 Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 81 | 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 Moolenaar | a21df1d | 2020-03-17 20:57:09 +0100 | [diff] [blame] | 85 | 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 Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 97 | |
| 98 | call delete('Xprofile_func.vim') |
| 99 | call delete('Xprofile_func.log') |
| 100 | endfunc |
| 101 | |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 102 | func Test_profile_func_with_ifelse() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 103 | let lines =<< trim [CODE] |
| 104 | func! Foo1() |
| 105 | if 1 |
| 106 | let x = 0 |
| 107 | elseif 1 |
| 108 | let x = 1 |
| 109 | else |
| 110 | let x = 2 |
| 111 | endif |
| 112 | endfunc |
| 113 | func! Foo2() |
| 114 | if 0 |
| 115 | let x = 0 |
| 116 | elseif 1 |
| 117 | let x = 1 |
| 118 | else |
| 119 | let x = 2 |
| 120 | endif |
| 121 | endfunc |
| 122 | func! Foo3() |
| 123 | if 0 |
| 124 | let x = 0 |
| 125 | elseif 0 |
| 126 | let x = 1 |
| 127 | else |
| 128 | let x = 2 |
| 129 | endif |
| 130 | endfunc |
| 131 | call Foo1() |
| 132 | call Foo2() |
| 133 | call Foo3() |
| 134 | [CODE] |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 135 | |
| 136 | call writefile(lines, 'Xprofile_func.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 137 | call system(GetVimCommand() |
| 138 | \ . ' -es -i NONE --noplugin' |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 139 | \ . ' -c "profile start Xprofile_func.log"' |
| 140 | \ . ' -c "profile func Foo*"' |
| 141 | \ . ' -c "so Xprofile_func.vim"' |
| 142 | \ . ' -c "qall!"') |
| 143 | call assert_equal(0, v:shell_error) |
| 144 | |
| 145 | let lines = readfile('Xprofile_func.log') |
| 146 | |
| 147 | " - Foo1() should pass 'if' block. |
| 148 | " - Foo2() should pass 'elseif' block. |
| 149 | " - Foo3() should pass 'else' block. |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 150 | call assert_equal(57, len(lines)) |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 151 | |
| 152 | call assert_equal('FUNCTION Foo1()', lines[0]) |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 153 | call assert_match('Defined:.*Xprofile_func.vim', lines[1]) |
| 154 | call assert_equal('Called 1 time', lines[2]) |
| 155 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3]) |
| 156 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4]) |
| 157 | call assert_equal('', lines[5]) |
| 158 | call assert_equal('count total (s) self (s)', lines[6]) |
| 159 | call assert_match('^\s*1\s\+.*\sif 1$', lines[7]) |
| 160 | call assert_match('^\s*1\s\+.*\s let x = 0$', lines[8]) |
| 161 | call assert_match( '^\s\+elseif 1$', lines[9]) |
| 162 | call assert_match( '^\s\+let x = 1$', lines[10]) |
| 163 | call assert_match( '^\s\+else$', lines[11]) |
| 164 | call assert_match( '^\s\+let x = 2$', lines[12]) |
| 165 | call assert_match('^\s*1\s\+.*\sendif$', lines[13]) |
| 166 | call assert_equal('', lines[14]) |
| 167 | call assert_equal('FUNCTION Foo2()', lines[15]) |
| 168 | call assert_equal('Called 1 time', lines[17]) |
| 169 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18]) |
| 170 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19]) |
| 171 | call assert_equal('', lines[20]) |
| 172 | call assert_equal('count total (s) self (s)', lines[21]) |
| 173 | call assert_match('^\s*1\s\+.*\sif 0$', lines[22]) |
| 174 | call assert_match( '^\s\+let x = 0$', lines[23]) |
| 175 | call assert_match('^\s*1\s\+.*\selseif 1$', lines[24]) |
| 176 | call assert_match('^\s*1\s\+.*\s let x = 1$', lines[25]) |
| 177 | call assert_match( '^\s\+else$', lines[26]) |
| 178 | call assert_match( '^\s\+let x = 2$', lines[27]) |
| 179 | call assert_match('^\s*1\s\+.*\sendif$', lines[28]) |
| 180 | call assert_equal('', lines[29]) |
| 181 | call assert_equal('FUNCTION Foo3()', lines[30]) |
| 182 | call assert_equal('Called 1 time', lines[32]) |
| 183 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33]) |
| 184 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34]) |
| 185 | call assert_equal('', lines[35]) |
| 186 | call assert_equal('count total (s) self (s)', lines[36]) |
| 187 | call assert_match('^\s*1\s\+.*\sif 0$', lines[37]) |
| 188 | call assert_match( '^\s\+let x = 0$', lines[38]) |
| 189 | call assert_match('^\s*1\s\+.*\selseif 0$', lines[39]) |
| 190 | call assert_match( '^\s\+let x = 1$', lines[40]) |
| 191 | call assert_match('^\s*1\s\+.*\selse$', lines[41]) |
| 192 | call assert_match('^\s*1\s\+.*\s let x = 2$', lines[42]) |
| 193 | call assert_match('^\s*1\s\+.*\sendif$', lines[43]) |
| 194 | call assert_equal('', lines[44]) |
| 195 | call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45]) |
| 196 | call assert_equal('count total (s) self (s) function', lines[46]) |
| 197 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47]) |
| 198 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48]) |
| 199 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49]) |
| 200 | call assert_equal('', lines[50]) |
| 201 | call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51]) |
| 202 | call assert_equal('count total (s) self (s) function', lines[52]) |
| 203 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53]) |
| 204 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54]) |
| 205 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55]) |
| 206 | call assert_equal('', lines[56]) |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 207 | |
| 208 | call delete('Xprofile_func.vim') |
| 209 | call delete('Xprofile_func.log') |
| 210 | endfunc |
| 211 | |
| 212 | func Test_profile_func_with_trycatch() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 213 | let lines =<< trim [CODE] |
| 214 | func! Foo1() |
| 215 | try |
| 216 | let x = 0 |
| 217 | catch |
| 218 | let x = 1 |
| 219 | finally |
| 220 | let x = 2 |
| 221 | endtry |
| 222 | endfunc |
| 223 | func! Foo2() |
| 224 | try |
| 225 | throw 0 |
| 226 | catch |
| 227 | let x = 1 |
| 228 | finally |
| 229 | let x = 2 |
| 230 | endtry |
| 231 | endfunc |
| 232 | func! Foo3() |
| 233 | try |
| 234 | throw 0 |
| 235 | catch |
| 236 | throw 1 |
| 237 | finally |
| 238 | let x = 2 |
| 239 | endtry |
| 240 | endfunc |
| 241 | call Foo1() |
| 242 | call Foo2() |
| 243 | try |
| 244 | call Foo3() |
| 245 | catch |
| 246 | endtry |
| 247 | [CODE] |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 248 | |
| 249 | call writefile(lines, 'Xprofile_func.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 250 | call system(GetVimCommand() |
| 251 | \ . ' -es -i NONE --noplugin' |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 252 | \ . ' -c "profile start Xprofile_func.log"' |
| 253 | \ . ' -c "profile func Foo*"' |
| 254 | \ . ' -c "so Xprofile_func.vim"' |
| 255 | \ . ' -c "qall!"') |
| 256 | call assert_equal(0, v:shell_error) |
| 257 | |
| 258 | let lines = readfile('Xprofile_func.log') |
| 259 | |
| 260 | " - Foo1() should pass 'try' 'finally' blocks. |
| 261 | " - Foo2() should pass 'catch' 'finally' blocks. |
| 262 | " - Foo3() should not pass 'endtry'. |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 263 | call assert_equal(57, len(lines)) |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 264 | |
| 265 | call assert_equal('FUNCTION Foo1()', lines[0]) |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 266 | call assert_match('Defined:.*Xprofile_func.vim', lines[1]) |
| 267 | call assert_equal('Called 1 time', lines[2]) |
| 268 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3]) |
| 269 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4]) |
| 270 | call assert_equal('', lines[5]) |
| 271 | call assert_equal('count total (s) self (s)', lines[6]) |
| 272 | call assert_match('^\s*1\s\+.*\stry$', lines[7]) |
| 273 | call assert_match('^\s*1\s\+.*\s let x = 0$', lines[8]) |
| 274 | call assert_match( '^\s\+catch$', lines[9]) |
| 275 | call assert_match( '^\s\+let x = 1$', lines[10]) |
| 276 | call assert_match('^\s*1\s\+.*\sfinally$', lines[11]) |
| 277 | call assert_match('^\s*1\s\+.*\s let x = 2$', lines[12]) |
| 278 | call assert_match('^\s*1\s\+.*\sendtry$', lines[13]) |
| 279 | call assert_equal('', lines[14]) |
| 280 | call assert_equal('FUNCTION Foo2()', lines[15]) |
| 281 | call assert_equal('Called 1 time', lines[17]) |
| 282 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[18]) |
| 283 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[19]) |
| 284 | call assert_equal('', lines[20]) |
| 285 | call assert_equal('count total (s) self (s)', lines[21]) |
| 286 | call assert_match('^\s*1\s\+.*\stry$', lines[22]) |
| 287 | call assert_match('^\s*1\s\+.*\s throw 0$', lines[23]) |
| 288 | call assert_match('^\s*1\s\+.*\scatch$', lines[24]) |
| 289 | call assert_match('^\s*1\s\+.*\s let x = 1$', lines[25]) |
| 290 | call assert_match('^\s*1\s\+.*\sfinally$', lines[26]) |
| 291 | call assert_match('^\s*1\s\+.*\s let x = 2$', lines[27]) |
| 292 | call assert_match('^\s*1\s\+.*\sendtry$', lines[28]) |
| 293 | call assert_equal('', lines[29]) |
| 294 | call assert_equal('FUNCTION Foo3()', lines[30]) |
| 295 | call assert_equal('Called 1 time', lines[32]) |
| 296 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[33]) |
| 297 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[34]) |
| 298 | call assert_equal('', lines[35]) |
| 299 | call assert_equal('count total (s) self (s)', lines[36]) |
| 300 | call assert_match('^\s*1\s\+.*\stry$', lines[37]) |
| 301 | call assert_match('^\s*1\s\+.*\s throw 0$', lines[38]) |
| 302 | call assert_match('^\s*1\s\+.*\scatch$', lines[39]) |
| 303 | call assert_match('^\s*1\s\+.*\s throw 1$', lines[40]) |
| 304 | call assert_match('^\s*1\s\+.*\sfinally$', lines[41]) |
| 305 | call assert_match('^\s*1\s\+.*\s let x = 2$', lines[42]) |
| 306 | call assert_match( '^\s\+endtry$', lines[43]) |
| 307 | call assert_equal('', lines[44]) |
| 308 | call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[45]) |
| 309 | call assert_equal('count total (s) self (s) function', lines[46]) |
| 310 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[47]) |
| 311 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[48]) |
| 312 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[49]) |
| 313 | call assert_equal('', lines[50]) |
| 314 | call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[51]) |
| 315 | call assert_equal('count total (s) self (s) function', lines[52]) |
| 316 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[53]) |
| 317 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[54]) |
| 318 | call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo.()$', lines[55]) |
| 319 | call assert_equal('', lines[56]) |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 320 | |
| 321 | call delete('Xprofile_func.vim') |
| 322 | call delete('Xprofile_func.log') |
| 323 | endfunc |
| 324 | |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 325 | func Test_profile_file() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 326 | let lines =<< trim [CODE] |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 327 | func! Foo() |
| 328 | endfunc |
| 329 | for i in range(10) |
| 330 | " a comment |
| 331 | call Foo() |
| 332 | endfor |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 333 | call Foo() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 334 | [CODE] |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 335 | |
| 336 | call writefile(lines, 'Xprofile_file.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 337 | call system(GetVimCommandClean() |
| 338 | \ . ' -es' |
Bram Moolenaar | c011a3d | 2017-01-16 22:37:42 +0100 | [diff] [blame] | 339 | \ . ' -c "profile start Xprofile_file.log"' |
| 340 | \ . ' -c "profile file Xprofile_file.vim"' |
| 341 | \ . ' -c "so Xprofile_file.vim"' |
| 342 | \ . ' -c "so Xprofile_file.vim"' |
| 343 | \ . ' -c "qall!"') |
| 344 | call assert_equal(0, v:shell_error) |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 345 | |
| 346 | let lines = readfile('Xprofile_file.log') |
| 347 | |
| 348 | call assert_equal(14, len(lines)) |
| 349 | |
| 350 | call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0]) |
| 351 | call assert_equal('Sourced 2 times', lines[1]) |
| 352 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2]) |
| 353 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3]) |
| 354 | call assert_equal('', lines[4]) |
| 355 | call assert_equal('count total (s) self (s)', lines[5]) |
Bram Moolenaar | 67435d9 | 2017-10-19 21:04:37 +0200 | [diff] [blame] | 356 | call assert_match(' 2 0.\d\+ func! Foo()', lines[6]) |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 357 | call assert_equal(' endfunc', lines[7]) |
| 358 | " Loop iterates 10 times. Since script runs twice, body executes 20 times. |
| 359 | " First line of loop executes one more time than body to detect end of loop. |
| 360 | call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8]) |
| 361 | call assert_equal(' " a comment', lines[9]) |
Bram Moolenaar | d21b16f | 2017-08-14 23:13:30 +0200 | [diff] [blame] | 362 | " if self and total are equal we only get one number |
| 363 | call assert_match('^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[10]) |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 364 | call assert_match('^\s*22\s\+\d\+\.\d\+\s\+endfor$', lines[11]) |
Bram Moolenaar | e32bbde | 2017-01-15 21:12:48 +0100 | [diff] [blame] | 365 | " if self and total are equal we only get one number |
| 366 | call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12]) |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 367 | call assert_equal('', lines[13]) |
| 368 | |
| 369 | call delete('Xprofile_file.vim') |
| 370 | call delete('Xprofile_file.log') |
| 371 | endfunc |
| 372 | |
Bram Moolenaar | 67435d9 | 2017-10-19 21:04:37 +0200 | [diff] [blame] | 373 | func Test_profile_file_with_cont() |
| 374 | let lines = [ |
| 375 | \ 'echo "hello', |
| 376 | \ ' \ world"', |
| 377 | \ 'echo "foo ', |
| 378 | \ ' \bar"', |
| 379 | \ ] |
| 380 | |
| 381 | call writefile(lines, 'Xprofile_file.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 382 | call system(GetVimCommandClean() |
| 383 | \ . ' -es' |
Bram Moolenaar | 67435d9 | 2017-10-19 21:04:37 +0200 | [diff] [blame] | 384 | \ . ' -c "profile start Xprofile_file.log"' |
| 385 | \ . ' -c "profile file Xprofile_file.vim"' |
| 386 | \ . ' -c "so Xprofile_file.vim"' |
| 387 | \ . ' -c "qall!"') |
| 388 | call assert_equal(0, v:shell_error) |
| 389 | |
| 390 | let lines = readfile('Xprofile_file.log') |
| 391 | call assert_equal(11, len(lines)) |
| 392 | |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 393 | call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0]) |
| 394 | call assert_equal('Sourced 1 time', lines[1]) |
| 395 | call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2]) |
| 396 | call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3]) |
| 397 | call assert_equal('', lines[4]) |
| 398 | call assert_equal('count total (s) self (s)', lines[5]) |
| 399 | call assert_match(' 1 0.\d\+ echo "hello', lines[6]) |
| 400 | call assert_equal(' \ world"', lines[7]) |
| 401 | call assert_match(' 1 0.\d\+ echo "foo ', lines[8]) |
| 402 | call assert_equal(' \bar"', lines[9]) |
| 403 | call assert_equal('', lines[10]) |
Bram Moolenaar | 67435d9 | 2017-10-19 21:04:37 +0200 | [diff] [blame] | 404 | |
| 405 | call delete('Xprofile_file.vim') |
| 406 | call delete('Xprofile_file.log') |
| 407 | endfunc |
| 408 | |
Bram Moolenaar | 296b1f2 | 2017-01-15 15:22:33 +0100 | [diff] [blame] | 409 | func Test_profile_completion() |
| 410 | call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx') |
| 411 | call assert_equal('"profile continue file func pause start', @:) |
| 412 | |
| 413 | call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx') |
| 414 | call assert_match('^"profile start.* test_profile\.vim', @:) |
| 415 | endfunc |
| 416 | |
| 417 | func Test_profile_errors() |
| 418 | call assert_fails("profile func Foo", 'E750:') |
| 419 | call assert_fails("profile pause", 'E750:') |
| 420 | call assert_fails("profile continue", 'E750:') |
| 421 | endfunc |
Bram Moolenaar | ac112f0 | 2017-12-05 16:46:28 +0100 | [diff] [blame] | 422 | |
| 423 | func Test_profile_truncate_mbyte() |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 424 | if &enc !=# 'utf-8' |
Bram Moolenaar | ac112f0 | 2017-12-05 16:46:28 +0100 | [diff] [blame] | 425 | return |
| 426 | endif |
| 427 | |
| 428 | let lines = [ |
| 429 | \ 'scriptencoding utf-8', |
| 430 | \ 'func! Foo()', |
| 431 | \ ' return [', |
| 432 | \ ' \ "' . join(map(range(0x4E00, 0x4E00 + 340), 'nr2char(v:val)'), '') . '",', |
| 433 | \ ' \ "' . join(map(range(0x4F00, 0x4F00 + 340), 'nr2char(v:val)'), '') . '",', |
| 434 | \ ' \ ]', |
| 435 | \ 'endfunc', |
| 436 | \ 'call Foo()', |
| 437 | \ ] |
| 438 | |
| 439 | call writefile(lines, 'Xprofile_file.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 440 | call system(GetVimCommandClean() |
| 441 | \ . ' -es --cmd "set enc=utf-8"' |
Bram Moolenaar | ac112f0 | 2017-12-05 16:46:28 +0100 | [diff] [blame] | 442 | \ . ' -c "profile start Xprofile_file.log"' |
| 443 | \ . ' -c "profile file Xprofile_file.vim"' |
| 444 | \ . ' -c "so Xprofile_file.vim"' |
| 445 | \ . ' -c "qall!"') |
| 446 | call assert_equal(0, v:shell_error) |
| 447 | |
| 448 | split Xprofile_file.log |
| 449 | if &fenc != '' |
| 450 | call assert_equal('utf-8', &fenc) |
| 451 | endif |
| 452 | /func! Foo() |
| 453 | let lnum = line('.') |
| 454 | call assert_match('^\s*return \[$', getline(lnum + 1)) |
| 455 | call assert_match("\u4F52$", getline(lnum + 2)) |
| 456 | call assert_match("\u5052$", getline(lnum + 3)) |
| 457 | call assert_match('^\s*\\ \]$', getline(lnum + 4)) |
| 458 | bwipe! |
| 459 | |
| 460 | call delete('Xprofile_file.vim') |
| 461 | call delete('Xprofile_file.log') |
| 462 | endfunc |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 463 | |
| 464 | func Test_profdel_func() |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 465 | let lines =<< trim [CODE] |
| 466 | profile start Xprofile_file.log |
| 467 | func! Foo1() |
| 468 | endfunc |
| 469 | func! Foo2() |
| 470 | endfunc |
| 471 | func! Foo3() |
| 472 | endfunc |
| 473 | |
| 474 | profile func Foo1 |
| 475 | profile func Foo2 |
| 476 | call Foo1() |
| 477 | call Foo2() |
| 478 | |
| 479 | profile func Foo3 |
| 480 | profdel func Foo2 |
| 481 | profdel func Foo3 |
| 482 | call Foo1() |
| 483 | call Foo2() |
| 484 | call Foo3() |
| 485 | [CODE] |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 486 | call writefile(lines, 'Xprofile_file.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 487 | call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q') |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 488 | call assert_equal(0, v:shell_error) |
| 489 | |
| 490 | let lines = readfile('Xprofile_file.log') |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 491 | call assert_equal(26, len(lines)) |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 492 | |
| 493 | " Check that: |
| 494 | " - Foo1() is called twice (profdel not invoked) |
| 495 | " - Foo2() is called once (profdel invoked after it was called) |
| 496 | " - Foo3() is not called (profdel invoked before it was called) |
| 497 | call assert_equal('FUNCTION Foo1()', lines[0]) |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 498 | call assert_match('Defined:.*Xprofile_file.vim', lines[1]) |
| 499 | call assert_equal('Called 2 times', lines[2]) |
| 500 | call assert_equal('FUNCTION Foo2()', lines[8]) |
| 501 | call assert_equal('Called 1 time', lines[10]) |
| 502 | call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[16]) |
| 503 | call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[21]) |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 504 | |
| 505 | call delete('Xprofile_file.vim') |
| 506 | call delete('Xprofile_file.log') |
| 507 | endfunc |
| 508 | |
| 509 | func Test_profdel_star() |
| 510 | " Foo() is invoked once before and once after 'profdel *'. |
| 511 | " So profiling should report it only once. |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 512 | let lines =<< trim [CODE] |
| 513 | profile start Xprofile_file.log |
| 514 | func! Foo() |
| 515 | endfunc |
| 516 | profile func Foo |
| 517 | call Foo() |
| 518 | profdel * |
| 519 | call Foo() |
| 520 | [CODE] |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 521 | call writefile(lines, 'Xprofile_file.vim') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 522 | call system(GetVimCommandClean() . ' -es -c "so Xprofile_file.vim" -c q') |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 523 | call assert_equal(0, v:shell_error) |
| 524 | |
| 525 | let lines = readfile('Xprofile_file.log') |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 526 | call assert_equal(16, len(lines)) |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 527 | |
| 528 | call assert_equal('FUNCTION Foo()', lines[0]) |
Bram Moolenaar | 4c7b08f | 2018-09-10 22:03:40 +0200 | [diff] [blame] | 529 | call assert_match('Defined:.*Xprofile_file.vim', lines[1]) |
| 530 | call assert_equal('Called 1 time', lines[2]) |
| 531 | call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[8]) |
| 532 | call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[12]) |
Bram Moolenaar | 1fbfe7c | 2018-06-30 21:18:13 +0200 | [diff] [blame] | 533 | |
| 534 | call delete('Xprofile_file.vim') |
| 535 | call delete('Xprofile_file.log') |
| 536 | endfunc |
Bram Moolenaar | 1635880 | 2019-08-30 18:37:26 +0200 | [diff] [blame] | 537 | |
| 538 | " When typing the function it won't have a script ID, test that this works. |
| 539 | func Test_profile_typed_func() |
| 540 | CheckScreendump |
| 541 | |
| 542 | let lines =<< trim END |
| 543 | profile start XprofileTypedFunc |
| 544 | END |
| 545 | call writefile(lines, 'XtestProfile') |
| 546 | let buf = RunVimInTerminal('-S XtestProfile', #{}) |
| 547 | |
| 548 | call term_sendkeys(buf, ":func DoSomething()\<CR>" |
| 549 | \ .. "echo 'hello'\<CR>" |
| 550 | \ .. "endfunc\<CR>") |
| 551 | call term_sendkeys(buf, ":profile func DoSomething\<CR>") |
| 552 | call term_sendkeys(buf, ":call DoSomething()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 553 | call TermWait(buf, 100) |
Bram Moolenaar | 1635880 | 2019-08-30 18:37:26 +0200 | [diff] [blame] | 554 | call StopVimInTerminal(buf) |
| 555 | let lines = readfile('XprofileTypedFunc') |
| 556 | call assert_equal("FUNCTION DoSomething()", lines[0]) |
| 557 | call assert_equal("Called 1 time", lines[1]) |
| 558 | |
| 559 | " clean up |
| 560 | call delete('XprofileTypedFunc') |
| 561 | call delete('XtestProfile') |
| 562 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 563 | |
| 564 | " vim: shiftwidth=2 sts=2 expandtab |