profile_spec.lua 7.6 KB

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