robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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