Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 1 | " Tests for memory usage. |
| 2 | |
| 3 | if !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 |
| 7 | endif |
| 8 | |
| 9 | source shared.vim |
| 10 | |
| 11 | func s:pick_nr(str) abort |
| 12 | return substitute(a:str, '[^0-9]', '', 'g') * 1 |
| 13 | endfunc |
| 14 | |
| 15 | if 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 |
| 23 | elseif 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 |
| 30 | else |
| 31 | finish |
| 32 | endif |
| 33 | |
| 34 | " Wait for memory usage to level off. |
| 35 | func s:monitor_memory_usage(pid) abort |
| 36 | let proc = {} |
| 37 | let proc.pid = a:pid |
| 38 | let proc.hist = [] |
Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 39 | let proc.max = 0 |
| 40 | |
| 41 | func proc.op() abort |
| 42 | " Check the last 200ms. |
| 43 | let val = s:memory_usage(self.pid) |
Bram Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 44 | if self.max < val |
Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 45 | 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 Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 56 | return {'last': get(proc.hist, -1), 'max': proc.max} |
Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 57 | endfunc |
| 58 | |
| 59 | let s:term_vim = {} |
| 60 | |
| 61 | func 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 |
| 66 | endfunc |
| 67 | |
| 68 | func 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!' |
| 72 | endfunc |
| 73 | |
| 74 | func s:vim_new() abort |
| 75 | return copy(s:term_vim) |
| 76 | endfunc |
| 77 | |
| 78 | func 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 Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 101 | " 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 Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 106 | |
| 107 | call vim.stop() |
| 108 | call delete(testfile) |
| 109 | endfunc |
| 110 | |
| 111 | func 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 Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 140 | " The usage may be a bit less than the last value, use 80%. |
| 141 | " Allow for 1% tolerance at the upper limit. |
Bram Moolenaar | 08cda65 | 2019-03-20 22:45:01 +0100 | [diff] [blame] | 142 | let lower = before * 8 / 10 |
Bram Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 143 | let upper = (after.max + (after.last - before)) * 101 / 100 |
| 144 | call assert_inrange(lower, upper, last) |
Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 145 | |
| 146 | call vim.stop() |
| 147 | call delete(testfile) |
| 148 | endfunc |