Jianw
2025-05-13 3b39fe3810c3ee2ec9ec97236c1769c5c85e062c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- Require luasocket only when needed.
local socket
 
local profiler = {}
 
local metrics = {
   {name = "Wall", get = function() return socket.gettime() end},
   {name = "CPU", get = os.clock},
   {name = "Memory", get = function() return collectgarbage("count") end}
}
 
local functions = {
   {name = "load", module = "cache"},
   {name = "update", module = "cache"},
   {name = "decode", module = "decoder"},
   {name = "parse", module = "parser"},
   {name = "run", module = "stages.unwrap_parens"},
   {name = "run", module = "stages.parse_inline_options"},
   {name = "run", module = "stages.linearize"},
   {name = "run", module = "stages.name_functions"},
   {name = "run", module = "stages.resolve_locals"},
   {name = "run", module = "stages.detect_bad_whitespace"},
   {name = "run", module = "stages.detect_cyclomatic_complexity"},
   {name = "run", module = "stages.detect_empty_blocks"},
   {name = "run", module = "stages.detect_empty_statements"},
   {name = "run", module = "stages.detect_globals"},
   {name = "run", module = "stages.detect_reversed_fornum_loops"},
   {name = "run", module = "stages.detect_unbalanced_assignments"},
   {name = "run", module = "stages.detect_uninit_accesses"},
   {name = "run", module = "stages.detect_unreachable_code"},
   {name = "run", module = "stages.detect_unused_fields"},
   {name = "run", module = "stages.detect_unused_locals"},
   {name = "filter", module = "filter"},
   {name = "normalize", module = "options"}
}
 
local stats = {}
local start_values = {}
 
local function start_phase(name)
   for _, metric in ipairs(metrics) do
      start_values[metric][name] = metric.get()
   end
end
 
local function stop_phase(name)
   for _, metric in ipairs(metrics) do
      local increment = metric.get() - start_values[metric][name]
      stats[metric][name] = (stats[metric][name] or 0) + increment
   end
end
 
local phase_stack = {}
 
local function push_phase(name)
   local prev_name = phase_stack[#phase_stack]
 
   if prev_name then
      stop_phase(prev_name)
   end
 
   table.insert(phase_stack, name)
   start_phase(name)
end
 
local function pop_phase(name)
   assert(name == table.remove(phase_stack))
   stop_phase(name)
   local prev_name = phase_stack[#phase_stack]
 
   if prev_name then
      start_phase(prev_name)
   end
end
 
local function continue_wrapper(name, ...)
   pop_phase(name)
   return ...
end
 
local function wrap(fn, name)
   return function(...)
      push_phase(name)
      return continue_wrapper(name, fn(...))
   end
end
 
local function patch(fn)
   local mod = require("luacheck." .. fn.module)
   local orig = mod[fn.name]
   local new = wrap(orig, fn.module .. "." .. fn.name)
   mod[fn.name] = new
end
 
function profiler.init()
   socket = require "socket"
   collectgarbage("stop")
 
   for _, metric in ipairs(metrics) do
      stats[metric] = {}
      start_values[metric] = {}
   end
 
   for _, fn in ipairs(functions) do
      patch(fn)
   end
 
   push_phase("other")
end
 
function profiler.report()
   pop_phase("other")
 
   for _, metric in ipairs(metrics) do
      local names = {}
      local total = 0
 
      for name, value in pairs(stats[metric]) do
         table.insert(names, name)
         total = total + value
      end
 
      table.sort(names, function(name1, name2)
         local stats1 = stats[metric][name1]
         local stats2 = stats[metric][name2]
 
         if stats1 ~= stats2 then
            return stats1 > stats2
         else
            return name1 < name2
         end
      end)
 
      print(metric.name)
      print()
 
      for _, name in ipairs(names) do
         print(("%s - %f (%f%%)"):format(name, stats[metric][name], stats[metric][name] / total * 100))
      end
 
      print(("Total - %f"):format(total))
      print()
   end
end
 
return profiler