profile_spec.lua 7.6 KB

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