Skip to content

Commit

Permalink
WIP: parse "Source:" from profile output with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Aug 14, 2018
1 parent 0a5f292 commit 100f420
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 9 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fixtures: tests/fixtures/conditional_function.profile
fixtures: tests/fixtures/function_in_function.profile
fixtures: tests/fixtures/function_in_function_count.profile
fixtures: tests/fixtures/function_in_function_with_ref.profile
fixtures: tests/fixtures/duplicate_s_function.profile
fixtures: $(PROFILES_TO_MERGE_COND)

# TODO: cleanup. Should be handled by the generic rule at the bottom.
Expand Down Expand Up @@ -55,9 +56,11 @@ $(PROFILES_TO_MERGE_COND): tests/test_plugin/merged_conditionals.vim Makefile
done
sed -i 's:^SCRIPT .*/test_plugin:SCRIPT tests/test_plugin:' $(PROFILES_TO_MERGE_COND)

tests/fixtures/%.profile: tests/test_plugin/%.vim Makefile
$(VIM) --noplugin -Nu tests/t.vim --cmd 'let g:prof_fname = "$@"' -c 'source $<' -c q
sed -i 's:^SCRIPT .*/test_plugin:SCRIPT tests/test_plugin:' $@
tests/fixtures/%.profile: tests/test_plugin/%.vim Makefile tests/t.vim
$(VIM) --noplugin -Nu tests/t.vim --cmd 'let g:prof_fname = "$@"' -c 'source $<' \
-c 'exe empty(v:errmsg) ? "qall" : "echoerr v:errmsg"'
sed -Ei 's:^SCRIPT .*/tests/test_plugin:SCRIPT tests/test_plugin:' $@
sed -Ei 's~^ Source: .*/tests/test_plugin~ Source: tests/test_plugin~' $@


# Helpers to generate (combined) coverage and show a diff {{{
Expand Down
27 changes: 22 additions & 5 deletions covimerage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Function(object):
total_time = attr.ib(default=None)
self_time = attr.ib(default=None)
lines = attr.ib(default=attr.Factory(dict), repr=False)
source = None


@attr.s
Expand Down Expand Up @@ -206,6 +207,7 @@ def write_coveragepy_data(self, data_file='.coverage'):
@attr.s
class Profile(object):
fname = attr.ib()
# TODO: make this a dict? (scripts_by_fname)
scripts = attr.ib(default=attr.Factory(list))
anonymous_functions = attr.ib(default=attr.Factory(dict))
_fobj = None
Expand All @@ -214,6 +216,7 @@ class Profile(object):
def __attrs_post_init__(self):
self.fname, self._fobj, self._fstr = get_fname_and_fobj_and_str(
self.fname)
self.scripts_by_fname = {}

@property
def scriptfiles(self):
Expand Down Expand Up @@ -396,6 +399,7 @@ def skip_to_count_header():
in_script = Script(fname)
logger.debug('Parsing script %s', in_script)
self.scripts.append(in_script)
self.scripts_by_fname[fname] = in_script

next_line = next(file_object)
m = RE_SOURCED_TIMES.match(next_line)
Expand All @@ -408,7 +412,17 @@ def skip_to_count_header():
func_name = line[10:-2]
in_function = Function(name=func_name)
logger.debug('Parsing function %s', in_function)
plnum += skip_to_count_header()
while True:
next_line = next(file_object)
if not next_line:
break
plnum += 1
if next_line.startswith('count'):
break
if next_line.startswith(' Source:'):
fname, lnum = next_line[12:].rstrip().rsplit(':', 1)
in_function.source = (self.scripts_by_fname[fname],
int(lnum))
lnum = 0
self.map_functions(functions)

Expand All @@ -426,12 +440,15 @@ def map_functions(self, functions):
logger.error('Could not find source for function: %s', f.name)

def map_function(self, f):
script_line = self.find_func_in_source(f)
if not script_line:
return False
if f.source:
script, script_lnum = f.source
else:
script_line = self.find_func_in_source(f)
if not script_line:
return False
script, script_lnum = script_line

# Assign counts from function to script.
script, script_lnum = script_line
for [f_lnum, f_line] in f.lines.items():
s_line = script.lines[script_lnum + f_lnum]

Expand Down
67 changes: 67 additions & 0 deletions tests/fixtures/duplicate_s_function.profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
SCRIPT tests/test_plugin/duplicate_s_function.vim
Sourced 1 time
Total time: 0.001189
Self time: 0.000583

count total (s) self (s)
" https://github.com/vim/vim/issues/3286
1 0.000052 function! s:function(name) abort
echom a:name
endfunction

1 0.000367 0.000149 call s:function('name')
1 0.000691 0.000303 call test_plugin#function#function('name')

SCRIPT tests/test_plugin/autoload/test_plugin/function.vim
Sourced 1 time
Total time: 0.000099
Self time: 0.000099

count total (s) self (s)
1 0.000036 function! s:function(name) abort
echom a:name
endfunction

1 0.000005 function! test_plugin#function#function(name) abort
call s:function(a:name)
endfunction

FUNCTION test_plugin#function#function()
Called 1 time
Total time: 0.000201
Self time: 0.000036
Source: tests/test_plugin/autoload/test_plugin/function.vim:5

count total (s) self (s)
1 0.000198 0.000033 call s:function(a:name)

FUNCTION <SNR>2_function()
Called 1 time
Total time: 0.000217
Self time: 0.000217
Source: tests/test_plugin/duplicate_s_function.vim:2

count total (s) self (s)
1 0.000200 echom a:name

FUNCTION <SNR>3_function()
Called 1 time
Total time: 0.000165
Self time: 0.000165
Source: tests/test_plugin/autoload/test_plugin/function.vim:1

count total (s) self (s)
1 0.000163 echom a:name

FUNCTIONS SORTED ON TOTAL TIME
count total (s) self (s) function
1 0.000217 <SNR>2_function()
1 0.000201 0.000036 test_plugin#function#function()
1 0.000165 <SNR>3_function()

FUNCTIONS SORTED ON SELF TIME
count total (s) self (s) function
1 0.000217 <SNR>2_function()
1 0.000165 <SNR>3_function()
1 0.000201 0.000036 test_plugin#function#function()

3 changes: 2 additions & 1 deletion tests/t.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ let prof_fname = get(g:, 'prof_fname')
exe 'profile start '.prof_fname
profile! file tests/test_plugin/**

set runtimepath+=$PWD/test_plugin
let sfile = expand('<sfile>')
let &runtimepath = $PWD.'/tests/test_plugin,'.&runtimepath

" call test_plugin#func1(1)

Expand Down
35 changes: 35 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,38 @@ def test_map_functions(caplog):
assert len(funcs) == 1
assert caplog.record_tuples == [
('covimerage', 40, 'Could not find source for function: missing')]


def test_duplicate_s_function(caplog):
from covimerage import Profile

fname = 'tests/fixtures/duplicate_s_function.profile'
p = Profile(fname)
p.parse()

assert len(p.scripts) == 2

N = None
assert [(l.count, l.line)
for l in p.scripts[0].lines.values()
if not l.line.startswith('"')] == [
(1, 'function! s:function(name) abort'),
(1, ' echom a:name'),
(N, 'endfunction'),
(N, ''),
(1, "call s:function('name')"),
(1, "call test_plugin#function#function('name')"),
]

assert [(l.count, l.line)
for l in p.scripts[1].lines.values()
if not l.line.startswith('"')] == [
(1, 'function! s:function(name) abort'),
(1, ' echom a:name'),
(N, 'endfunction'),
(N, ''),
(1, 'function! test_plugin#function#function(name) abort'),
(1, ' call s:function(a:name)'),
(N, 'endfunction')]

assert not caplog.records
7 changes: 7 additions & 0 deletions tests/test_plugin/autoload/test_plugin/function.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function! s:function(name) abort
echom a:name
endfunction

function! test_plugin#function#function(name) abort
call s:function(a:name)
endfunction
7 changes: 7 additions & 0 deletions tests/test_plugin/duplicate_s_function.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
" https://github.com/vim/vim/issues/3286
function! s:function(name) abort
echom a:name
endfunction

call s:function('name')
call test_plugin#function#function('name')

0 comments on commit 100f420

Please sign in to comment.