blob: 976fcde70bc05f3e95a1b831eb9deb4f15d99592 [file] [log] [blame]
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001" Tests for memory usage.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02005CheckNotGui
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02006
Bram Moolenaar97202d92021-01-28 18:34:35 +01007" Skip tests on Travis CI ASAN build because it's difficult to estimate memory
8" usage.
9CheckNotAsan
Bram Moolenaar209b8e32019-03-14 13:43:24 +010010
11source shared.vim
12
13func s:pick_nr(str) abort
14 return substitute(a:str, '[^0-9]', '', 'g') * 1
15endfunc
16
17if has('win32')
18 if !executable('wmic')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +020019 throw 'Skipped: wmic program missing'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010020 endif
21 func s:memory_usage(pid) abort
22 let cmd = printf('wmic process where processid=%d get WorkingSetSize', a:pid)
23 return s:pick_nr(system(cmd)) / 1024
24 endfunc
25elseif has('unix')
26 if !executable('ps')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +020027 throw 'Skipped: ps program missing'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028 endif
29 func s:memory_usage(pid) abort
30 return s:pick_nr(system('ps -o rss= -p ' . a:pid))
31 endfunc
32else
Bram Moolenaarb46fecd2019-06-15 17:58:09 +020033 throw 'Skipped: not win32 or unix'
Bram Moolenaar209b8e32019-03-14 13:43:24 +010034endif
35
36" Wait for memory usage to level off.
37func s:monitor_memory_usage(pid) abort
38 let proc = {}
39 let proc.pid = a:pid
40 let proc.hist = []
Bram Moolenaar209b8e32019-03-14 13:43:24 +010041 let proc.max = 0
42
43 func proc.op() abort
44 " Check the last 200ms.
45 let val = s:memory_usage(self.pid)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +010046 if self.max < val
Bram Moolenaar209b8e32019-03-14 13:43:24 +010047 let self.max = val
48 endif
49 call add(self.hist, val)
50 if len(self.hist) < 20
51 return 0
52 endif
53 let sample = remove(self.hist, 0)
54 return len(uniq([sample] + self.hist)) == 1
55 endfunc
56
57 call WaitFor({-> proc.op()}, 10000)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +010058 return {'last': get(proc.hist, -1), 'max': proc.max}
Bram Moolenaar209b8e32019-03-14 13:43:24 +010059endfunc
60
61let s:term_vim = {}
62
63func s:term_vim.start(...) abort
64 let self.buf = term_start([GetVimProg()] + a:000)
65 let self.job = term_getjob(self.buf)
66 call WaitFor({-> job_status(self.job) ==# 'run'})
67 let self.pid = job_info(self.job).process
Bram Moolenaardad5d2f2022-06-15 18:08:42 +010068
69 " running an external command may fail once in a while
70 let g:test_is_flaky = 1
Bram Moolenaar209b8e32019-03-14 13:43:24 +010071endfunc
72
73func s:term_vim.stop() abort
74 call term_sendkeys(self.buf, ":qall!\<CR>")
75 call WaitFor({-> job_status(self.job) ==# 'dead'})
76 exe self.buf . 'bwipe!'
77endfunc
78
79func s:vim_new() abort
80 return copy(s:term_vim)
81endfunc
82
83func Test_memory_func_capture_vargs()
84 " Case: if a local variable captures a:000, funccall object will be free
85 " just after it finishes.
86 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +020087 let lines =<< trim END
88 func s:f(...)
89 let x = a:000
90 endfunc
91 for _ in range(10000)
92 call s:f(0)
93 endfor
94 END
95 call writefile(lines, testfile)
Bram Moolenaar209b8e32019-03-14 13:43:24 +010096
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'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200125 let lines =<< trim END
126 func s:f()
127 let x = l:
128 endfunc
129 for _ in range(10000)
130 call s:f()
131 endfor
132 END
133 call writefile(lines, testfile)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100134
135 let vim = s:vim_new()
136 call vim.start('--clean', '-c', 'set noswapfile', testfile)
137 let before = s:monitor_memory_usage(vim.pid).last
138
139 call term_sendkeys(vim.buf, ":so %\<CR>")
140 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
141 let after = s:monitor_memory_usage(vim.pid)
142
143 " Rerun Xtest.vim.
144 for _ in range(3)
145 call term_sendkeys(vim.buf, ":so %\<CR>")
146 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
147 let last = s:monitor_memory_usage(vim.pid).last
148 endfor
149
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100150 " The usage may be a bit less than the last value, use 80%.
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100151 " Allow for 20% tolerance at the upper limit. That's very permissive, but
Bram Moolenaar1832d122020-01-01 15:17:25 +0100152 " otherwise the test fails sometimes. On Cirrus CI with FreeBSD we need to
Bram Moolenaar6bce5852021-03-13 22:11:51 +0100153 " be even much more permissive.
Bram Moolenaar1832d122020-01-01 15:17:25 +0100154 if has('bsd')
Bram Moolenaar6bce5852021-03-13 22:11:51 +0100155 let multiplier = 19
Bram Moolenaar1832d122020-01-01 15:17:25 +0100156 else
157 let multiplier = 12
158 endif
Bram Moolenaar08cda652019-03-20 22:45:01 +0100159 let lower = before * 8 / 10
Bram Moolenaar1832d122020-01-01 15:17:25 +0100160 let upper = (after.max + (after.last - before)) * multiplier / 10
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100161 call assert_inrange(lower, upper, last)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100162
163 call vim.stop()
164 call delete(testfile)
165endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200166
167" vim: shiftwidth=2 sts=2 expandtab