def total_runtime_plot(target_perc=.75):

    hosts = defaultdict(int)
    total_cpus = 0
    times = defaultdict(int)
    for result in get_results(outcome=1, userid=2):
        r = Result(*result)
        hosts[r.hostname] += 1
        total_cpus += r.N_cpus

        st, et = r.start_time, r.end_time
        d = et - st

        poll_interval = 5 # in minutes
        for t in range(0, int(d.total_seconds())+1, 60*poll_interval):
            timeval = dtfloor(st + datetime.timedelta(seconds=t))
            times[timeval] += 1

    time_values, counts = zip(*times.iteritems())
    counts = pd.Series(counts, index=time_values).sort_index()

    fig, ax = plt.subplots(figsize=(8,4))
    tu_red = '#990033'
    params = {'color': tu_red, 'lw': 2, 'ls': 'solid', 'marker': None}

    x = counts.index.to_pydatetime()
    y = counts
    target_cpus = target_perc * total_cpus

    ax.plot_date(x, y, label="Actual Usage", **params)
    ax.axhline(target_cpus, color='k', linestyle=':', label='Target Usage')
    ax.axhline(total_cpus, color='k', linestyle='--', label='Usage Limit')

    # XAXIS
    ax.xaxis.set_minor_locator(mpldates.HourLocator())
    ax.xaxis.set_major_locator(mpldates.HourLocator(np.arange(0,25,6)))
    ax.xaxis.set_major_formatter(mpldates.DateFormatter('%m-%d-%y: %H:%M'))
    plt.setp(ax.xaxis.get_majorticklabels(), rotation=80)
    ax.set_xlabel('Time', size=12)

    # YAXIS
    ax.set_ylabel('CPU cores in use', size=12)
    ax.tick_params(axis='both', which='major', labelsize=8)

    ax.legend(loc=1, prop={'size':8})

    plt.tight_layout()
    fig.savefig('total_runtime_plot.png', dpi=150)
def host_runtime_plot():
    # GENERATE DATA
    hosts = defaultdict(list)
    min_time = datetime.datetime.now()
    max_time = datetime.datetime.strptime("01-01-1970", "%m-%d-%Y")
    for result in get_results(outcome=1, userid=2):
        r = Result(*result)
        hosts[r.hostname].append((r.start_time, r.end_time))
        if r.start_time < min_time:
            min_time = r.start_time
        if r.end_time > max_time:
            max_time = r.end_time

    total_time = max_time - min_time
    data = np.zeros((len(hosts), int(total_time.total_seconds() / 60)))
    for i, times in enumerate([v for k, v in sorted(hosts.items())]):
        for st, et in times:
            st_ind = int((st - min_time).total_seconds() / 60)
            et_ind = int((et - min_time).total_seconds() / 60)
            for j in range(st_ind, et_ind):
                data[i, j] += 1

    save_data(hosts, data, min_time)

    # PLOTTING
    fig, ax = plt.subplots(figsize=(20, 8))

    N_hosts = len(hosts)
    N_bins = data.shape[1]
    host_tick_size = 100

    # IMSHOW PARAMETERS
    extent = (0, N_bins, 0, N_hosts * host_tick_size)
    cmap = "cubehelix_r"
    imshow_params = {"extent": extent, "alpha": 0.7, "cmap": cmap, "interpolation": "none"}

    ax.imshow(data, **imshow_params)

    # XTICKS
    start_hour = min_time + datetime.timedelta(hours=1)
    start_hour = start_hour.replace(minute=0, second=0, microsecond=0)
    start_index = dt2hr_ind(start_hour, min_time)
    hr_multiple = 6
    xticks = np.arange(start_index, N_bins + 1, 60 * hr_multiple)
    ihours = lambda i: datetime.timedelta(hours=i)
    xticklabels = [
        (start_hour + ihours(i)).strftime("%b %d, %I %p") for i in range(0, hr_multiple * len(xticks), hr_multiple)
    ]
    xlabel = "Time"

    ax.set_xticks(xticks)
    ax.set_xticklabels(xticklabels, rotation=70)
    ax.set_xlabel(xlabel)

    # YTICKS
    yticks = np.arange(N_hosts) * host_tick_size + host_tick_size / 2
    yticklabels = sorted(hosts)
    ylabel = "Host"

    ax.set_yticks(yticks)
    ax.set_yticklabels(yticklabels)
    ax.set_ylabel(ylabel)

    ax.tick_params(axis="both", which="major", labelsize="10")

    # COLORBAR
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="2.5%", pad=0.05)
    colorbar = discrete_colorbar(5, cmap, cax)
    colorbar.set_label("Number of active CPU cores")

    # ADJUST FOR LONG TIMES AT THE BOTTOM
    fig.subplots_adjust(bottom=0.2, top=0.9)

    fig.savefig("host_runtime_plot.png", dpi=150)
Beispiel #3
0
def host_runtime_plot():
    # GENERATE DATA
    hosts = defaultdict(list)
    min_time = datetime.datetime.now()
    max_time = datetime.datetime.strptime('01-01-1970', '%m-%d-%Y')
    for result in get_results(outcome=1, userid=2):
        r = Result(*result)
        hosts[r.hostname].append((r.start_time, r.end_time))
        if r.start_time < min_time: min_time = r.start_time
        if r.end_time > max_time: max_time = r.end_time

    total_time = max_time - min_time
    data = np.zeros((len(hosts), int(total_time.total_seconds()/60)))
    for i, times in enumerate([v for k,v in sorted(hosts.items())]):
        for st, et in times:
            st_ind = int((st - min_time).total_seconds()/60)
            et_ind = int((et - min_time).total_seconds()/60)
            for j in range(st_ind, et_ind):
                data[i, j] += 1

    save_data(hosts, data, min_time)

    # PLOTTING
    fig, ax = plt.subplots(figsize=(20, 8))

    N_hosts = len(hosts)
    N_bins = data.shape[1]
    host_tick_size = 100

    # IMSHOW PARAMETERS
    extent = (0, N_bins, 0, N_hosts*host_tick_size)
    cmap = 'cubehelix_r'
    imshow_params = {'extent': extent, 'alpha': 0.7, 'cmap': cmap,
                     'interpolation': 'none'}

    ax.imshow(data, **imshow_params)

    # XTICKS
    start_hour = min_time + datetime.timedelta(hours=1)
    start_hour = start_hour.replace(minute=0, second=0, microsecond=0)
    start_index = dt2hr_ind(start_hour, min_time)
    hr_multiple = 6
    xticks = np.arange(start_index, N_bins+1, 60*hr_multiple)
    ihours = lambda i: datetime.timedelta(hours=i)
    xticklabels = [(start_hour + ihours(i)).strftime('%b %d, %I %p')
                   for i in range(0, hr_multiple*len(xticks), hr_multiple)]
    xlabel = 'Time'

    ax.set_xticks(xticks)
    ax.set_xticklabels(xticklabels, rotation=70)
    ax.set_xlabel(xlabel)

    # YTICKS
    yticks = np.arange(N_hosts)*host_tick_size + host_tick_size/2
    yticklabels = sorted(hosts)
    ylabel = 'Host'

    ax.set_yticks(yticks)
    ax.set_yticklabels(yticklabels)
    ax.set_ylabel(ylabel)

    ax.tick_params(axis='both', which='major', labelsize='10')

    # COLORBAR
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="2.5%", pad=0.05)
    colorbar = discrete_colorbar(5, cmap, cax)
    colorbar.set_label('Number of active CPU cores')

    # ADJUST FOR LONG TIMES AT THE BOTTOM
    fig.subplots_adjust(bottom=0.2, top=0.9)

    fig.savefig('host_runtime_plot.png', dpi=150)