summaryrefslogtreecommitdiff
path: root/scripts/backends_cpu_usage/plot.py
blob: 4e5083ad2c02a2c74f56120854c68a4e0fca0d04 (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
#!/usr/bin/python


from matplotlib import pyplot as plt
from movingaverage import movingaverage


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


files = [
    ('sqlite', 'b'),
    ('sqlcipher', 'r'),
    ('u1dblite', 'g'),
    ('u1dbcipher', 'm'),
]


# config the plot
plt.xlabel('time (s)')
plt.ylabel('cpu usage (%)')
plt.title('u1db backends CPU usage')


for fi in files:

    backend = fi[0]
    color = fi[1]
    filename = '%s.txt' % backend 

    x = []
    y = []

    xmax = None
    xmin = None
    ymax = None
    ymin = None

    # read data from file
    with open(filename, 'r') as f:
        line = f.readline()
        while line is not None:
            time, cpu = tuple(line.strip().split(' '))
            cpu = float(cpu)
            x.append(float(time))
            y.append(cpu)
            if ymax == None or cpu > ymax:
                ymax = cpu
                xmax = time
            if ymin == None or cpu < ymin:
                ymin = cpu
                xmin = time
            line = f.readline()
            if line == '':
                break

    kwargs = {
        'linewidth': 1.0,
        'linestyle': '-',
    #    'marker': '.',
        'color': color,
    }
    plt.plot(
        [n for n in smooth(x)],
        [n for n in smooth(y)],
        label=backend, **kwargs)

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

    # annotate max and min values
    #plt.axes().annotate("%.2f GB" % ymax, xy=(xmax, ymax))
    #plt.axes().annotate("%.2f GB" % ymin, xy=(xmin, ymin))


plt.ylim(0, 100)
plt.grid()
plt.legend()
plt.show()