decor_spec.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. local n = require('test.functional.testnvim')()
  2. local Screen = require('test.functional.ui.screen')
  3. local exec_lua = n.exec_lua
  4. describe('decor perf', function()
  5. before_each(n.clear)
  6. it('can handle long lines', function()
  7. Screen.new(100, 101)
  8. local result = exec_lua [==[
  9. local ephemeral_pattern = {
  10. { 0, 4, 'Comment', 11 },
  11. { 0, 3, 'Keyword', 12 },
  12. { 1, 2, 'Label', 12 },
  13. { 0, 1, 'String', 21 },
  14. { 1, 3, 'Function', 21 },
  15. { 2, 10, 'Label', 8 },
  16. }
  17. local regular_pattern = {
  18. { 4, 5, 'String', 12 },
  19. { 1, 4, 'Function', 2 },
  20. }
  21. for _, list in ipairs({ ephemeral_pattern, regular_pattern }) do
  22. for _, p in ipairs(list) do
  23. p[3] = vim.api.nvim_get_hl_id_by_name(p[3])
  24. end
  25. end
  26. local text = ('abcdefghijklmnopqrstuvwxyz0123'):rep(333)
  27. local line_len = #text
  28. vim.api.nvim_buf_set_lines(0, 0, 0, false, { text })
  29. local ns = vim.api.nvim_create_namespace('decor_spec.lua')
  30. vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
  31. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  32. local ps, pe
  33. local function add_pattern(pattern, ephemeral)
  34. ps = vim.uv.hrtime()
  35. local i = 0
  36. while i < line_len - 10 do
  37. for _, p in ipairs(pattern) do
  38. vim.api.nvim_buf_set_extmark(0, ns, 0, i + p[1], {
  39. end_row = 0,
  40. end_col = i + p[2],
  41. hl_group = p[3],
  42. priority = p[4],
  43. ephemeral = ephemeral,
  44. })
  45. end
  46. i = i + 5
  47. end
  48. pe = vim.uv.hrtime()
  49. end
  50. vim.api.nvim_set_decoration_provider(ns, {
  51. on_win = function()
  52. return true
  53. end,
  54. on_line = function()
  55. add_pattern(ephemeral_pattern, true)
  56. end,
  57. })
  58. add_pattern(regular_pattern, false)
  59. local total = {}
  60. local provider = {}
  61. for i = 1, 100 do
  62. local tic = vim.uv.hrtime()
  63. vim.cmd'redraw!'
  64. local toc = vim.uv.hrtime()
  65. table.insert(total, toc - tic)
  66. table.insert(provider, pe - ps)
  67. end
  68. return { total, provider }
  69. ]==]
  70. local total, provider = unpack(result)
  71. table.sort(total)
  72. table.sort(provider)
  73. local ms = 1 / 1000000
  74. local function fmt(stats)
  75. return string.format(
  76. 'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms',
  77. stats[1] * ms,
  78. stats[1 + math.floor(#stats * 0.25)] * ms,
  79. stats[1 + math.floor(#stats * 0.5)] * ms,
  80. stats[1 + math.floor(#stats * 0.75)] * ms,
  81. stats[#stats] * ms
  82. )
  83. end
  84. print('\nTotal ' .. fmt(total) .. '\nDecoration provider: ' .. fmt(provider))
  85. end)
  86. it('can handle full screen of highlighting', function()
  87. Screen.new(100, 51)
  88. local result = exec_lua(function()
  89. local long_line = 'local a={' .. ('a=5,'):rep(22) .. '}'
  90. local lines = {}
  91. for _ = 1, 50 do
  92. table.insert(lines, long_line)
  93. end
  94. vim.api.nvim_buf_set_lines(0, 0, 0, false, lines)
  95. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  96. vim.treesitter.start(0, 'lua')
  97. local total = {}
  98. for _ = 1, 100 do
  99. local tic = vim.uv.hrtime()
  100. vim.cmd 'redraw!'
  101. local toc = vim.uv.hrtime()
  102. table.insert(total, toc - tic)
  103. end
  104. return { total }
  105. end)
  106. local total = unpack(result)
  107. table.sort(total)
  108. local ms = 1 / 1000000
  109. local res = string.format(
  110. 'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms',
  111. total[1] * ms,
  112. total[1 + math.floor(#total * 0.25)] * ms,
  113. total[1 + math.floor(#total * 0.5)] * ms,
  114. total[1 + math.floor(#total * 0.75)] * ms,
  115. total[#total] * ms
  116. )
  117. print('\nTotal ' .. res)
  118. end)
  119. end)