From 7434bc8945fcf323b0735c6b49c312925b644815 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 2 Sep 2016 14:29:48 +0200 Subject: [PATCH 1/6] Add benchmarking project: Botan --- benchmark/benchmark_project.py | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 benchmark/benchmark_project.py diff --git a/benchmark/benchmark_project.py b/benchmark/benchmark_project.py new file mode 100644 index 00000000..e16fb3b2 --- /dev/null +++ b/benchmark/benchmark_project.py @@ -0,0 +1,99 @@ +from contextlib import contextmanager +import multiprocessing +import os +import subprocess +import tempfile +import time +import urllib.request +import shutil +import sys +import zipfile + +PYTHON_BINARY = sys.executable +SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) +CLCACHE_SCRIPT = os.path.join(SCRIPT_DIR, "..", "clcache.py") +CLCACHE_CMD = [PYTHON_BINARY, CLCACHE_SCRIPT] + +PROJECT_URL = "https://github.com/randombit/botan/archive/1.11.31.zip" +PROJECT_ZIP_FILENAME = "botan.zip" + +WORKING_DIR = os.getcwd() +JOBS = multiprocessing.cpu_count() + +def ensure_downloaded(url, localfile): + """ + Download the given url and save it at localfile. If the local file already exists, do nothing. + :param url: + :param localfile: + :return: + """ + if not os.path.exists(localfile): + tmppath = localfile + ".part" + urllib.request.urlretrieve(url, filename=tmppath) + os.rename(tmppath, localfile) + +def extract(archive_path, root_path): + """ + Never extract archives from untrusted sources without prior inspection using this method. + """ + archive_path = os.path.abspath(archive_path) + root_path = os.path.abspath(root_path) + with cd(root_path): + with zipfile.ZipFile(archive_path, "r") as unzip: + root_folder = unzip.namelist()[0] + unzip.extractall() + return root_folder + +@contextmanager +def cd(targetDirectory): + oldDirectory = os.getcwd() + os.chdir(os.path.expanduser(targetDirectory)) + try: + yield + finally: + os.chdir(oldDirectory) + +@contextmanager +def benchmark(title): + start = time.time() + try: + yield + finally: + end = time.time() + s = end - start + print('Runtime {}: {}s'.format(title, round(s))) + +def reset(log): + print("Removing build dir ...", file=log) + shutil.rmtree("build") + + print("Configuring Botan ...", file=log) + subprocess.check_call([ + PYTHON_BINARY, + "configure.py", + "--cc-bin=clcache.bat", + "--no-autoload", + "--enable-modules=tls,auto_rng", # add auto_rng because of bug https://github.com/randombit/botan/issues/615 + ], stdout=log) + +if __name__ == '__main__': + print("Running {} parallel jobs".format(JOBS)) + + with open("log.txt", "w") as logfile: + with tempfile.TemporaryDirectory(prefix=os.path.join(SCRIPT_DIR, "botan_")) as tmpDir, cd(tmpDir): + ensure_downloaded(PROJECT_URL, os.path.join(WORKING_DIR, PROJECT_ZIP_FILENAME)) + root_folder_name = extract(os.path.join(WORKING_DIR, PROJECT_ZIP_FILENAME), ".") + + with cd(root_folder_name): + + #time.sleep(30) + + reset(logfile) + with benchmark("cold cache"): + subprocess.check_call(["jom", "/nologo", "-j{}".format(JOBS)], stdout=logfile) + + for i in range(3): + number = i+1 + reset(logfile) + with benchmark("hot cache " + str(number)): + subprocess.check_call(["jom", "/nologo", "-j{}".format(JOBS)], stdout=logfile) From e891aeb047b7d66f0fdc91213b06e610cf8cf2b0 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 2 Sep 2016 15:12:03 +0200 Subject: [PATCH 2/6] Check build dir existence in reset() --- benchmark/benchmark_project.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/benchmark/benchmark_project.py b/benchmark/benchmark_project.py index e16fb3b2..5c94c1ed 100644 --- a/benchmark/benchmark_project.py +++ b/benchmark/benchmark_project.py @@ -64,8 +64,9 @@ def benchmark(title): print('Runtime {}: {}s'.format(title, round(s))) def reset(log): - print("Removing build dir ...", file=log) - shutil.rmtree("build") + if os.path.exists("build"): + print("Removing build dir ...", file=log) + shutil.rmtree("build") print("Configuring Botan ...", file=log) subprocess.check_call([ From c6aeefdf4136c0c72c47920897cac45fb64d2d4b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 2 Sep 2016 16:37:54 +0200 Subject: [PATCH 3/6] Don't show benchmark results in case of an exception --- benchmark/benchmark_project.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/benchmark/benchmark_project.py b/benchmark/benchmark_project.py index 5c94c1ed..19eec3b1 100644 --- a/benchmark/benchmark_project.py +++ b/benchmark/benchmark_project.py @@ -56,12 +56,13 @@ def cd(targetDirectory): @contextmanager def benchmark(title): start = time.time() - try: - yield - finally: - end = time.time() - s = end - start - print('Runtime {}: {}s'.format(title, round(s))) + + yield # Execute code block in 'with'. If an exception occurs, we stop here. + + end = time.time() + s = end - start + print('Runtime {}: {}s'.format(title, round(s))) + def reset(log): if os.path.exists("build"): From 819a63ce004a10e6ffd6969fdf6ae526bb6b26bb Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 2 Sep 2016 17:10:55 +0200 Subject: [PATCH 4/6] The $HOME equivalent in Windows is calles %USERPROFILE% --- README.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.asciidoc b/README.asciidoc index 7d56a104..8dd88d55 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -62,7 +62,7 @@ Alternative installation via clcache.bat This method is especially handy when there are multiple Python installations on a system. -Create a clcache.bat and put it in a directory in PATH (e.g. %HOME%\bin): +Create a clcache.bat and put it in a directory in PATH (e.g. %USERPROFILE%\bin): @echo off @setlocal From 4f4c59602641adbbd4a3770d5fbd32b245bd9bad Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 2 Sep 2016 19:16:49 +0200 Subject: [PATCH 5/6] Fix logging --- benchmark/benchmark_project.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/benchmark/benchmark_project.py b/benchmark/benchmark_project.py index 19eec3b1..7722ddd3 100644 --- a/benchmark/benchmark_project.py +++ b/benchmark/benchmark_project.py @@ -66,10 +66,12 @@ def benchmark(title): def reset(log): if os.path.exists("build"): - print("Removing build dir ...", file=log) + log.write("Removing build dir ...\n") + log.flush() shutil.rmtree("build") - print("Configuring Botan ...", file=log) + log.write("Configuring Botan ...\n") + log.flush() subprocess.check_call([ PYTHON_BINARY, "configure.py", From b6601b9fd3a4bb420df5d0d561c37bc3b480e630 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 2 Sep 2016 19:21:25 +0200 Subject: [PATCH 6/6] Use constant tmp dir name --- benchmark/benchmark_project.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/benchmark/benchmark_project.py b/benchmark/benchmark_project.py index 7722ddd3..96a6d4b3 100644 --- a/benchmark/benchmark_project.py +++ b/benchmark/benchmark_project.py @@ -2,7 +2,6 @@ import multiprocessing import os import subprocess -import tempfile import time import urllib.request import shutil @@ -17,6 +16,8 @@ PROJECT_URL = "https://github.com/randombit/botan/archive/1.11.31.zip" PROJECT_ZIP_FILENAME = "botan.zip" +TMP_DIR = os.path.join(SCRIPT_DIR, "botan_tmp") + WORKING_DIR = os.getcwd() JOBS = multiprocessing.cpu_count() @@ -80,11 +81,16 @@ def reset(log): "--enable-modules=tls,auto_rng", # add auto_rng because of bug https://github.com/randombit/botan/issues/615 ], stdout=log) + if __name__ == '__main__': print("Running {} parallel jobs".format(JOBS)) with open("log.txt", "w") as logfile: - with tempfile.TemporaryDirectory(prefix=os.path.join(SCRIPT_DIR, "botan_")) as tmpDir, cd(tmpDir): + if os.path.exists(TMP_DIR): + shutil.rmtree(TMP_DIR) + os.mkdir(TMP_DIR) + + with cd(TMP_DIR): ensure_downloaded(PROJECT_URL, os.path.join(WORKING_DIR, PROJECT_ZIP_FILENAME)) root_folder_name = extract(os.path.join(WORKING_DIR, PROJECT_ZIP_FILENAME), ".")