|
|
|
# profile_tasks.py: an Ansible plugin for timing tasks
|
|
|
|
|
|
|
|
# Copyright (C) 2014 Jharrod LaFon <jharrod.lafon@gmail.com>
|
|
|
|
# https://github.com/jlafon/ansible-profile/
|
|
|
|
# Included with permission
|
|
|
|
|
|
|
|
|
|
|
|
# The MIT License (MIT)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2014 Jharrod LaFon
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.plugins.callback import CallbackBase
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
class CallbackModule(CallbackBase):
|
|
|
|
"""
|
|
|
|
A plugin for timing tasks
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
tests: Fix AttributeError in callback plugins used by test suite
CALLBACK_VERSION et al are documented as required in
https://docs.ansible.com/ansible/2.10/dev_guide/developing_plugins.html#callback-plugins.
The need for document_fragment is noted in
https://github.com/ansible/ansible/blob/cfa8075537f5fa83ca5bfe3170373aaef9ce932f/lib/ansible/plugins/callback/default.py#L28-L32
Fixes #758
This addresses the following error, seen while running
`ansible_tests.py`.
```
TASK [Gathering Facts gather_timeout=10, gather_subset=['all']]
****************
task path:
/home/alex/src/mitogen/tests/ansible/regression/issue_109__target_has_old_ansible_installed.yml:4
[WARNING]: Failure using method (v2_runner_on_start) in callback plugin
(<ansible.plugins.callback.nice_stdout.CallbackModule object at
0x7f76b3dad090>): 'show_per_host_start'
Callback Exception:
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py",
line 372, in send_callback
method(*new_args, **kwargs)
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/plugins/callback/default.py",
line 240, in v2_runner_on_start
if self.get_option('show_per_host_start'):
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/plugins/callback/__init__.py",
line 91, in get_option
return self._plugin_options[k]
Callback Exception:
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py",
line 372, in send_callback
method(*new_args, **kwargs)
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/plugins/callback/default.py",
line 240, in v2_runner_on_start
if self.get_option('show_per_host_start'):
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/plugins/callback/__init__.py",
line 91, in get_option
return self._plugin_options[k]
[task 339882] 00:00:08.172036 D ansible_mitogen.affinity: CPU mask for
WorkerProcess: 0x000004
Callback Exception:
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py",
line 372, in send_callback
method(*new_args, **kwargs)
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/plugins/callback/default.py",
line 240, in v2_runner_on_start
if self.get_option('show_per_host_start'):
File
"/home/alex/src/mitogen/.tox/py27-ansible2.10/lib/python2.7/site-packages/ansible/plugins/callback/__init__.py",
line 91, in get_option
return self._plugin_options[k]
```
4 years ago
|
|
|
super(CallbackModule, self).__init__()
|
|
|
|
self.stats = {}
|
|
|
|
self.current = None
|
|
|
|
|
|
|
|
def playbook_on_task_start(self, name, is_conditional):
|
|
|
|
"""
|
|
|
|
Logs the start of each task
|
|
|
|
"""
|
|
|
|
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]
|
|
|
|
|
|
|
|
# Record the start time of the current task
|
|
|
|
self.current = name
|
|
|
|
self.stats[self.current] = time.time()
|
|
|
|
|
|
|
|
called = False
|
|
|
|
|
|
|
|
def playbook_on_stats(self, stats):
|
|
|
|
"""
|
|
|
|
Prints the timings
|
|
|
|
"""
|
|
|
|
if CallbackModule.called:
|
|
|
|
return
|
|
|
|
CallbackModule.called = True
|
|
|
|
|
|
|
|
# Record the timing of the very last task
|
|
|
|
if self.current is not None:
|
|
|
|
self.stats[self.current] = time.time() - self.stats[self.current]
|
|
|
|
|
|
|
|
# Sort the tasks by their running time
|
|
|
|
results = sorted(self.stats.items(),
|
|
|
|
key=lambda value: value[1], reverse=True)
|
|
|
|
|
|
|
|
# Just keep the top 10
|
|
|
|
results = results[:10]
|
|
|
|
|
|
|
|
# Print the timings
|
|
|
|
for name, elapsed in results:
|
|
|
|
print("{0:-<70}{1:->9}".format(
|
|
|
|
'{0} '.format(name),
|
|
|
|
' {0:.02f}s'.format(elapsed)))
|
|
|
|
|