simple_plotter.py 2.76 KB
Newer Older
Mario Hock's avatar
Mario Hock committed
1
2
3
4
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys
5
import matplotlib
Mario Hock's avatar
Mario Hock committed
6
import matplotlib.pyplot as plt
Mario Hock's avatar
Mario Hock committed
7

Mario Hock's avatar
Mario Hock committed
8
from cnl_library import CNLParser, calc_ema
Mario Hock's avatar
Mario Hock committed
9
10


Mario Hock's avatar
Mario Hock committed
11
12
13
14
15
16
17
18
19
20
21
def append_twice(base_list, extend_list):
    if ( isinstance(extend_list, list) ):
        for x in extend_list:
            base_list.append(x)
            base_list.append(x)
    else:
        base_list.append(extend_list)
        base_list.append(extend_list)



Mario Hock's avatar
Mario Hock committed
22
23


Mario Hock's avatar
Mario Hock committed
24
def parse_cnl_file(filename):
Mario Hock's avatar
Mario Hock committed
25
26
27
28
29
30
    ## * Parse input file. *
    cnl_file = CNLParser(filename)


    ## Display header informations.
    print( cnl_file.get_type() )
Mario Hock's avatar
Mario Hock committed
31
    print( cnl_file.get_comment() )
Mario Hock's avatar
Mario Hock committed
32
33
34
35
36

    print( "CPUs: " + str(cnl_file.get_cpus()) )
    print( "NICs: " + str(cnl_file.get_nics()) )


37
    ## Prepare data for matplotlib
Mario Hock's avatar
Mario Hock committed
38

Mario Hock's avatar
Mario Hock committed
39
    #nics = cnl_file.get_nics()
Mario Hock's avatar
Mario Hock committed
40
    nics = ("eth1", "eth2")  ## XXX
41
42
    net_cols = list()
    nic_fields = [".send", ".receive"]
Mario Hock's avatar
Mario Hock committed
43
    for nic_name in nics:
44
45
        for nic_field in nic_fields:
            net_cols.append( nic_name + nic_field )
Mario Hock's avatar
Mario Hock committed
46

47
48
49
50
    cpu_cols = [ cpu_name + ".util" for cpu_name in cnl_file.get_cpus() ]

    cols = cnl_file.get_csv_columns()
    x_values = cols["end"]
Mario Hock's avatar
Mario Hock committed
51
    #print( cols )   ## XXX
Mario Hock's avatar
Mario Hock committed
52
53


Mario Hock's avatar
Mario Hock committed
54
55
56
57
58
    ## Augment cnl_file with processed data.
    cnl_file.cols = cols
    cnl_file.net_col_names = net_cols
    cnl_file.cpu_col_names = cpu_cols
    cnl_file.x_values = x_values
Mario Hock's avatar
Mario Hock committed
59

Mario Hock's avatar
Mario Hock committed
60
    return cnl_file
Mario Hock's avatar
Mario Hock committed
61

Mario Hock's avatar
Mario Hock committed
62

Mario Hock's avatar
Mario Hock committed
63
64


Mario Hock's avatar
Mario Hock committed
65
66
67
def plot_net(ax, x_values, cols, active_cols):
    ax.set_ylim(0,10**10)
    ax.set_ylabel('Throughput (Bit/s)')
68

Mario Hock's avatar
Mario Hock committed
69
70
71
    for col_name in active_cols:
        ax.plot(x_values , cols[col_name], label=col_name)
        ax.plot(x_values , calc_ema(cols[col_name], 0.2), label=col_name+" (ema)")
72

Mario Hock's avatar
Mario Hock committed
73
    ax.legend()
Mario Hock's avatar
Mario Hock committed
74

75

Mario Hock's avatar
Mario Hock committed
76
77
78
def plot_cpu(ax, x_values, cols, active_cols):
    ax.set_ylim(0,100)
    ax.set_ylabel('CPU util (%)')
Mario Hock's avatar
Mario Hock committed
79

Mario Hock's avatar
Mario Hock committed
80
81
82
    for col_name in active_cols:
        ax.plot(x_values , cols[col_name], label=col_name)
        #ax2.plot(x_values , calc_ema(cols[col_name], 0.2), label=col_name+" (ema)")
Mario Hock's avatar
Mario Hock committed
83

Mario Hock's avatar
Mario Hock committed
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
    ax.legend()




## MAIN ##
if __name__ == "__main__":

    ### DEMO:

    filename = sys.argv[1]
    print( filename )

    cnl_file = parse_cnl_file(filename)

    ## Plot with matplotlib.

    ## Create figure (window/file)
    fig = plt.figure()
    fig.canvas.set_window_title('CPUnetPlot')

    ## Draw comment on the figure (use absolute positioning).
    t = matplotlib.text.Text(10,10, "Comment: " + cnl_file.get_comment(), figure=fig)
    fig.texts.append(t)


    ## Prepare subplots
    ax_net = fig.add_subplot(211)
112
    ax_cpu = fig.add_subplot(212, sharex=ax_net)
Mario Hock's avatar
Mario Hock committed
113
114
    #ax_net = fig.add_subplot(111)  ## twin
    #ax_cpu = ax_net.twinx()      ## twin
Mario Hock's avatar
Mario Hock committed
115

116

Mario Hock's avatar
Mario Hock committed
117
118
119
    ## Plot
    plot_net(ax_net, cnl_file.x_values, cnl_file.cols, cnl_file.net_col_names)
    plot_cpu(ax_cpu, cnl_file.x_values, cnl_file.cols, cnl_file.cpu_col_names )
Mario Hock's avatar
Mario Hock committed
120
121

    plt.show()