blob: eadd4ea9700721775ba5e32bff878fc15c3edeb0 [file] [log] [blame]
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001" Tests for memory usage.
2
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02003if !has('terminal')
4 throw 'Skipped, terminal feature missing'
5endif
6if has('gui_running')
7 throw 'Skipped, does not work in GUI'
8endif
9if $ASAN_OPTIONS !=# ''
Bram Moolenaar209b8e32019-03-14 13:43:24 +010010 " Skip tests on Travis CI ASAN build because it's difficult to estimate
11 " memory usage.
Bram Moolenaarb0f94c12019-06-13 22:19:53 +020012 throw 'Skipped, does not work with ASAN'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010013endif
14
15source shared.vim
16
17func s:pick_nr(str) abort
18 return substitute(a:str, '[^0-9]', '', 'g') * 1
19endfunc
20
21if has('win32')
22 if !executable('wmic')
Bram Moolenaarb0f94c12019-06-13 22:19:53 +020023 throw 'Skipped, wmic program missing'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010024 endif
25 func s:memory_usage(pid) abort
26 let cmd = printf('wmic process where processid=%d get WorkingSetSize', a:pid)
27 return s:pick_nr(system(cmd)) / 1024
28 endfunc
29elseif has('unix')
30 if !executable('ps')
Bram Moolenaarb0f94c12019-06-13 22:19:53 +020031 throw 'Skipped, ps program missing'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010032 endif
33 func s:memory_usage(pid) abort
34 return s:pick_nr(system('ps -o rss= -p ' . a:pid))
35 endfunc
36else
Bram Moolenaarb0f94c12019-06-13 22:19:53 +020037 throw 'Skipped, not win32 or unix'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010038endif
39
40" Wait for memory usage to level off.
41func s:monitor_memory_usage(pid) abort
42 let proc = {}
43 let proc.pid = a:pid
44 let proc.hist = []
Bram Moolenaar209b8e32019-03-14 13:43:24 +010045 let proc.max = 0
46
47 func proc.op() abort
48 " Check the last 200ms.
49 let val = s:memory_usage(self.pid)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +010050 if self.max < val
Bram Moolenaar209b8e32019-03-14 13:43:24 +010051 let self.max = val
52 endif
53 call add(self.hist, val)
54 if len(self.hist) < 20
55 return 0
56 endif
57 let sample = remove(self.hist, 0)
58 return len(uniq([sample] + self.hist)) == 1
59 endfunc
60
61 call WaitFor({-> proc.op()}, 10000)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +010062 return {'last': get(proc.hist, -1), 'max': proc.max}
Bram Moolenaar209b8e32019-03-14 13:43:24 +010063endfunc
64
65let s:term_vim = {}
66
67func s:term_vim.start(...) abort
68 let self.buf = term_start([GetVimProg()] + a:000)
69 let self.job = term_getjob(self.buf)
70 call WaitFor({-> job_status(self.job) ==# 'run'})
71 let self.pid = job_info(self.job).process
72endfunc
73
74func s:term_vim.stop() abort
75 call term_sendkeys(self.buf, ":qall!\<CR>")
76 call WaitFor({-> job_status(self.job) ==# 'dead'})
77 exe self.buf . 'bwipe!'
78endfunc
79
80func s:vim_new() abort
81 return copy(s:term_vim)
82endfunc
83
84func Test_memory_func_capture_vargs()
85 " Case: if a local variable captures a:000, funccall object will be free
86 " just after it finishes.
87 let testfile = 'Xtest.vim'
88 call writefile([
89 \ 'func s:f(...)',
90 \ ' let x = a:000',
91 \ 'endfunc',
92 \ 'for _ in range(10000)',
93 \ ' call s:f(0)',
94 \ 'endfor',
95 \ ], testfile)
96
97 let vim = s:vim_new()
98 call vim.start('--clean', '-c', 'set noswapfile', testfile)
99 let before = s:monitor_memory_usage(vim.pid).last
100
101 call term_sendkeys(vim.buf, ":so %\<CR>")
102 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
103 let after = s:monitor_memory_usage(vim.pid)
104
105 " Estimate the limit of max usage as 2x initial usage.
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200106 " The lower limit can fluctuate a bit, use 97%.
107 call assert_inrange(before * 97 / 100, 2 * before, after.max)
Bram Moolenaar3a731ee2019-03-27 21:41:36 +0100108
109 " In this case, garbage collecting is not needed.
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200110 " The value might fluctuate a bit, allow for 3% tolerance below and 5% above.
111 " Based on various test runs.
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100112 let lower = after.last * 97 / 100
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200113 let upper = after.last * 105 / 100
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100114 call assert_inrange(lower, upper, after.max)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100115
116 call vim.stop()
117 call delete(testfile)
118endfunc
119
120func Test_memory_func_capture_lvars()
121 " Case: if a local variable captures l: dict, funccall object will not be
122 " free until garbage collector runs, but after that memory usage doesn't
123 " increase so much even when rerun Xtest.vim since system memory caches.
124 let testfile = 'Xtest.vim'
125 call writefile([
126 \ 'func s:f()',
127 \ ' let x = l:',
128 \ 'endfunc',
129 \ 'for _ in range(10000)',
130 \ ' call s:f()',
131 \ 'endfor',
132 \ ], testfile)
133
134 let vim = s:vim_new()
135 call vim.start('--clean', '-c', 'set noswapfile', testfile)
136 let before = s:monitor_memory_usage(vim.pid).last
137
138 call term_sendkeys(vim.buf, ":so %\<CR>")
139 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
140 let after = s:monitor_memory_usage(vim.pid)
141
142 " Rerun Xtest.vim.
143 for _ in range(3)
144 call term_sendkeys(vim.buf, ":so %\<CR>")
145 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
146 let last = s:monitor_memory_usage(vim.pid).last
147 endfor
148
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100149 " The usage may be a bit less than the last value, use 80%.
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100150 " Allow for 20% tolerance at the upper limit. That's very permissive, but
151 " otherwise the test fails sometimes.
Bram Moolenaar08cda652019-03-20 22:45:01 +0100152 let lower = before * 8 / 10
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100153 let upper = (after.max + (after.last - before)) * 12 / 10
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100154 call assert_inrange(lower, upper, last)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100155
156 call vim.stop()
157 call delete(testfile)
158endfunc