Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep a running total of each job runtime. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions callback_plugins/profile_tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import os
import time
from collections import defaultdict
from ansible.plugins.callback import CallbackBase


Expand All @@ -10,7 +11,8 @@ class CallbackModule(CallbackBase):
"""
def __init__(self):
super(CallbackModule, self).__init__()
self.stats = {}
self.start_times = {}
self.stats = defaultdict(lambda: 0)
self.current = None

def playbook_on_task_start(self, name, is_conditional):
Expand All @@ -23,11 +25,11 @@ def playbook_on_task_start(self, name, is_conditional):

if self.current is not None:
# Record the running time of the last executed task
self.stats[self.current] = time.time() - self.stats[self.current]
self.stats[self.current] = self.stats[self.current] + (time.time() - self.start_times[self.current])

# Record the start time of the current task
self.current = name
self.stats[self.current] = time.time()
self.start_times[self.current] = time.time()

def playbook_on_stats(self, stats):
"""
Expand All @@ -39,7 +41,7 @@ def playbook_on_stats(self, stats):

# Record the timing of the very last task
if self.current is not None:
self.stats[self.current] = time.time() - self.stats[self.current]
self.stats[self.current] = self.stats[self.current] + (time.time() - self.start_times[self.current])

# Sort the tasks by their running time
results = sorted(
Expand Down