autocmd_spec.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. local n = require('test.functional.testnvim')()
  2. local clear = n.clear
  3. local exec_lua = n.exec_lua
  4. local N = 7500
  5. describe('autocmd perf', function()
  6. before_each(function()
  7. clear()
  8. exec_lua([[
  9. out = {}
  10. function start()
  11. ts = vim.uv.hrtime()
  12. end
  13. function stop(name)
  14. out[#out+1] = ('%14.6f ms - %s'):format((vim.uv.hrtime() - ts) / 1000000, name)
  15. end
  16. ]])
  17. end)
  18. after_each(function()
  19. for _, line in ipairs(exec_lua([[return out]])) do
  20. print(line)
  21. end
  22. end)
  23. it('nvim_create_autocmd, nvim_del_autocmd (same pattern)', function()
  24. exec_lua(
  25. [[
  26. local N = ...
  27. local ids = {}
  28. start()
  29. for i = 1, N do
  30. ids[i] = vim.api.nvim_create_autocmd('User', {
  31. pattern = 'Benchmark',
  32. command = 'eval 0', -- noop
  33. })
  34. end
  35. stop('nvim_create_autocmd')
  36. start()
  37. for i = 1, N do
  38. vim.api.nvim_del_autocmd(ids[i])
  39. end
  40. stop('nvim_del_autocmd')
  41. ]],
  42. N
  43. )
  44. end)
  45. it('nvim_create_autocmd, nvim_del_autocmd (unique patterns)', function()
  46. exec_lua(
  47. [[
  48. local N = ...
  49. local ids = {}
  50. start()
  51. for i = 1, N do
  52. ids[i] = vim.api.nvim_create_autocmd('User', {
  53. pattern = 'Benchmark' .. i,
  54. command = 'eval 0', -- noop
  55. })
  56. end
  57. stop('nvim_create_autocmd')
  58. start()
  59. for i = 1, N do
  60. vim.api.nvim_del_autocmd(ids[i])
  61. end
  62. stop('nvim_del_autocmd')
  63. ]],
  64. N
  65. )
  66. end)
  67. it('nvim_create_autocmd + nvim_del_autocmd', function()
  68. exec_lua(
  69. [[
  70. local N = ...
  71. start()
  72. for _ = 1, N do
  73. local id = vim.api.nvim_create_autocmd('User', {
  74. pattern = 'Benchmark',
  75. command = 'eval 0', -- noop
  76. })
  77. vim.api.nvim_del_autocmd(id)
  78. end
  79. stop('nvim_create_autocmd + nvim_del_autocmd')
  80. ]],
  81. N
  82. )
  83. end)
  84. it('nvim_exec_autocmds (same pattern)', function()
  85. exec_lua(
  86. [[
  87. local N = ...
  88. for i = 1, N do
  89. vim.api.nvim_create_autocmd('User', {
  90. pattern = 'Benchmark',
  91. command = 'eval 0', -- noop
  92. })
  93. end
  94. start()
  95. vim.api.nvim_exec_autocmds('User', { pattern = 'Benchmark', modeline = false })
  96. stop('nvim_exec_autocmds')
  97. ]],
  98. N
  99. )
  100. end)
  101. it('nvim_del_augroup_by_id', function()
  102. exec_lua(
  103. [[
  104. local N = ...
  105. local group = vim.api.nvim_create_augroup('Benchmark', {})
  106. for i = 1, N do
  107. vim.api.nvim_create_autocmd('User', {
  108. pattern = 'Benchmark',
  109. command = 'eval 0', -- noop
  110. group = group,
  111. })
  112. end
  113. start()
  114. vim.api.nvim_del_augroup_by_id(group)
  115. stop('nvim_del_augroup_by_id')
  116. ]],
  117. N
  118. )
  119. end)
  120. it('nvim_del_augroup_by_name', function()
  121. exec_lua(
  122. [[
  123. local N = ...
  124. local group = vim.api.nvim_create_augroup('Benchmark', {})
  125. for i = 1, N do
  126. vim.api.nvim_create_autocmd('User', {
  127. pattern = 'Benchmark',
  128. command = 'eval 0', -- noop
  129. group = group,
  130. })
  131. end
  132. start()
  133. vim.api.nvim_del_augroup_by_name('Benchmark')
  134. stop('nvim_del_augroup_by_id')
  135. ]],
  136. N
  137. )
  138. end)
  139. it(':autocmd, :autocmd! (same pattern)', function()
  140. exec_lua(
  141. [[
  142. local N = ...
  143. start()
  144. for i = 1, N do
  145. vim.cmd('autocmd User Benchmark eval 0')
  146. end
  147. stop(':autocmd')
  148. start()
  149. vim.cmd('autocmd! User Benchmark')
  150. stop(':autocmd!')
  151. ]],
  152. N
  153. )
  154. end)
  155. it(':autocmd, :autocmd! (unique patterns)', function()
  156. exec_lua(
  157. [[
  158. local N = ...
  159. start()
  160. for i = 1, N do
  161. vim.cmd(('autocmd User Benchmark%d eval 0'):format(i))
  162. end
  163. stop(':autocmd')
  164. start()
  165. vim.cmd('autocmd! User')
  166. stop(':autocmd!')
  167. ]],
  168. N
  169. )
  170. end)
  171. end)