decor_spec.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. local screen = Screen.new(100, 101)
  8. screen:attach()
  9. local result = exec_lua [==[
  10. local ephemeral_pattern = {
  11. { 0, 4, 'Comment', 11 },
  12. { 0, 3, 'Keyword', 12 },
  13. { 1, 2, 'Label', 12 },
  14. { 0, 1, 'String', 21 },
  15. { 1, 3, 'Function', 21 },
  16. { 2, 10, 'Label', 8 },
  17. }
  18. local regular_pattern = {
  19. { 4, 5, 'String', 12 },
  20. { 1, 4, 'Function', 2 },
  21. }
  22. for _, list in ipairs({ ephemeral_pattern, regular_pattern }) do
  23. for _, p in ipairs(list) do
  24. p[3] = vim.api.nvim_get_hl_id_by_name(p[3])
  25. end
  26. end
  27. local text = ('abcdefghijklmnopqrstuvwxyz0123'):rep(333)
  28. local line_len = #text
  29. vim.api.nvim_buf_set_lines(0, 0, 0, false, { text })
  30. local ns = vim.api.nvim_create_namespace('decor_spec.lua')
  31. vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
  32. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  33. local ps, pe
  34. local function add_pattern(pattern, ephemeral)
  35. ps = vim.uv.hrtime()
  36. local i = 0
  37. while i < line_len - 10 do
  38. for _, p in ipairs(pattern) do
  39. vim.api.nvim_buf_set_extmark(0, ns, 0, i + p[1], {
  40. end_row = 0,
  41. end_col = i + p[2],
  42. hl_group = p[3],
  43. priority = p[4],
  44. ephemeral = ephemeral,
  45. })
  46. end
  47. i = i + 5
  48. end
  49. pe = vim.uv.hrtime()
  50. end
  51. vim.api.nvim_set_decoration_provider(ns, {
  52. on_win = function()
  53. return true
  54. end,
  55. on_line = function()
  56. add_pattern(ephemeral_pattern, true)
  57. end,
  58. })
  59. add_pattern(regular_pattern, false)
  60. local total = {}
  61. local provider = {}
  62. for i = 1, 100 do
  63. local tic = vim.uv.hrtime()
  64. vim.cmd'redraw!'
  65. local toc = vim.uv.hrtime()
  66. table.insert(total, toc - tic)
  67. table.insert(provider, pe - ps)
  68. end
  69. return { total, provider }
  70. ]==]
  71. local total, provider = unpack(result)
  72. table.sort(total)
  73. table.sort(provider)
  74. local ms = 1 / 1000000
  75. local function fmt(stats)
  76. return string.format(
  77. 'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms',
  78. stats[1] * ms,
  79. stats[1 + math.floor(#stats * 0.25)] * ms,
  80. stats[1 + math.floor(#stats * 0.5)] * ms,
  81. stats[1 + math.floor(#stats * 0.75)] * ms,
  82. stats[#stats] * ms
  83. )
  84. end
  85. print('\nTotal ' .. fmt(total) .. '\nDecoration provider: ' .. fmt(provider))
  86. end)
  87. end)