summaryrefslogtreecommitdiff
path: root/mnv/runtime/indent/testdir/tools/tracer.mnv
blob: 74dfc054958020200b964862074eb399800ebcd0 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
mnv9script

# Whenever indent plugins contain "search*()" lines explicitly annotated with
# "MNV_INDENT_TEST_TRACE_(START|END)" comment markers; this script can then be
# used as shown to measure and record elapsed time for such decorated calls.
#
# Usage:
#	cd runtime/indent
#	mnv -u NONE -S testdir/tools/tracer.mnv \
#		html.mnv javascript.mnv \
#		../autoload/python.mnv ../autoload/dist/mnvindent.mnv
#	git diff
#	make clean test
#	mnv testdir/00-TRACE_LOG.fail

def GenerateTempletForTracing(fname: string, vname: string): list<string>
  #### ONLY INSTRUMENT "search*()"es FOR INDENT TESTS.

  const templet: list<string> =<< trim eval END

  if getcwd() =~# '\<runtime/indent$'

  def! g:IndentTestTrace(id: string, start: list<number>, result: any): any
    const end: list<number> = reltime(start)

    if !has_key(g:indent_test_trace_times, id)
      g:indent_test_trace_times[id] = []
    endif

    g:indent_test_trace_times[id]
	->add(reltimefloat(end))
    return result
  enddef

  def! g:IndentTestInitTracing()
    # Possibly use a later "{fname}", cf. ":runtime indent/foo.mnv".
    autocmd_add([{{
	replace: true,
	group:	'tracing',
	event:	'QuitPre',
	bufnr:	bufnr(),
	cmd:	'g:IndentTestWriteTraceTimes()',
      }}])
    g:indent_test_trace_times = {{}}
  enddef

  def! g:IndentTestWriteTraceTimes()
    # Anticipate usage by multiple languages.
    const token: string = printf('%02x', (rand() % 26))
    writefile(['" {fname}:',
	    "let {vname}_" .. token .. " = " .. string(g:indent_test_trace_times),
	    "let {vname}_" .. token .. "_summary = " .. string(g:indent_test_trace_times
		->items()
		->reduce((outer: dict<dict<any>>, times: list<any>) =>
		    extend({{[times[0]]: times[1]
			      ->copy()
			      ->reduce((inner: dict<any>, v: float) =>
				  extend({{
				      min: inner.min < v ? inner.min : v,
				      max: inner.max > v ? inner.max : v,
				      sum: (inner.sum + v),
				      avg: ((inner.sum + v) / inner.count),
				    }},
				    inner,
				    "keep"),
				  {{
				      min: v:numbermax - 0.0,
				      max: v:numbermin + 0.0,
				      sum: 0.0,
				      avg: 0.0,
				      count: len(times[1]),
				  }})}},
			    outer),
			{{}}))],
	(!empty($MNV_INDENT_TEST_LOG) && filewritable($MNV_INDENT_TEST_LOG))
	    ? $MNV_INDENT_TEST_LOG
	    : "testdir/00-TRACE_LOG.fail",
	"a")
  enddef

  call g:IndentTestInitTracing()

  else

  def! g:IndentTestTrace(_: string, _: list<number>, result: any): any
    return result
  enddef

  endif

  END
  return templet
enddef

def InstrumentMarkedEntry(): bool
  const marker_start: string = 'MNV_INDENT_TEST_TRACE_START'
  const start: number = search('\C\<' .. marker_start .. '\>', 'ceW')

  if start == 0
    return false
  endif

  const marker_end: string = 'MNV_INDENT_TEST_TRACE_END'
  const end: number = search('\C\<' .. marker_end .. '\>', 'ceW')

  if end == 0
    return false
  endif

  const tracee: list<string> = matchlist(
    getline(start + 1),
    '\(^.\+\)\(\<search\%(pair\)\=\%(pos\)\=\s*(.*$\)')

  if empty(get(tracee, 1, '')) || empty(get(tracee, 2, ''))
    return false
  endif

  const end_line: string = getline(end)
  const tracer: string = printf('%sg:IndentTestTrace("%s", reltime(), %s',
      tracee[1],
      strpart(end_line, (stridx(end_line, marker_end) + strlen(marker_end) + 1)),
      tracee[2])

  if (end - start) > 1
    setline((start + 1), tracer)
    setline((end - 1), getline(end - 1) .. ')')
  else
    setline((start + 1), tracer .. ')')
  endif

  return true
enddef

def ProcessIndentPluginCmdlineArgs()
  const names: list<string> = range(char2nr('a'), char2nr('z'))
      ->map((_: number, n: number) => nr2char(n, true))
  var entries: number = 0
  var next: number = 0

  for fname: string in argv(-1)
    if filereadable(fname) && filewritable(fname)
      execute 'new ' .. fname
      call cursor(1, 1)

      while InstrumentMarkedEntry()
	entries += 1
      endwhile

      if entries > 0
	append(1, GenerateTempletForTracing(fname, get(names, next, names[-1])))
	wq
      endif

      entries = 0
      next += 1
    endif
  endfor
enddef

if empty(system('git status --porcelain=v1'))
  ProcessIndentPluginCmdlineArgs()
endif

quitall

# mnv:fdm=syntax:sw=2:ts=8:noet:nosta: