Skip to content

Commit bb8ad9e

Browse files
committed
Add ansible profiler of tasks for tests.
1 parent 6a38960 commit bb8ad9e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# profile_tasks.py: an Ansible plugin for timing tasks
2+
3+
# Copyright (C) 2014 Jharrod LaFon <[email protected]>
4+
# https://github.com/jlafon/ansible-profile/
5+
# Included with permission
6+
7+
8+
# The MIT License (MIT)
9+
#
10+
# Copyright (c) 2014 Jharrod LaFon
11+
#
12+
# Permission is hereby granted, free of charge, to any person obtaining a copy
13+
# of this software and associated documentation files (the "Software"), to deal
14+
# in the Software without restriction, including without limitation the rights
15+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
# copies of the Software, and to permit persons to whom the Software is
17+
# furnished to do so, subject to the following conditions:
18+
#
19+
# The above copyright notice and this permission notice shall be included in
20+
# all copies or substantial portions of the Software.
21+
#
22+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
# SOFTWARE.
29+
30+
31+
from ansible.plugins.callback import CallbackBase
32+
import time
33+
34+
35+
class CallbackModule(CallbackBase):
36+
"""
37+
A plugin for timing tasks
38+
"""
39+
def __init__(self):
40+
self.stats = {}
41+
self.current = None
42+
43+
def playbook_on_task_start(self, name, is_conditional):
44+
"""
45+
Logs the start of each task
46+
"""
47+
if self.current is not None:
48+
# Record the running time of the last executed task
49+
self.stats[self.current] = time.time() - self.stats[self.current]
50+
51+
# Record the start time of the current task
52+
self.current = name
53+
self.stats[self.current] = time.time()
54+
55+
def playbook_on_stats(self, stats):
56+
"""
57+
Prints the timings
58+
"""
59+
# Record the timing of the very last task
60+
if self.current is not None:
61+
self.stats[self.current] = time.time() - self.stats[self.current]
62+
63+
# Sort the tasks by their running time
64+
results = sorted(self.stats.items(),
65+
key=lambda value: value[1], reverse=True)
66+
67+
# Just keep the top 25
68+
results = results[:25]
69+
70+
# Print the timings
71+
for name, elapsed in results:
72+
print("{0:-<70}{1:->9}".format(
73+
'{0} '.format(name),
74+
' {0:.02f}s'.format(elapsed)))

0 commit comments

Comments
 (0)