aboutsummaryrefslogtreecommitdiff
path: root/test/benchmark/keycodes_spec.lua
blob: 443df2a6fb003d2ee226bf2eb5190962ec2f2002 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
local n = require('test.functional.testnvim')()
local clear = n.clear
local api = n.api
local fn = n.fn

local keycodes = require('src.nvim.keycodes')

describe('nvim_replace_termcodes performance', function()
  it('200 calls with a key repeated 5000 times', function()
    clear()
    local stats = {}
    local sum = 0
    local ms = 1 / 1000000

    for _, keycode in ipairs(keycodes.names) do
      local notation = ('<%s>'):format(keycode[2])
      local str = notation:rep(5000)

      local start = vim.uv.hrtime()
      for _ = 1, 200 do
        api.nvim_replace_termcodes(str, false, true, true)
      end
      local elapsed = vim.uv.hrtime() - start

      table.insert(stats, elapsed)
      sum = sum + elapsed
      io.stdout:write(('\n%-20s%14.6f ms'):format(notation, elapsed * ms))
      io.stdout:flush()
    end
    io.stdout:write('\n')

    table.sort(stats)
    print(('%18s'):rep(6):format('avg', 'min', '25%', 'median', '75%', 'max'))
    print(
      (' %14.6f ms'):rep(6):format(
        sum / #stats * ms,
        stats[1] * ms,
        stats[1 + math.floor(#stats * 0.25)] * ms,
        stats[1 + math.floor(#stats * 0.5)] * ms,
        stats[1 + math.floor(#stats * 0.75)] * ms,
        stats[#stats] * ms
      )
    )
  end)
end)

describe('keytrans() performance', function()
  it('200 calls with a key repeated 5000 times', function()
    clear()
    local stats = {}
    local sum = 0
    local ms = 1 / 1000000

    for _, keycode in ipairs(keycodes.names) do
      local notation = ('<%s>'):format(keycode[2])
      local str = api.nvim_replace_termcodes(notation, false, true, true):rep(5000)

      local start = vim.uv.hrtime()
      for _ = 1, 200 do
        fn.keytrans(str)
      end
      local elapsed = vim.uv.hrtime() - start

      table.insert(stats, elapsed)
      sum = sum + elapsed
      io.stdout:write(('\n%-20s%14.6f ms'):format(notation, elapsed * ms))
      io.stdout:flush()
    end
    io.stdout:write('\n')

    table.sort(stats)
    print((' %17s'):rep(6):format('avg', 'min', '25%', 'median', '75%', 'max'))
    print(
      (' %14.6f ms'):rep(6):format(
        sum / #stats * ms,
        stats[1] * ms,
        stats[1 + math.floor(#stats * 0.25)] * ms,
        stats[1 + math.floor(#stats * 0.5)] * ms,
        stats[1 + math.floor(#stats * 0.75)] * ms,
        stats[#stats] * ms
      )
    )
  end)
end)