Skip to content

Instantly share code, notes, and snippets.

@prayerslayer
Created January 28, 2019 16:29
Show Gist options
  • Save prayerslayer/6736a8ab10b83a3305aaecd01c2ef214 to your computer and use it in GitHub Desktop.
Save prayerslayer/6736a8ab10b83a3305aaecd01c2ef214 to your computer and use it in GitHub Desktop.
Find slow Robot Framework keywords
#!/usr/bin/env python
"""Usage: elapsed_by_keyword path/to/output.xml
Reads result of a test run from Robot output file
and outputs "keyword elapsed_time"
sorted by the elapsed time in each keyword.
"""
import sys
import datetime
from robot.api import ExecutionResult, ResultVisitor
class ExecutionKeywordStats(ResultVisitor):
def __init__(self):
self.elapsed_by = {}
def end_test(self, test):
if test.name in self.elapsed_by:
[_, elapsed, count] = self.elapsed_by[test.name]
self.elapsed_by[test.name] = ['test', elapsed + test.elapsedtime, count + 1]
else:
self.elapsed_by[test.name] = ['test', test.elapsedtime, 1]
def end_keyword(self, keyword):
if keyword.name in self.elapsed_by:
[_, elapsed, count] = self.elapsed_by[keyword.name]
self.elapsed_by[keyword.name] = ['keyword', elapsed + keyword.elapsedtime, count + 1]
else:
self.elapsed_by[keyword.name] = ['keyword', keyword.elapsedtime, 1]
def stats_by_keywords(path):
result = ExecutionResult(path)
visitor = ExecutionKeywordStats()
result.visit(visitor)
print('name,type,elapsed,count,avg')
for kw in sorted(visitor.elapsed_by, key=visitor.elapsed_by.get, reverse=True):
[t, elapsed, count] = visitor.elapsed_by[kw]
print(kw + ',' + t + ',' + str(elapsed) + ',' + str(count) + ',' + str(elapsed/count))
if __name__ == '__main__':
stats_by_keywords(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment