robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each , pending = 5t . describe , t . it , t . before_each , t . after_each , t . pending 6local clear =n . clear 7local eval =n . eval 8local eq =t . eq 9local feed_command =n . feed_command 10local retry =t . retry 11local ok =t . ok 12local source =n . source 13local poke_eventloop =n . poke_eventloop 14local load_adjust =n . load_adjust 15local write_file =t . write_file 16local is_os =t . is_os 17local is_ci =t . is_ci 18 19clear () 20if t . is_asan () then 21pending ( 'ASAN build is difficult to estimate memory usage' , function () end ) 22return 23elseif is_os ( 'win' ) then 24if is_ci ( 'github' ) then 25pending ( 26'Windows runners in Github Actions do not have a stable environment to estimate memory usage' , 27function () end 28) 29return 30elseif eval ( "executable('wmic')" ) == 0 then 31pending ( 'missing "wmic" command' , function () end ) 32return 33end 34elseif eval ( "executable('ps')" ) == 0 then 35pending ( 'missing "ps" command' , function () end ) 36return 37end 38 39local monitor_memory_usage ={ 40memory_usage =function ( self ) 41local handle 42if is_os ( 'win' ) then 43handle =io . popen ( 'wmic process where processid=' .. self . pid .. ' get WorkingSetSize' ) 44else 45handle =io . popen ( 'ps -o rss= -p ' .. self . pid ) 46end 47return tonumber ( handle : read ( '*a' ): match ( '%d+' )) 48end , 49op =function ( self ) 50retry ( nil , 10000 , function () 51local val =self . memory_usage ( self ) 52if self . max < val then 53self . max =val 54end 55table . insert ( self . hist , val ) 56ok ( # self . hist > 20 ) 57local result ={} 58for key , value in ipairs ( self . hist ) do 59if value ~= self . hist [ key + 1 ] then 60table . insert ( result , value ) 61end 62end 63table . remove ( self . hist , 1 ) 64self . last =self . hist [ # self . hist ] 65eq ( 1 , # result ) 66end ) 67end , 68dump =function ( self ) 69return 'max: ' .. self . max .. ', last: ' .. self . last 70end , 71monitor_memory_usage =function ( self , pid ) 72local obj ={ 73pid =pid , 74max =0 , 75last =0 , 76hist ={} , 77} 78setmetatable ( obj , { __index =self } ) 79obj : op () 80return obj 81end , 82} 83setmetatable ( monitor_memory_usage , { 84__call =function ( self , pid ) 85return monitor_memory_usage . monitor_memory_usage ( self , pid ) 86end , 87} ) 88 89describe ( 'memory usage' , function () 90local tmpfile ='X_memory_usage' 91 92after_each ( function () 93os . remove ( tmpfile ) 94end ) 95 96local function check_result ( tbl , status , result ) 97if not status then 98( '' ) 99for key , val in pairs ( tbl ) do 100( key , val : dump ()) 101end 102error ( result ) 103end 104end 105 106before_each ( clear ) 107 108--[[ 109Case: if a local variable captures a:000, funccall object will be free 110just after it finishes. 111]] 112-- 113it ( 'function capture vargs' , function () 114local pid =eval ( 'getpid()' ) 115local before =monitor_memory_usage ( pid ) 116write_file ( 117tmpfile , 118[[ 119func s:f(...) 120let x = a:000 121endfunc 122for _ in range(10000) 123call s:f(0) 124endfor 125]] 126) 127-- TODO: check_result fails if command() is used here. Why? #16064 128feed_command ( 'source ' .. tmpfile ) 129poke_eventloop () 130local after =monitor_memory_usage ( pid ) 131-- Estimate the limit of max usage as 2x initial usage. 132-- The lower limit can fluctuate a bit, use 97%. 133check_result ( { before =before , after =after } , pcall ( ok , before . last * 97 / 100 < after . max )) 134check_result ( { before =before , after =after } , pcall ( ok , before . last * 2 > after . max )) 135-- In this case, garbage collecting is not needed. 136-- The value might fluctuate a bit, allow for 3% tolerance below and 5% above. 137-- Based on various test runs. 138local lower =after . last * 97 / 100 139local upper =after . last * 105 / 100 140check_result ( { before =before , after =after } , pcall ( ok , lower < after . max )) 141check_result ( { before =before , after =after } , pcall ( ok , after . max < upper )) 142end ) 143 144--[[ 145Case: if a local variable captures l: dict, funccall object will not be 146free until garbage collector runs, but after that memory usage doesn't 147increase so much even when rerun Xtest.vim since system memory caches. 148]] 149-- 150it ( 'function capture lvars' , function () 151local pid =eval ( 'getpid()' ) 152local before =monitor_memory_usage ( pid ) 153write_file ( 154tmpfile , 155[[ 156if !exists('s:defined_func') 157func s:f() 158let x = l: 159endfunc 160endif 161let s:defined_func = 1 162for _ in range(10000) 163call s:f() 164endfor 165]] 166) 167feed_command ( 'source ' .. tmpfile ) 168poke_eventloop () 169local after =monitor_memory_usage ( pid ) 170for _ =1 , 3 do 171-- TODO: check_result fails if command() is used here. Why? #16064 172feed_command ( 'source ' .. tmpfile ) 173poke_eventloop () 174end 175local last =monitor_memory_usage ( pid ) 176-- The usage may be a bit less than the last value, use 80%. 177-- Allow for 20% tolerance at the upper limit. That's very permissive, but 178-- otherwise the test fails sometimes. On FreeBSD we need to be even much 179-- more permissive. 180local upper_multiplier =is_os ( 'freebsd' ) and 19 or 12 181local lower =before . last * 8 / 10 182local upper =load_adjust (( after . max + ( after . last - before . last )) * upper_multiplier / 10 ) 183check_result ( { before =before , after =after , last =last } , pcall ( ok , lower < last . last )) 184check_result ( { before =before , after =after , last =last } , pcall ( ok , last . last < upper )) 185end ) 186 187it ( 'releases memory when closing windows when folds exist' , function () 188if is_os ( 'mac' ) then 189pending ( 'macOS memory compression causes flakiness' ) 190end 191local pid =eval ( 'getpid()' ) 192source ( [[ 193new 194" Insert lines 195call nvim_buf_set_lines(0, 0, 0, v:false, repeat([''], 999)) 196" Create folds 197normal! gg 198for _ in range(500) 199normal! zfjj 200endfor 201]] ) 202poke_eventloop () 203local before =monitor_memory_usage ( pid ) 204source ( [[ 205" Split and close window multiple times 206for _ in range(1000) 207split 208close 209endfor 210]] ) 211poke_eventloop () 212local after =monitor_memory_usage ( pid ) 213source ( 'bwipe!' ) 214poke_eventloop () 215-- Allow for an increase of 10% in memory usage, which accommodates minor fluctuation, 216-- but is small enough that if memory were not released (prior to PR #14884), the test 217-- would fail. 218local upper =before . last * 1.10 219check_result ( { before =before , after =after } , pcall ( ok , after . last <= upper )) 220end ) 221end )