Get an Excel-friendly execution time log from your app.
This class is meant for debugging performance issues. You can log the execution time for specific parts of code and group actions by type/category.
The log is a CSV file which allows you to do simple visualization to help you understand what's going on in your code.
ExecutionTimer(log_file[, delimiter=',', is_enabled=True])
When initializing the timer you must provide the path to the log file as a string. The file name including extension must be part of the string. delimiter defaults to ',' but could be ';' or "\t" too depending on your spreadsheet app. is_enabled allows you to disable logging when you don't need it, without having to remove all the log lines in code.
ExecutionTimer.start(action[, category=None])
Starts a new log line, with action as the text in the log file. category is optional. It can help you group different types of actions.
ExecutionTimer.end(action[, comment])
This is run after start(). The action param should be the same as the action of the previous start. comment allows you to add extra info into the log. For instance the SQL statement when logging data fetching.
ExecutionTimer.complete()
Ends logging. Should usually be placed at the very end of your code.
See example.py for a working example of ExecutionTimer. The output of the example can be found in example_log.csv
Initialize the timer.
This should be done at the start of the code.
ExecutionTimer('./example_log.csv', ',', True)
Log some action
The start method starts logging, the end method ends it.
ExecutionTimer.start('Doing some stuff', 'Stuff')
print("Doing stuff for 2 secs...")
time.sleep(2)
ExecutionTimer.end('Doing some stuff')
End logging
At the end of your code, you should complete the log. It will give you the total exec time.
ExecutionTimer.complete()
The example.py script creates a random report with some data collection and report generation logs. example_log.csv is a log file generated by the example script.
Here is an example of a Google Sheet with some analysis based on the log: https://docs.google.com/spreadsheets/d/1MH2bx2yxt3jqQX0KxdsxWz9D2MsmE7EZjOn0RJeMLPg/edit?usp=sharing