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. |
Bram Moolenaar | 3a731ee | 2019-03-27 21:41:36 +0100 | [diff] [blame] | 100 | " The lower limit can fluctuate a bit, use 98%. |
| 101 | call assert_inrange(before * 98 / 100, 2 * before, after.max) |
| 102 | |
| 103 | " In this case, garbage collecting is not needed. |
| 104 | " The value might fluctuate a bit, allow for 3% tolerance. |
Bram Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 105 | let lower = after.last * 97 / 100 |
| 106 | let upper = after.last * 103 / 100 |
| 107 | call assert_inrange(lower, upper, after.max) |
Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 108 | |
| 109 | call vim.stop() |
| 110 | call delete(testfile) |
| 111 | endfunc |
| 112 | |
| 113 | func Test_memory_func_capture_lvars() |
| 114 | " Case: if a local variable captures l: dict, funccall object will not be |
| 115 | " free until garbage collector runs, but after that memory usage doesn't |
| 116 | " increase so much even when rerun Xtest.vim since system memory caches. |
| 117 | let testfile = 'Xtest.vim' |
| 118 | call writefile([ |
| 119 | \ 'func s:f()', |
| 120 | \ ' let x = l:', |
| 121 | \ 'endfunc', |
| 122 | \ 'for _ in range(10000)', |
| 123 | \ ' call s:f()', |
| 124 | \ 'endfor', |
| 125 | \ ], testfile) |
| 126 | |
| 127 | let vim = s:vim_new() |
| 128 | call vim.start('--clean', '-c', 'set noswapfile', testfile) |
| 129 | let before = s:monitor_memory_usage(vim.pid).last |
| 130 | |
| 131 | call term_sendkeys(vim.buf, ":so %\<CR>") |
| 132 | call WaitFor({-> term_getcursor(vim.buf)[0] == 1}) |
| 133 | let after = s:monitor_memory_usage(vim.pid) |
| 134 | |
| 135 | " Rerun Xtest.vim. |
| 136 | for _ in range(3) |
| 137 | call term_sendkeys(vim.buf, ":so %\<CR>") |
| 138 | call WaitFor({-> term_getcursor(vim.buf)[0] == 1}) |
| 139 | let last = s:monitor_memory_usage(vim.pid).last |
| 140 | endfor |
| 141 | |
Bram Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 142 | " The usage may be a bit less than the last value, use 80%. |
Bram Moolenaar | 6b6f7aa | 2019-03-22 14:36:59 +0100 | [diff] [blame] | 143 | " Allow for 20% tolerance at the upper limit. That's very permissive, but |
| 144 | " otherwise the test fails sometimes. |
Bram Moolenaar | 08cda65 | 2019-03-20 22:45:01 +0100 | [diff] [blame] | 145 | let lower = before * 8 / 10 |
Bram Moolenaar | 6b6f7aa | 2019-03-22 14:36:59 +0100 | [diff] [blame] | 146 | let upper = (after.max + (after.last - before)) * 12 / 10 |
Bram Moolenaar | f7e47af | 2019-03-21 21:16:36 +0100 | [diff] [blame] | 147 | call assert_inrange(lower, upper, last) |
Bram Moolenaar | 209b8e3 | 2019-03-14 13:43:24 +0100 | [diff] [blame] | 148 | |
| 149 | call vim.stop() |
| 150 | call delete(testfile) |
| 151 | endfunc |