summaryrefslogtreecommitdiff
path: root/scripts/profiling/sync/plot.py
blob: 8e3e1c159789458c6dc1a2fda82537ccddb4201b (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
#!/usr/bin/python


import argparse
from matplotlib import pyplot as plt
from movingaverage import movingaverage
from scipy.interpolate import interp1d
from numpy import linspace


def smooth(l):
    return movingaverage(l, 3, data_is_list=True, avoid_fp_drift=False)


def plot(filename, subtitle=''):

    # config the plot
    plt.xlabel('time (s)')
    plt.ylabel('usage (%)')
    title = 'soledad sync'
    if subtitle != '':
        title += '- %s' % subtitle
    plt.title(title)

    x = []
    ycpu = []
    ymem = []
    ypcpu = []
    ypmem = []

    ys = [
        (ycpu, 'total cpu', 'r'),
        (ymem, 'total mem', 'b'),
        (ypcpu, 'proc cpu', 'm'),
        (ypmem, 'proc mem', 'g'),
    ]

    # read data from file
    with open(filename, 'r') as f:
        line = f.readline()
        while True:
            line = f.readline()
            if line.startswith('#'):
                continue
            if line == '' or line is None:
                break
            time, cpu, mem, pcpu, pmem = tuple(line.strip().split(' '))
            x.append(float(time))
            ycpu.append(float(cpu))
            ymem.append(float(mem))
            ypcpu.append(float(pcpu))
            ypmem.append(float(pmem))

    smoothx = [n for n in smooth(x)]
    #xnew = linspace(0.01, 19, 100)

    for y in ys:
        kwargs = {
            'linewidth': 1.0,
            'linestyle': '-',
        #    'marker': '.',
            'color': y[2],
        }
        #f = interp1d(x, y[0], kind='cubic')
        plt.plot(
            smoothx,
            [n for n in smooth(y[0])],
            #xnew,
            #f(xnew),
            label=y[1], **kwargs)

    #plt.axes().get_xaxis().set_ticks(x)
    #plt.axes().get_xaxis().set_ticklabels(x)

    # annotate max and min values
    plt.xlim(0, 20)
    plt.ylim(0, 100)
    plt.grid()
    plt.legend()
    plt.show()


if __name__ == '__main__':
    # parse command line
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-d', dest='datafile', required=False, default='/tmp/profile.log',
        help='the data file to plot')
    parser.add_argument(
        '-s', dest='subtitle', required=False, default='',
        help='a subtitle for the plot')
    args = parser.parse_args()
    plot(args.datafile, args.subtitle)