Repository: ansible/ansible

Issue:
Ansible callback plugin profile_tasks doesn't handle tasks with duplicate name
ansible 2.0.0.2

Run against a set of tasks like so:

```
- name: Duplicate
  debug: msg="hi"

- name: Duplicate
  command: sleep 100
```

You'll notice you only get timing on one. This is flaw in the core architecture of this plugin. It uses a dict to store values by task name it seems.

@aioue 



Discussion:
I ended up modifying my copy to use a list. I also chose to record start, stop, and delta times on the dictionary in the array. 

I would put up a PR but I actually moded it to work on 1.9. I also removed sorting and the limit of 10. 

@jjshoe do you have the 1.9 version published anywhere?

@aioue it's highly modified, would love to see ENV vars control some of what I've done, hold on a second and I'll get you a gist.

```
# (C) 2015, Tom Paine, <github@aioue.net>
# (C) 2014, Jharrod LaFon, @JharrodLaFon
# (C) 2012-2013, Michael DeHaan, <michael.dehaan@gmail.com>
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# File is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# See <http://www.gnu.org/licenses/> for a copy of the
# GNU General Public License

# Provides per-task timing, ongoing playbook elapsed time and
# ordered list of top 20 longest running tasks at end

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import time

from collections import OrderedDict

# define start time
t0 = tn = time.time()

def secondsToStr(t):
    # http://bytes.com/topic/python/answers/635958-handy-short-cut-formatting-elapsed-time-floating-point-seconds
    rediv = lambda ll, b: list(divmod(ll[0], b)) + ll[1:]
    return "%d:%02d:%02d.%03d" % tuple(reduce(rediv, [[t * 1000, ], 1000, 60, 60]))


def filled(msg, fchar="*"):
    if len(msg) == 0:
        width = 79
    else:
        msg = "%s " % msg
        width = 79 - len(msg)
    if width < 3:
        width = 3
    filler = fchar * width
    return "%s%s " % (msg, filler)


def timestamp(self):
    self.stats.reverse()
    if self.current is not None:
        for stat in self.stats:
            if stat['task'] == self.current:
                stat['end_time'] = time.time()
                stat['duration'] = stat['end_time'] - stat['start_time']
                self.stats.reverse()
                return
    self.stats.reverse()

def tasktime():
    global tn
    time_current = time.strftime('%A %d %B %Y  %H:%M:%S %z')
    time_elapsed = secondsToStr(time.time() - tn)
    time_total_elapsed = secondsToStr(time.time() - t0)
    tn = time.time()
    return filled('%s (%s)%s%s' % (time_current, time_elapsed, ' ' * 7, time_total_elapsed))


class CallbackModule(object):
    """
    This callback module provides per-task timing, ongoing playbook elapsed time
    and ordered list of top 20 longest running tasks at end.
    """
    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'aggregate'
    CALLBACK_NAME = 'profile_tasks'
    CALLBACK_NEEDS_WHITELIST = True

    def __init__(self):
        self.stats = []
        self.current = None

        super(CallbackModule, self).__init__()

    def _record_task(self, name):
        """
        Logs the start of each task
        """
        print(tasktime())
        timestamp(self)

        # Record the start time of the current task
        self.current = name
        self.stats.append({'task': self.current, 'start_time': time.time()})

    def playbook_on_task_start(self, name, is_conditional):
        self._record_task(name)

    def v2_playbook_on_handler_task_start(self, task):
        self._record_task('HANDLER: ' + task.name)

    def playbook_on_setup(self):
        print(tasktime())

    def playbook_on_stats(self, stats):
        print(tasktime())
        print(filled("", fchar="="))

        timestamp(self)

        # Sort the tasks by their running time
        #results = sorted(
        #    self.stats.items(),
        #    key=lambda value: value[1],
        #    reverse=True,
        #)

        # Just keep the top 20
        # results = results[:20]

        # Print the timings
        for stat in self.stats:
            print(
                "{0:-<70}{1:->9}".format(
                    '{0} '.format(stat['task']),
                    ' {0:.02f}s'.format(stat['duration']),
                )
            )
```

I wouldn't be in a hurry to run with this, there's some clean ups that can be done in so you don't need the .reverses as well. Working on updating it now. 


Resolving patch:
diff --git a/lib/ansible/plugins/callback/profile_tasks.py b/lib/ansible/plugins/callback/profile_tasks.py
--- a/lib/ansible/plugins/callback/profile_tasks.py
+++ b/lib/ansible/plugins/callback/profile_tasks.py
@@ -1,3 +1,4 @@
+# (C) 2016, Joel, http://github.com/jjshoe 
 # (C) 2015, Tom Paine, <github@aioue.net>
 # (C) 2014, Jharrod LaFon, @JharrodLaFon
 # (C) 2012-2013, Michael DeHaan, <michael.dehaan@gmail.com>
@@ -22,6 +23,8 @@
 from __future__ import (absolute_import, division, print_function)
 __metaclass__ = type
 
+import collections
+import os
 import time
 
 from ansible.plugins.callback import CallbackBase
@@ -49,7 +52,7 @@ def filled(msg, fchar="*"):
 
 def timestamp(self):
     if self.current is not None:
-        self.stats[self.current] = time.time() - self.stats[self.current]
+        self.stats[self.current]['time'] = time.time() - self.stats[self.current]['time']
 
 
 def tasktime():
@@ -72,12 +75,22 @@ class CallbackModule(CallbackBase):
     CALLBACK_NEEDS_WHITELIST = True
 
     def __init__(self):
-        self.stats = {}
+        self.stats = collections.OrderedDict()
         self.current = None
+        self.sort_order = os.getenv('PROFILE_TASKS_SORT_ORDER', True)
+        self.task_output_limit = os.getenv('PROFILE_TASKS_TASK_OUTPUT_LIMIT', 20)
+
+        if self.sort_order == 'ascending':
+            self.sort_order = False;
+
+        if self.task_output_limit == 'all':
+            self.task_output_limit = None
+        else:
+            self.task_output_limit = int(self.task_output_limit) 
 
         super(CallbackModule, self).__init__()
 
-    def _record_task(self, name):
+    def _record_task(self, task):
         """
         Logs the start of each task
         """
@@ -85,14 +98,14 @@ def _record_task(self, name):
         timestamp(self)
 
         # Record the start time of the current task
-        self.current = name
-        self.stats[self.current] = time.time()
+        self.current = task._uuid
+        self.stats[self.current] = {'time': time.time(), 'name': task.get_name(), 'path': task.get_path()}
 
-    def playbook_on_task_start(self, name, is_conditional):
-        self._record_task(name)
+    def v2_playbook_on_task_start(self, task, is_conditional):
+        self._record_task(task)
 
     def v2_playbook_on_handler_task_start(self, task):
-        self._record_task('HANDLER: ' + task.name)
+        self._record_task(task)
 
     def playbook_on_setup(self):
         self._display.display(tasktime())
@@ -103,21 +116,25 @@ def playbook_on_stats(self, stats):
 
         timestamp(self)
 
-        # Sort the tasks by their running time
-        results = sorted(
-            self.stats.items(),
-            key=lambda value: value[1],
-            reverse=True,
-        )
+        results = self.stats.items() 
+
+        # Sort the tasks by the specified sort
+        if self.sort_order != 'none':
+            results = sorted(
+                self.stats.iteritems(),
+                key=lambda x:x[1]['time'],
+                reverse=self.sort_order,
+            )
 
-        # Just keep the top 20
-        results = results[:20]
+        # Display the number of tasks specified or the default of 20 
+        results = results[:self.task_output_limit]
 
         # Print the timings
-        for name, elapsed in results:
+        for uuid, result in results:
             self._display.display(
-                "{0:-<70}{1:->9}".format(
-                    '{0} '.format(name),
-                    ' {0:.02f}s'.format(elapsed),
+                "{0:-<70}{1:-<70}{2:->9}".format(
+                    '{0} '.format(result['path']),
+                    '{0} '.format(result['name']),
+                    ' {0:.02f}s'.format(result['time']),
                 )
             )


Test patch: