robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
7.0 KiB262 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local cimport = t.cimport
6local ffi = t.ffi
7local eq = t.eq
8local neq = t.neq
9
10local prof = cimport('./src/nvim/profile.h')
11
12local function split(inputstr, sep)
13  if sep == nil then
14    sep = '%s'
15  end
16
17  local q, i = {}, 1
18  for str in string.gmatch(inputstr, '([^' .. sep .. ']+)') do
19    q[i] = str
20    i = i + 1
21  end
22
23  return q
24end
25
26local function trim(s)
27  local from = s:match '^%s*()'
28  return from > #s and '' or s:match('.*%S', from)
29end
30
31local function starts(str, start)
32  return string.sub(str, 1, string.len(start)) == start
33end
34
35local function cmp_assert(v1, v2, op, opstr)
36  local res = op(v1, v2)
37  if res == false then
38    print(string.format('expected: %f %s %f', v1, opstr, v2))
39  end
40  t.ok(res)
41end
42
43local function lt(a, b) -- luacheck: ignore
44  cmp_assert(a, b, function(x, y)
45    return x < y
46  end, '<')
47end
48
49local function lte(a, b) -- luacheck: ignore
50  cmp_assert(a, b, function(x, y)
51    return x <= y
52  end, '<=')
53end
54
55local function gt(a, b) -- luacheck: ignore
56  cmp_assert(a, b, function(x, y)
57    return x > y
58  end, '>')
59end
60
61local function gte(a, b)
62  cmp_assert(a, b, function(x, y)
63    return x >= y
64  end, '>=')
65end
66
67-- missing functions:
68--  profile_self
69--  profile_get_wait
70--  profile_set_wait
71--  profile_sub_wait
72describe('profiling related functions', function()
73  local function profile_start()
74    return prof.profile_start()
75  end
76  local function profile_end(q)
77    return prof.profile_end(q)
78  end
79  local function profile_zero()
80    return prof.profile_zero()
81  end
82  local function profile_setlimit(ms)
83    return prof.profile_setlimit(ms)
84  end
85  local function profile_passed_limit(q)
86    return prof.profile_passed_limit(q)
87  end
88  local function profile_add(t1, t2)
89    return prof.profile_add(t1, t2)
90  end
91  local function profile_sub(t1, t2)
92    return prof.profile_sub(t1, t2)
93  end
94  local function profile_divide(q, cnt)
95    return prof.profile_divide(q, cnt)
96  end
97  local function profile_cmp(t1, t2)
98    return prof.profile_cmp(t1, t2)
99  end
100  local function profile_msg(q)
101    return ffi.string(prof.profile_msg(q))
102  end
103
104  local function toseconds(q) -- luacheck: ignore
105    local str = trim(profile_msg(q))
106    local spl = split(str, '.')
107    local s, us = spl[1], spl[2]
108    return tonumber(s) + tonumber(us) / 1000000
109  end
110
111  -- this is quite difficult to test, as it would rely on other functions in
112  -- the profiling package. Those functions in turn will probably be tested
113  -- using profile_cmp... circular reasoning.
114  describe('profile_cmp', function()
115    itp('can compare subsequent starts', function()
116      local s1, s2 = profile_start(), profile_start()
117      t.ok(profile_cmp(s1, s2) > 0)
118      t.ok(profile_cmp(s2, s1) < 0)
119    end)
120
121    itp('can compare the zero element', function()
122      t.ok(profile_cmp(profile_zero(), profile_zero()) == 0)
123    end)
124
125    itp('correctly orders divisions', function()
126      local start = profile_start()
127      t.ok(profile_cmp(start, profile_divide(start, 10)) <= 0)
128    end)
129  end)
130
131  describe('profile_divide', function()
132    itp('actually performs division', function()
133      -- note: the routine actually performs floating-point division to get
134      -- better rounding behaviour, we have to take that into account when
135      -- checking. (check range, not exact number).
136      local divisor = 10
137
138      local start = profile_start()
139      local divided = profile_divide(start, divisor)
140
141      local res = divided
142      for _ = 1, divisor - 1 do
143        res = profile_add(res, divided)
144      end
145
146      -- res should be in the range [start - divisor, start + divisor]
147      local start_min, start_max = profile_sub(start, divisor), profile_add(start, divisor)
148      t.ok(profile_cmp(start_min, res) >= 0)
149      t.ok(profile_cmp(start_max, res) <= 0)
150    end)
151  end)
152
153  describe('profile_zero', function()
154    itp('returns the same value on each call', function()
155      eq(0, profile_zero())
156    end)
157  end)
158
159  describe('profile_start', function()
160    itp('increases', function()
161      local last = profile_start()
162      for _ = 1, 100 do
163        local curr = profile_start()
164        gte(curr, last)
165        last = curr
166      end
167    end)
168  end)
169
170  describe('profile_end', function()
171    itp('the elapsed time cannot be zero', function()
172      neq(profile_zero(), profile_end(profile_start()))
173    end)
174
175    itp('outer elapsed >= inner elapsed', function()
176      for _ = 1, 100 do
177        local start_outer = profile_start()
178        local start_inner = profile_start()
179        local elapsed_inner = profile_end(start_inner)
180        local elapsed_outer = profile_end(start_outer)
181
182        gte(elapsed_outer, elapsed_inner)
183      end
184    end)
185  end)
186
187  describe('profile_setlimit', function()
188    itp('sets no limit when 0 is passed', function()
189      eq(profile_setlimit(0), profile_zero())
190    end)
191
192    itp('sets a limit in the future otherwise', function()
193      local future = profile_setlimit(1000)
194      local now = profile_start()
195      t.ok(profile_cmp(future, now) < 0)
196    end)
197  end)
198
199  describe('profile_passed_limit', function()
200    itp('start is in the past', function()
201      local start = profile_start()
202      eq(true, profile_passed_limit(start))
203    end)
204
205    itp('start + start is in the future', function()
206      local start = profile_start()
207      local future = profile_add(start, start)
208      eq(false, profile_passed_limit(future))
209    end)
210  end)
211
212  describe('profile_msg', function()
213    itp('prints the zero time as 0.00000', function()
214      local str = trim(profile_msg(profile_zero()))
215      eq('0.000000', str)
216    end)
217
218    itp('prints the time passed, in seconds.microsends', function()
219      local start = profile_start()
220      local endt = profile_end(start)
221      local str = trim(profile_msg(endt))
222      local spl = split(str, '.')
223
224      -- string has two parts (before dot and after dot)
225      eq(2, #spl)
226
227      local s, us = spl[1], spl[2]
228
229      -- zero seconds have passed (if this is not true, either LuaJIT is too
230      -- slow or the profiling functions are too slow and need to be fixed)
231      eq('0', s)
232
233      -- more or less the same goes for the microsecond part, if it doesn't
234      -- start with 0, it's too slow.
235      t.ok(starts(us, '0'))
236    end)
237  end)
238
239  describe('profile_add', function()
240    itp('adds profiling times', function()
241      local start = profile_start()
242      t.eq(start, profile_add(profile_zero(), start))
243    end)
244  end)
245
246  describe('profile_sub', function()
247    itp('subtracts profiling times', function()
248      -- subtracting zero does nothing
249      local start = profile_start()
250      t.eq(start, profile_sub(start, profile_zero()))
251
252      local start1, start2, start3 = profile_start(), profile_start(), profile_start()
253      local cmp = profile_cmp(profile_sub(start2, start1), profile_sub(start3, start1))
254      -- t2 >= t1 => profile_cmp(t1, t2) >= 0
255      t.ok(cmp >= 0)
256
257      cmp = profile_cmp(profile_sub(start3, start1), profile_sub(start2, start1))
258      -- t2 <= t1 => profile_cmp(t1, t2) <= 0
259      t.ok(cmp <= 0)
260    end)
261  end)
262end)