Untitled

 avatar
unknown
python
a year ago
2.9 kB
6
Indexable
import re
import subprocess
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sys

TIMES = 10
PROCESSES = 1000

ratata = ['\n\tFirst half\nMemory', '\nFirst CPU cycle',
          '\n\tSecond half\nMemory', '\nFirst CPU cycle']


def format_stdout(lines, output=False):
    lines = lines[lines.index('', lines.index(
        '', lines.index('') + 1) + 1) + 1:]

    all_values = list()

    for i in range(4):
        if output:
            print(ratata[i])
        values = list()
        for line in lines[:2]:
            if output:
                print(line)
            for s in re.findall(r"[-+]?\d*\.\d+|\d+", line):
                values.append(float(s))

        all_values.append(values)

        lines = lines[lines.index('', lines.index('') + 1) + 1:]

    return all_values


def read_stdout():
    print("reading")
    lines = list()

    while True:
        try:
            lines.append(input())
        except EOFError:
            break

    format_stdout(lines, output=True)


def run_shit(program, c, i, m):
    FIRST = 3
    SECOND = 0

    mean = list()

    for _ in range(5):
        print(c/TIMES,i/TIMES,m/TIMES)
        result = subprocess.run([f'./{program}', '-c', str(c/TIMES), '-i', str(i/TIMES), '-m', str(
            m/TIMES), '-p', str(PROCESSES)], stdout=subprocess.PIPE).stdout.decode('utf-8')
        mean.append(format_stdout(result.split("\n"))[FIRST][SECOND])

    return np.average(mean)


def run_command():
    simple_mean = list()
    skeleton_mean = list()
    better_mean = list()
    memory_mean = list()
    x = np.array(range(1, TIMES)) / 10

    c = 2 /2
    i = 4 / 2
    m = TIMES / 2

    for m in range(1, TIMES):
        simple_mean.append(run_shit('roundrobin', c, i, m))
        skeleton_mean.append(run_shit('skeleton', c, i, m))
        better_mean.append(run_shit('fast', c, i, m))
        memory_mean.append(run_shit('memory', c, i, m))


    # mpl.use('pgf')
    # mpl.rcParams.update({
    #     'text.usetex': True,
    #     'pgf.texsystem': 'pdflatex',
    #     # Hiermee wordt er niet een specifiek lettertype door matplotlib geladen.
    #     'pgf.rcfonts': False,
    #     'font.family': 'serif',
    #     'font.sans-serif': [],
    #     'font.monospace': [],
    # })
    plt.plot(x, memory_mean, label='Memory efficient')
    # plt.plot(x, skeleton_mean, label='No CPU scheduler')
    plt.plot(x, simple_mean, label="Roundrobin")
    plt.plot(x, better_mean, label='Dynamic')
    plt.xlabel('Weight of memory')
    plt.ylabel('Execution time')

    plt.legend()
    # plt.yscale('log')
    plt.show()
    # plt.savefig('MEMORY.pgf', bbox_inches='tight')


def main():
    if len(sys.argv) > 1:
        read_stdout()
    else:
        run_command()


if __name__ == "__main__":
    try:
        main()
    except ValueError:
        # Compilation failed
        pass
Editor is loading...
Leave a Comment