blob: 13e7eec876aac71715b3a59d4069d0bf066689ee [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
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010095 call writefile(lines, testfile, 'D')
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()
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100117endfunc
118
119func Test_memory_func_capture_lvars()
120 " Case: if a local variable captures l: dict, funccall object will not be
121 " free until garbage collector runs, but after that memory usage doesn't
122 " increase so much even when rerun Xtest.vim since system memory caches.
123 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200124 let lines =<< trim END
125 func s:f()
126 let x = l:
127 endfunc
128 for _ in range(10000)
129 call s:f()
130 endfor
131 END
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100132 call writefile(lines, testfile, 'D')
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100133
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
Bram Moolenaar1832d122020-01-01 15:17:25 +0100151 " otherwise the test fails sometimes. On Cirrus CI with FreeBSD we need to
Bram Moolenaar6bce5852021-03-13 22:11:51 +0100152 " be even much more permissive.
Bram Moolenaar1832d122020-01-01 15:17:25 +0100153 if has('bsd')
Bram Moolenaar6bce5852021-03-13 22:11:51 +0100154 let multiplier = 19
Bram Moolenaar1832d122020-01-01 15:17:25 +0100155 else
156 let multiplier = 12
157 endif
Bram Moolenaar08cda652019-03-20 22:45:01 +0100158 let lower = before * 8 / 10
Bram Moolenaar1832d122020-01-01 15:17:25 +0100159 let upper = (after.max + (after.last - before)) * multiplier / 10
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100160 call assert_inrange(lower, upper, last)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100161
162 call vim.stop()
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100163endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200164
165" vim: shiftwidth=2 sts=2 expandtab