blob: 5f15d51b4719be8d66461fb87cc894f521bbe85a [file] [log] [blame]
Bram Moolenaar296b1f22017-01-15 15:22:33 +01001" Test Vim profiler
2if !has('profile')
3 finish
4endif
5
6func Test_profile_func()
Bram Moolenaar296b1f22017-01-15 15:22:33 +01007 let lines = [
8 \ "func! Foo1()",
9 \ "endfunc",
10 \ "func! Foo2()",
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010011 \ " let l:count = 100",
12 \ " while l:count > 0",
13 \ " let l:count = l:count - 1",
Bram Moolenaar296b1f22017-01-15 15:22:33 +010014 \ " endwhile",
15 \ "endfunc",
16 \ "func! Foo3()",
17 \ "endfunc",
18 \ "func! Bar()",
19 \ "endfunc",
20 \ "call Foo1()",
21 \ "call Foo1()",
22 \ "profile pause",
23 \ "call Foo1()",
24 \ "profile continue",
25 \ "call Foo2()",
26 \ "call Foo3()",
27 \ "call Bar()",
28 \ "if !v:profiling",
29 \ " delfunc Foo2",
30 \ "endif",
31 \ "delfunc Foo3",
32 \ ]
33
34 call writefile(lines, 'Xprofile_func.vim')
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010035 call system(v:progpath
36 \ . ' -es -u NONE -U NONE -i NONE --noplugin'
37 \ . ' -c "profile start Xprofile_func.log"'
38 \ . ' -c "profile func Foo*"'
39 \ . ' -c "so Xprofile_func.vim"'
40 \ . ' -c "qall!"')
41 call assert_equal(0, v:shell_error)
42
Bram Moolenaar296b1f22017-01-15 15:22:33 +010043 let lines = readfile('Xprofile_func.log')
44
Bram Moolenaar296b1f22017-01-15 15:22:33 +010045 " - Foo1() is called 3 times but should be reported as called twice
46 " since one call is in between "profile pause" .. "profile continue".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010047 " - Foo2() should come before Foo1() since Foo1() does much more work.
Bram Moolenaar296b1f22017-01-15 15:22:33 +010048 " - Foo3() is not reported because function is deleted.
49 " - Unlike Foo3(), Foo2() should not be deleted since there is a check
50 " for v:profiling.
51 " - Bar() is not reported since it does not match "profile func Foo*".
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010052 call assert_equal(28, len(lines))
53
54 call assert_equal('FUNCTION Foo1()', lines[0])
55 call assert_equal('Called 2 times', lines[1])
56 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
57 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
58 call assert_equal('', lines[4])
59 call assert_equal('count total (s) self (s)', lines[5])
60 call assert_equal('', lines[6])
61 call assert_equal('FUNCTION Foo2()', lines[7])
62 call assert_equal('Called 1 time', lines[8])
63 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[9])
64 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[10])
65 call assert_equal('', lines[11])
66 call assert_equal('count total (s) self (s)', lines[12])
67 call assert_match('^\s*1\s\+.*\slet l:count = 100$', lines[13])
68 call assert_match('^\s*101\s\+.*\swhile l:count > 0$', lines[14])
69 call assert_match('^\s*100\s\+.*\s let l:count = l:count - 1$', lines[15])
70 call assert_match('^\s*100\s\+.*\sendwhile$', lines[16])
71 call assert_equal('', lines[17])
72 call assert_equal('FUNCTIONS SORTED ON TOTAL TIME', lines[18])
73 call assert_equal('count total (s) self (s) function', lines[19])
74 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[20])
75 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[21])
76 call assert_equal('', lines[22])
77 call assert_equal('FUNCTIONS SORTED ON SELF TIME', lines[23])
78 call assert_equal('count total (s) self (s) function', lines[24])
79 call assert_match('^\s*1\s\+\d\+\.\d\+\s\+Foo2()$', lines[25])
80 call assert_match('^\s*2\s\+\d\+\.\d\+\s\+Foo1()$', lines[26])
81 call assert_equal('', lines[27])
Bram Moolenaar296b1f22017-01-15 15:22:33 +010082
83 call delete('Xprofile_func.vim')
84 call delete('Xprofile_func.log')
85endfunc
86
87func Test_profile_file()
Bram Moolenaar296b1f22017-01-15 15:22:33 +010088 let lines = [
89 \ 'func! Foo()',
90 \ 'endfunc',
91 \ 'for i in range(10)',
92 \ ' " a comment',
93 \ ' call Foo()',
94 \ 'endfor',
95 \ 'call Foo()',
96 \ ]
97
98 call writefile(lines, 'Xprofile_file.vim')
Bram Moolenaarc011a3d2017-01-16 22:37:42 +010099 call system(v:progpath
100 \ . ' -es -u NONE -U NONE -i NONE --noplugin'
101 \ . ' -c "profile start Xprofile_file.log"'
102 \ . ' -c "profile file Xprofile_file.vim"'
103 \ . ' -c "so Xprofile_file.vim"'
104 \ . ' -c "so Xprofile_file.vim"'
105 \ . ' -c "qall!"')
106 call assert_equal(0, v:shell_error)
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100107
108 let lines = readfile('Xprofile_file.log')
109
110 call assert_equal(14, len(lines))
111
112 call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
113 call assert_equal('Sourced 2 times', lines[1])
114 call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
115 call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
116 call assert_equal('', lines[4])
117 call assert_equal('count total (s) self (s)', lines[5])
118 call assert_equal(' func! Foo()', lines[6])
119 call assert_equal(' endfunc', lines[7])
120 " Loop iterates 10 times. Since script runs twice, body executes 20 times.
121 " First line of loop executes one more time than body to detect end of loop.
122 call assert_match('^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$', lines[8])
123 call assert_equal(' " a comment', lines[9])
124 call assert_match('^\s*20\s\+\d\+\.\d\+\s\+\d\+\.\d\+\s\+call Foo()$', lines[10])
125 call assert_match('^\s*20\s\+\d\+\.\d\+\s\+endfor$', lines[11])
Bram Moolenaare32bbde2017-01-15 21:12:48 +0100126 " if self and total are equal we only get one number
127 call assert_match('^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$', lines[12])
Bram Moolenaar296b1f22017-01-15 15:22:33 +0100128 call assert_equal('', lines[13])
129
130 call delete('Xprofile_file.vim')
131 call delete('Xprofile_file.log')
132endfunc
133
134func Test_profile_completion()
135 call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
136 call assert_equal('"profile continue file func pause start', @:)
137
138 call feedkeys(":profile start test_prof\<C-A>\<C-B>\"\<CR>", 'tx')
139 call assert_match('^"profile start.* test_profile\.vim', @:)
140endfunc
141
142func Test_profile_errors()
143 call assert_fails("profile func Foo", 'E750:')
144 call assert_fails("profile pause", 'E750:')
145 call assert_fails("profile continue", 'E750:')
146endfunc