Last active
July 9, 2021 14:09
-
-
Save mnojek/ff038ad0283b883fbbcdc4947cd47845 to your computer and use it in GitHub Desktop.
Robot Framework execution time analyzer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The script analyzes output file from Robot Framework execution | |
and calculates total time spent on specific keyword and libraries. | |
This can help to optimize the code in places where the execution is longer. | |
It takes XML file and outputs the results into 2 separate CSV files: | |
- kw_stats.csv that includes time spent on each keyword | |
- lib_stats.csv that includes time spent on each library | |
Each file content is structured like this: | |
<keyword or library name>;<total time in seconds> | |
To run the script just provide the path to XML file: | |
execution-time-checker.py output.xml | |
""" | |
import sys | |
from collections import defaultdict | |
from robot.api import ExecutionResult, ResultVisitor | |
total_time = defaultdict(int) | |
total_time_libs = defaultdict(int) | |
class ExecutionTimeChecker(ResultVisitor): | |
def visit_keyword(self, kw): | |
if kw.type in ["IF/ELSE ROOT", "FOR ITERATION"]: | |
return | |
if kw.type == "KEYWORD": | |
total_time[kw.name] += kw.elapsedtime / 1000 | |
if kw.libname: | |
total_time_libs[kw.libname] += kw.elapsedtime / 1000 | |
for keyword in kw.keywords: | |
self.visit_keyword(keyword) | |
def check_keywords(xml_file): | |
result = ExecutionResult(xml_file) | |
result.visit(ExecutionTimeChecker()) | |
sorted_result = dict(sorted(total_time.items(), key=lambda x: x[1], reverse=True)) | |
with open('kw_stats.csv', 'w') as f: | |
f.write("Keyword;Total time [s]\n") | |
for keyword, time in sorted_result.items(): | |
f.write("{keyword};{time:.3f}\n") | |
sorted_result = dict(sorted(total_time_libs.items(), key=lambda x: x[1], reverse=True)) | |
with open('lib_stats.csv', 'w') as f: | |
f.write("Library;Total time [s]\n") | |
for keyword, time in sorted_result.items(): | |
f.write(f"{keyword};{time:.3f}\n") | |
if __name__ == "__main__": | |
check_keywords(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment