summaryrefslogtreecommitdiff
path: root/hacking/cgroup_perf_recap_graph.py
blob: 2234dee0a10a09a48106c7e7448938c473b3c613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) 2018, Matt Martz <matt@sivel.net>
#
# This file is part of Ansible
#
# Ansible 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.
#
# Ansible 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.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

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

import os
import argparse
import csv

from collections import namedtuple

try:
    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
except ImportError:
    raise SystemExit('matplotlib is required for this script to work')


Data = namedtuple('Data', ['axis_name', 'dates', 'names', 'values'])


def task_start_ticks(dates, names):
    item = None
    ret = []
    for i, name in enumerate(names):
        if name == item:
            continue
        item = name
        ret.append((dates[i], name))
    return ret


def create_axis_data(filename, relative=False):
    x_base = None if relative else 0

    axis_name, dummy = os.path.splitext(os.path.basename(filename))

    dates = []
    names = []
    values = []
    with open(filename) as f:
        reader = csv.reader(f)
        for row in reader:
            if x_base is None:
                x_base = float(row[0])
            dates.append(mdates.epoch2num(float(row[0]) - x_base))
            names.append(row[1])
            values.append(float(row[3]))

    return Data(axis_name, dates, names, values)


def create_graph(data1, data2, width=11.0, height=8.0, filename='out.png', title=None):
    fig, ax1 = plt.subplots(figsize=(width, height), dpi=300)

    task_ticks = task_start_ticks(data1.dates, data1.names)

    ax1.grid(linestyle='dashed', color='lightgray')
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%X'))
    ax1.plot(data1.dates, data1.values, 'b-')
    if title:
        ax1.set_title(title)
    ax1.set_xlabel('Time')
    ax1.set_ylabel(data1.axis_name, color='b')
    for item in ax1.get_xticklabels():
        item.set_rotation(60)

    ax2 = ax1.twiny()
    ax2.set_xticks([x[0] for x in task_ticks])
    ax2.set_xticklabels([x[1] for x in task_ticks])
    ax2.grid(axis='x', linestyle='dashed', color='lightgray')
    ax2.xaxis.set_ticks_position('bottom')
    ax2.xaxis.set_label_position('bottom')
    ax2.spines['bottom'].set_position(('outward', 86))
    ax2.set_xlabel('Task')
    ax2.set_xlim(ax1.get_xlim())
    for item in ax2.get_xticklabels():
        item.set_rotation(60)

    ax3 = ax1.twinx()
    ax3.plot(data2.dates, data2.values, 'g-')
    ax3.set_ylabel(data2.axis_name, color='g')
    fig.tight_layout()
    fig.savefig(filename, format='png')


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('files', nargs=2, help='2 CSV files produced by cgroup_perf_recap to graph together')
    parser.add_argument('--relative', default=False, action='store_true',
                        help='Use relative dates instead of absolute')
    parser.add_argument('--output', default='out.png', help='output path of PNG file: Default %s(default)s')
    parser.add_argument('--width', type=float, default=11.0,
                        help='Width of output image in inches. Default %(default)s')
    parser.add_argument('--height', type=float, default=8.0,
                        help='Height of output image in inches. Default %(default)s')
    parser.add_argument('--title', help='Title for graph')
    return parser.parse_args()


def main():
    args = parse_args()
    data1 = create_axis_data(args.files[0], relative=args.relative)
    data2 = create_axis_data(args.files[1], relative=args.relative)
    create_graph(data1, data2, width=args.width, height=args.height, filename=args.output, title=args.title)
    print('Graph written to %s' % os.path.abspath(args.output))


if __name__ == '__main__':
    main()