blob: debc9c18595bc0c2d780fddb787bd84f1baa9215 [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
5
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02006if has('gui_running')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02007 throw 'Skipped: does not work in GUI'
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02008endif
Bram Moolenaarbffc5042019-06-15 16:34:21 +02009if execute('version') =~# '-fsanitize=[a-z,]*\<address\>'
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 Moolenaarb46fecd2019-06-15 17:58:09 +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 Moolenaarb46fecd2019-06-15 17:58:09 +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 Moolenaarb46fecd2019-06-15 17:58:09 +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 Moolenaarb46fecd2019-06-15 17:58:09 +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'
Bram Moolenaare7eb9272019-06-24 00:58:07 +020088 let lines =<< trim END
89 func s:f(...)
90 let x = a:000
91 endfunc
92 for _ in range(10000)
93 call s:f(0)
94 endfor
95 END
96 call writefile(lines, testfile)
Bram Moolenaar209b8e32019-03-14 13:43:24 +010097
98 let vim = s:vim_new()
99 call vim.start('--clean', '-c', 'set noswapfile', testfile)
100 let before = s:monitor_memory_usage(vim.pid).last
101
102 call term_sendkeys(vim.buf, ":so %\<CR>")
103 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
104 let after = s:monitor_memory_usage(vim.pid)
105
106 " Estimate the limit of max usage as 2x initial usage.
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200107 " The lower limit can fluctuate a bit, use 97%.
108 call assert_inrange(before * 97 / 100, 2 * before, after.max)
Bram Moolenaar3a731ee2019-03-27 21:41:36 +0100109
110 " In this case, garbage collecting is not needed.
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200111 " The value might fluctuate a bit, allow for 3% tolerance below and 5% above.
112 " Based on various test runs.
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100113 let lower = after.last * 97 / 100
Bram Moolenaar5d508dd2019-05-31 20:23:25 +0200114 let upper = after.last * 105 / 100
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100115 call assert_inrange(lower, upper, after.max)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100116
117 call vim.stop()
118 call delete(testfile)
119endfunc
120
121func Test_memory_func_capture_lvars()
122 " Case: if a local variable captures l: dict, funccall object will not be
123 " free until garbage collector runs, but after that memory usage doesn't
124 " increase so much even when rerun Xtest.vim since system memory caches.
125 let testfile = 'Xtest.vim'
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200126 let lines =<< trim END
127 func s:f()
128 let x = l:
129 endfunc
130 for _ in range(10000)
131 call s:f()
132 endfor
133 END
134 call writefile(lines, testfile)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100135
136 let vim = s:vim_new()
137 call vim.start('--clean', '-c', 'set noswapfile', testfile)
138 let before = s:monitor_memory_usage(vim.pid).last
139
140 call term_sendkeys(vim.buf, ":so %\<CR>")
141 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
142 let after = s:monitor_memory_usage(vim.pid)
143
144 " Rerun Xtest.vim.
145 for _ in range(3)
146 call term_sendkeys(vim.buf, ":so %\<CR>")
147 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
148 let last = s:monitor_memory_usage(vim.pid).last
149 endfor
150
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100151 " The usage may be a bit less than the last value, use 80%.
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100152 " Allow for 20% tolerance at the upper limit. That's very permissive, but
153 " otherwise the test fails sometimes.
Bram Moolenaar08cda652019-03-20 22:45:01 +0100154 let lower = before * 8 / 10
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100155 let upper = (after.max + (after.last - before)) * 12 / 10
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100156 call assert_inrange(lower, upper, last)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100157
158 call vim.stop()
159 call delete(testfile)
160endfunc