blob: 4f78d9a09497c40b21da9cda55450e4f4bc579c9 [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
68endfunc
69
70func s:term_vim.stop() abort
71 call term_sendkeys(self.buf, ":qall!\<CR>")
72 call WaitFor({-> job_status(self.job) ==# 'dead'})
73 exe self.buf . 'bwipe!'
74endfunc
75
76func s:vim_new() abort
77 return copy(s:term_vim)
78endfunc
79
80func Test_memory_func_capture_vargs()
81 " Case: if a local variable captures a:000, funccall object will be free
82 " just after it finishes.
83 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +020084 let lines =<< trim END
85 func s:f(...)
86 let x = a:000
87 endfunc
88 for _ in range(10000)
89 call s:f(0)
90 endfor
91 END
92 call writefile(lines, testfile)
Bram Moolenaar209b8e32019-03-14 13:43:24 +010093
94 let vim = s:vim_new()
95 call vim.start('--clean', '-c', 'set noswapfile', testfile)
96 let before = s:monitor_memory_usage(vim.pid).last
97
98 call term_sendkeys(vim.buf, ":so %\<CR>")
99 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
100 let after = s:monitor_memory_usage(vim.pid)
101
102 " Estimate the limit of max usage as 2x initial usage.
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200103 " The lower limit can fluctuate a bit, use 97%.
104 call assert_inrange(before * 97 / 100, 2 * before, after.max)
Bram Moolenaar3a731ee2019-03-27 21:41:36 +0100105
106 " In this case, garbage collecting is not needed.
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200107 " The value might fluctuate a bit, allow for 3% tolerance below and 5% above.
108 " Based on various test runs.
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100109 let lower = after.last * 97 / 100
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200110 let upper = after.last * 105 / 100
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100111 call assert_inrange(lower, upper, after.max)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100112
113 call vim.stop()
114 call delete(testfile)
115endfunc
116
117func Test_memory_func_capture_lvars()
118 " Case: if a local variable captures l: dict, funccall object will not be
119 " free until garbage collector runs, but after that memory usage doesn't
120 " increase so much even when rerun Xtest.vim since system memory caches.
121 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 let lines =<< trim END
123 func s:f()
124 let x = l:
125 endfunc
126 for _ in range(10000)
127 call s:f()
128 endfor
129 END
130 call writefile(lines, testfile)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100131
132 let vim = s:vim_new()
133 call vim.start('--clean', '-c', 'set noswapfile', testfile)
134 let before = s:monitor_memory_usage(vim.pid).last
135
136 call term_sendkeys(vim.buf, ":so %\<CR>")
137 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
138 let after = s:monitor_memory_usage(vim.pid)
139
140 " Rerun Xtest.vim.
141 for _ in range(3)
142 call term_sendkeys(vim.buf, ":so %\<CR>")
143 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
144 let last = s:monitor_memory_usage(vim.pid).last
145 endfor
146
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100147 " The usage may be a bit less than the last value, use 80%.
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100148 " Allow for 20% tolerance at the upper limit. That's very permissive, but
Bram Moolenaar1832d122020-01-01 15:17:25 +0100149 " otherwise the test fails sometimes. On Cirrus CI with FreeBSD we need to
150 " be even more permissive.
151 if has('bsd')
Bram Moolenaarbb062c12020-01-01 15:26:32 +0100152 let multiplier = 15
Bram Moolenaar1832d122020-01-01 15:17:25 +0100153 else
154 let multiplier = 12
155 endif
Bram Moolenaar08cda652019-03-20 22:45:01 +0100156 let lower = before * 8 / 10
Bram Moolenaar1832d122020-01-01 15:17:25 +0100157 let upper = (after.max + (after.last - before)) * multiplier / 10
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100158 call assert_inrange(lower, upper, last)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100159
160 call vim.stop()
161 call delete(testfile)
162endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200163
164" vim: shiftwidth=2 sts=2 expandtab