blob: 565a1155426ceea337ea17e3aba85c40169e041b [file] [log] [blame]
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001" Tests for memory usage.
2
3if !has('terminal') || has('gui_running') || $ASAN_OPTIONS !=# ''
4 " Skip tests on Travis CI ASAN build because it's difficult to estimate
5 " memory usage.
6 finish
7endif
8
9source shared.vim
10
11func s:pick_nr(str) abort
12 return substitute(a:str, '[^0-9]', '', 'g') * 1
13endfunc
14
15if has('win32')
16 if !executable('wmic')
17 finish
18 endif
19 func s:memory_usage(pid) abort
20 let cmd = printf('wmic process where processid=%d get WorkingSetSize', a:pid)
21 return s:pick_nr(system(cmd)) / 1024
22 endfunc
23elseif has('unix')
24 if !executable('ps')
25 finish
26 endif
27 func s:memory_usage(pid) abort
28 return s:pick_nr(system('ps -o rss= -p ' . a:pid))
29 endfunc
30else
31 finish
32endif
33
34" Wait for memory usage to level off.
35func s:monitor_memory_usage(pid) abort
36 let proc = {}
37 let proc.pid = a:pid
38 let proc.hist = []
Bram Moolenaar209b8e32019-03-14 13:43:24 +010039 let proc.max = 0
40
41 func proc.op() abort
42 " Check the last 200ms.
43 let val = s:memory_usage(self.pid)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +010044 if self.max < val
Bram Moolenaar209b8e32019-03-14 13:43:24 +010045 let self.max = val
46 endif
47 call add(self.hist, val)
48 if len(self.hist) < 20
49 return 0
50 endif
51 let sample = remove(self.hist, 0)
52 return len(uniq([sample] + self.hist)) == 1
53 endfunc
54
55 call WaitFor({-> proc.op()}, 10000)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +010056 return {'last': get(proc.hist, -1), 'max': proc.max}
Bram Moolenaar209b8e32019-03-14 13:43:24 +010057endfunc
58
59let s:term_vim = {}
60
61func s:term_vim.start(...) abort
62 let self.buf = term_start([GetVimProg()] + a:000)
63 let self.job = term_getjob(self.buf)
64 call WaitFor({-> job_status(self.job) ==# 'run'})
65 let self.pid = job_info(self.job).process
66endfunc
67
68func s:term_vim.stop() abort
69 call term_sendkeys(self.buf, ":qall!\<CR>")
70 call WaitFor({-> job_status(self.job) ==# 'dead'})
71 exe self.buf . 'bwipe!'
72endfunc
73
74func s:vim_new() abort
75 return copy(s:term_vim)
76endfunc
77
78func Test_memory_func_capture_vargs()
79 " Case: if a local variable captures a:000, funccall object will be free
80 " just after it finishes.
81 let testfile = 'Xtest.vim'
82 call writefile([
83 \ 'func s:f(...)',
84 \ ' let x = a:000',
85 \ 'endfunc',
86 \ 'for _ in range(10000)',
87 \ ' call s:f(0)',
88 \ 'endfor',
89 \ ], testfile)
90
91 let vim = s:vim_new()
92 call vim.start('--clean', '-c', 'set noswapfile', testfile)
93 let before = s:monitor_memory_usage(vim.pid).last
94
95 call term_sendkeys(vim.buf, ":so %\<CR>")
96 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
97 let after = s:monitor_memory_usage(vim.pid)
98
99 " Estimate the limit of max usage as 2x initial usage.
100 call assert_inrange(before, 2 * before, after.max)
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100101 " In this case, garbage collecting is not needed. The value might fluctuate
102 " a bit, allow for 3% tolerance.
103 let lower = after.last * 97 / 100
104 let upper = after.last * 103 / 100
105 call assert_inrange(lower, upper, after.max)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100106
107 call vim.stop()
108 call delete(testfile)
109endfunc
110
111func Test_memory_func_capture_lvars()
112 " Case: if a local variable captures l: dict, funccall object will not be
113 " free until garbage collector runs, but after that memory usage doesn't
114 " increase so much even when rerun Xtest.vim since system memory caches.
115 let testfile = 'Xtest.vim'
116 call writefile([
117 \ 'func s:f()',
118 \ ' let x = l:',
119 \ 'endfunc',
120 \ 'for _ in range(10000)',
121 \ ' call s:f()',
122 \ 'endfor',
123 \ ], testfile)
124
125 let vim = s:vim_new()
126 call vim.start('--clean', '-c', 'set noswapfile', testfile)
127 let before = s:monitor_memory_usage(vim.pid).last
128
129 call term_sendkeys(vim.buf, ":so %\<CR>")
130 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
131 let after = s:monitor_memory_usage(vim.pid)
132
133 " Rerun Xtest.vim.
134 for _ in range(3)
135 call term_sendkeys(vim.buf, ":so %\<CR>")
136 call WaitFor({-> term_getcursor(vim.buf)[0] == 1})
137 let last = s:monitor_memory_usage(vim.pid).last
138 endfor
139
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100140 " The usage may be a bit less than the last value, use 80%.
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100141 " Allow for 20% tolerance at the upper limit. That's very permissive, but
142 " otherwise the test fails sometimes.
Bram Moolenaar08cda652019-03-20 22:45:01 +0100143 let lower = before * 8 / 10
Bram Moolenaar6b6f7aa2019-03-22 14:36:59 +0100144 let upper = (after.max + (after.last - before)) * 12 / 10
Bram Moolenaarf7e47af2019-03-21 21:16:36 +0100145 call assert_inrange(lower, upper, last)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100146
147 call vim.stop()
148 call delete(testfile)
149endfunc