def create_testdata(PE, minimum, maximum, step=15): fun = define_function(PE) steplength = max((maximum - minimum) // step, 1) if steplength == 1: step = maximum - minimum current = minimum - steplength step_pad = len(str(maximum)) ans_pad = len(str(fun(maximum))) path = get_PE_dir(PE) test_path = path + '/Tests' if os.path.exists(test_path): nxt_num = get_next_testnumber(PE, test_path) else: nxt_num = 1 os.makedirs(test_path) file_2_write = '{}/Tests/PE_{:0>3}_test_{:0>2}.txt'.format( path, PE, nxt_num) fil = open(file_2_write, "w") for i in range(step + 1): current += steplength line = '{}: {number:{width}d} : {number_2:{width_2}d}'.format( i + 1, width=step_pad, number=current, width_2=ans_pad, number_2=fun(current)) print(line) fil.write(line + "\n") fil.close()
def benchmark_python_testfile(PE, testfile): print(PE, testfile) cwd = get_PE_dir(PE) PE_input = read_test_data(PE, testfile)[0] file_dir = cwd + '/Python' sys.path.insert(0, file_dir) for f in os.listdir(file_dir): if not f.endswith('.py'): continue file_no_ending = f[:-3] print(f) filename_2_write = cwd + '/Benchmarks/' + testfile[:-4] + f[6:] + '.txt' file = open(filename_2_write, "w") for index, value in enumerate(PE_input): filename = file_no_ending + '(' + value + ')' setup = 'from {} import {}'.format(file_no_ending, file_no_ending) time_taken = avg_run_file(filename, setup, number_of_runs=0) if time_taken == 0: break file.write(str(index) + " " + str(time_taken) + "\n") print(time_taken, value, file_no_ending) file.close()
def create_markdown(PE, create_all=False): path = get_PE_dir(PE) markdown_folderpath = path + "/Markdown" if not os.path.exists(markdown_folderpath): os.makedirs(markdown_folderpath) create_all = True if create_all: create_overview_markdown(PE, path) create_answer_markdown(PE, path) for language in sorted(get_languages(PE)): create_language_markdown(PE, path, language) PE_name = 'PE_{:0>3}'.format(PE) first_three_files = [ '{}/PE_{:0>3}_{}.md'.format(markdown_folderpath, PE, string) for string in ['problem', 'answer', 'overview'] ] readme = path + '/README.md' if os.path.exists(readme): os.remove(readme) for filename in first_three_files: os.system('cat {} >> {}'.format(filename, readme)) for files in sorted(os.listdir(markdown_folderpath)): filepath = markdown_folderpath + '/' + files if filepath in first_three_files: continue print(filepath) os.system('cat {} >> {}'.format(filepath, readme))
def is_solved(PE, language): filename = 'PE_{:0>3}'.format(PE) f = define_function(PE) path = get_PE_dir(PE) answer_fil = open(path + "/Tests/" + filename + "_test_01.txt") _, arguments, answer = get_2_args_and_ans(answer_fil.readlines(0)[0]) if language.lower() == 'python': return f(*arguments) == answer
def read_test_data(PE, filename): path = get_PE_dir(PE) + '/Tests/' + filename PE_input, result, labels = list(), list(), list() with open(path) as f: for line in f: line_list = line.strip().split(':') PE_input.append(line_list[1]) result.append(line_list[2]) labels.append(line_list[0]) return (PE_input, result, labels)
def unit_tests_python(PE, language, function=None, unit_test=2): path = get_PE_dir(PE) + '/Tests/' testname = "PE_{:0>3}_test_{:0>2}.txt".format(PE, unit_test) try: function() except: f = define_function(PE) unit_test_results = [] for line in open(path + testname).read().splitlines(): args, ans = get_args_and_ans(line) is_correct, is_timeout = is_solved_within_x(f, args, ans) if is_timeout: break unit_test_results.append(is_correct) return unit_test_results
def benchmark_python(PE, testfile='all'): PE_dir = get_PE_dir(PE) cwd = PE_dir + '/Tests/' testfile_lst = [] if testfile == 'all': for f in os.listdir(cwd): testfile_lst.append(f) else: testfile_lst.append(testfile) if not testfile: return if not os.path.exists(PE_dir + '/Benchmarks'): os.makedirs(PE_dir + '/Benchmarks') for testfil in testfile_lst: benchmark_python_testfile(PE, testfil)
def benchmark_plot(PE, testname, language="all"): cwd = get_PE_dir(PE) + '/Benchmarks' labels = read_test_data(PE, testname)[2] if language in ["all", True]: language_name = "" else: language_name = "- " + language.capitalize() plt.figure() for fname in os.listdir(cwd): if not os.path.getsize(cwd + '/' + fname) > 0: continue if not is_benchmark_datafile(fname, testname, language): continue data = np.loadtxt(cwd + '/' + fname) X = data[:, 0] Y = data[:, 1] labelname = str(PE) + fname.replace("_", " ")[14:-4] if language not in ["all", True]: plt.plot( X, Y, label=labelname.replace("." + LANGUAGES[language], " ")) else: plt.plot(X, Y, label=labelname) if not labels: pass else: plt.xticks(X, labels) plt.legend() plt.xlabel('Input') plt.ylabel('Time') plt.title('Project Euler {} - Benchmark {} {}'.format( PE, testname[-6:-4].replace("0", ""), language_name)) tikz_save('test.tex') plt.clf() plt.close() os.system('pdflatex standalonefile.tex') destination = os.path.dirname(os.getcwd()) + '/Raport/Benchmarks' new_name = testname[:-4] + '.tex' print('===') print(os.getcwd() + '/test.tex') print(destination + '/' + new_name) print('===') os.rename(os.getcwd() + '/test.tex', destination + '/' + new_name) image_name = testname[:-4] if language not in ["all", True]: image_name += "_" + language image_name += ".png" os.system('convert -density 200 standalonefile.pdf -quality 100 ' + image_name) destination = get_PE_dir(PE) + '/Images' if not os.path.exists(destination): os.makedirs(destination) os.rename(os.getcwd() + '/' + image_name, destination + '/' + image_name)
def benchmark_plot_all(PE, language="all"): language = language.lower() cwd = get_PE_dir(PE) + '/Tests' for testfile in os.listdir(cwd): benchmark_plot(PE, testfile, language)