plotBokeh.py 1.94 KB
Newer Older
ZHOU Shihang's avatar
ZHOU Shihang committed
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
import datetime

import numpy as np
from bokeh.models import NumeralTickFormatter
from bokeh.plotting import figure


def plot_histo(data, item_name):
    if item_name == "JobPeakCPUPercent":
        datapoints = list(map(lambda x: x / 100, data))
    else:
        datapoints = data
    hist, edges = np.histogram(datapoints, density=False, bins="auto")
    p = figure(title="resource consumption statistics of " + item_name, tools='', background_fill_color="#fafafa")
    mean = np.mean(datapoints)
    # var = round(np.var(datapoints), 2)
    if item_name == "JobPeakCPUPercent":
        axis_unit = "100 %"
        maximum = str(round(edges[-1] * 100, 2)) + "%"
        mean_display = str(round(mean * 100, 2)) + "%"
    elif item_name == "JobCPUTime" or item_name == "JobClockTime":
        axis_unit = "00:00:00"
        maximum = str(datetime.timedelta(seconds=round(edges[-1])))
        mean_display = str(datetime.timedelta(seconds=round(mean)))
    else:
        axis_unit = "0.000 b"
        maximum = humanbytes(edges[-1])
        mean_display = humanbytes(mean)
    p.xaxis.formatter = NumeralTickFormatter(format=axis_unit)
    p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
           fill_color="navy", line_color="white", alpha=0.5)
    p.xaxis.axis_label = item_name + ', max : ' + maximum + ", mean: " + mean_display  # + ", var: " + str(var)
    p.yaxis.axis_label = 'count'
    p.grid.grid_line_color = "white"
    return p


def humanbytes(B):
    B = float(B)
    KB = float(1024)
    MB = float(KB ** 2)  # 1,048,576
    GB = float(KB ** 3)  # 1,073,741,824
    TB = float(KB ** 4)  # 1,099,511,627,776
    if B < KB:
        return '{0} {1}'.format(B, 'Bytes' if 0 == B > 1 else 'Byte')
    elif KB <= B < MB:
        return '{0:.2f} KiB'.format(B / KB)
    elif MB <= B < GB:
        return '{0:.2f} MiB'.format(B / MB)
    elif GB <= B < TB:
        return '{0:.2f} GiB'.format(B / GB)
    elif TB <= B:
        return '{0:.2f} TiB'.format(B / TB)