Skip to content

Instantly share code, notes, and snippets.

@Tattoo
Forked from mkorpela/keyword_times.py
Created August 12, 2013 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tattoo/6208942 to your computer and use it in GitHub Desktop.
Save Tattoo/6208942 to your computer and use it in GitHub Desktop.
from robot.api import ExecutionResult, ResultVisitor
import re
class KeywordTimes(ResultVisitor):
VAR_PATTERN = re.compile(r'^(\$|\@)\{[^\}]+\}(, \$\{[^\}]+\})* = ')
def __init__(self):
self.keywords = {}
def end_keyword(self, keyword):
name = self._get_name(keyword)
if name not in self.keywords:
self.keywords[name] = [name,0,0]
self.keywords[name][1] += keyword.elapsedtime
self.keywords[name][2] += 1
def _get_name(self, keyword):
name = keyword.name
m = self.VAR_PATTERN.search(name)
if m:
return name[m.end():]
return name
if __name__ == '__main__':
import sys
resu = ExecutionResult(sys.argv[1])
times = KeywordTimes()
resu.visit(times)
s = sorted(times.keywords.values(), lambda a,b: b[1]-a[1])
shown_keywords = 100
print 'Total time (ms) | Number of calls | Keyword name'
for k, t, c in s[:shown_keywords]:
print str(t).rjust(14)+' | '+str(c).rjust(15)+(' | "%s"' % k)
print 'Showing %d of total keywords %d' % (shown_keywords, len(times.keywords))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment