|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | +A py.test test that attempts to build openssl and benchmark the effect of clcache |
| 4 | +""" |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import pytest |
| 8 | +import zipfile |
| 9 | +import subprocess |
| 10 | + |
| 11 | +from utils import block_message, retry_delete, common_module_setup, check_program, get_vc_envs, download_file |
| 12 | +from utils import DISTDIR, RETAIN_CACHE, EMPTY_CACHE |
| 13 | + |
| 14 | +from fixtures import clcache_envs |
| 15 | + |
| 16 | +OPENSSL_ZIP = "OpenSSL_1_0_2-stable.zip" |
| 17 | +OPENSSL_URL = "https://codeload.github.com/openssl/openssl/zip/OpenSSL_1_0_2-stable" |
| 18 | +SOURCES = None |
| 19 | + |
| 20 | + |
| 21 | +def clean_build(): |
| 22 | + """ |
| 23 | + Unpack the openssl source, possibly deleting the previous one |
| 24 | + :return: |
| 25 | + """ |
| 26 | + with zipfile.ZipFile(OPENSSL_ZIP, "r") as unzip: |
| 27 | + folder = unzip.namelist()[0] |
| 28 | + if os.path.exists(folder): |
| 29 | + with block_message("delete old openssl folder"): |
| 30 | + retry_delete(folder) |
| 31 | + |
| 32 | + with block_message("unzip openssl"): |
| 33 | + unzip.extractall() |
| 34 | + |
| 35 | + global SOURCES |
| 36 | + SOURCES = folder.rstrip("/") |
| 37 | + |
| 38 | + |
| 39 | +def configure_openssl(envs): |
| 40 | + """ |
| 41 | + Run the configure steps (requires perl) |
| 42 | + :param envs: |
| 43 | + :return: |
| 44 | + """ |
| 45 | + check_program(envs, "nmake.exe") |
| 46 | + check_program(envs, "perl.exe") |
| 47 | + |
| 48 | + with block_message("configure openssl"): |
| 49 | + subprocess.check_call(["perl", |
| 50 | + "Configure", "VC-WIN32", "no-asm", "--prefix=c:\openssl"], |
| 51 | + env=envs, |
| 52 | + cwd=SOURCES) |
| 53 | + |
| 54 | + with block_message("generate makefiles"): |
| 55 | + subprocess.check_call([os.path.join("ms", "do_ms.bat")], |
| 56 | + shell=True, |
| 57 | + env=envs, |
| 58 | + cwd=SOURCES) |
| 59 | + |
| 60 | + |
| 61 | +def setup_function(request): |
| 62 | + """ |
| 63 | + Ensure a clean build tree before each test |
| 64 | + :return: |
| 65 | + """ |
| 66 | + os.chdir(DISTDIR) |
| 67 | + clean_build() |
| 68 | + configure_openssl(get_vc_envs()) |
| 69 | + |
| 70 | + |
| 71 | +def setup_module(): |
| 72 | + """ |
| 73 | + Check that our exe has been built. |
| 74 | + :return: |
| 75 | + """ |
| 76 | + common_module_setup() |
| 77 | + download_file(OPENSSL_URL, OPENSSL_ZIP) |
| 78 | + |
| 79 | + |
| 80 | +def replace_wipe_cflags(filename): |
| 81 | + """ |
| 82 | + Open the nmake file given and turn off PDB generation for .obj files |
| 83 | + :param filename: |
| 84 | + :return: |
| 85 | + """ |
| 86 | + lines = [] |
| 87 | + with open(filename, "rb") as makefile: |
| 88 | + for line in makefile.readlines(): |
| 89 | + if line.startswith("APP_CFLAG="): |
| 90 | + lines.append("APP_CFLAG=") |
| 91 | + elif line.startswith("LIB_CFLAG="): |
| 92 | + lines.append("LIB_CFLAG=/Zl") |
| 93 | + else: |
| 94 | + lines.append(line.rstrip()) |
| 95 | + |
| 96 | + with open(filename, "wb") as makefile: |
| 97 | + for line in lines: |
| 98 | + makefile.write(line + "\r\n") |
| 99 | + |
| 100 | + |
| 101 | +def build_openssl(addpath=None, envs=get_vc_envs(), pdbs=False): |
| 102 | + """ |
| 103 | + Build openssl, optionally prefixing addpath to $PATH |
| 104 | + :param addpath: |
| 105 | + :param envs: env var dict to use |
| 106 | + :param pdbs: if False, turn off pdb generation in the makefile |
| 107 | + :return: |
| 108 | + """ |
| 109 | + nmakefile = os.path.join("ms", "nt.mak") |
| 110 | + if not pdbs: |
| 111 | + replace_wipe_cflags(os.path.join(SOURCES, nmakefile)) |
| 112 | + |
| 113 | + if addpath is not None: |
| 114 | + envs["PATH"] = addpath + os.pathsep + envs["PATH"] |
| 115 | + |
| 116 | + try: |
| 117 | + with block_message("running nmake"): |
| 118 | + subprocess.check_output(["nmake", "-f", nmakefile], |
| 119 | + shell=True, |
| 120 | + env=envs, |
| 121 | + cwd=SOURCES) |
| 122 | + except subprocess.CalledProcessError as cpe: |
| 123 | + print cpe.output |
| 124 | + raise |
| 125 | + |
| 126 | + |
| 127 | +def test_build_nocache(): |
| 128 | + """ |
| 129 | + Time an openssl build with no caching involved at all |
| 130 | + :return: |
| 131 | + """ |
| 132 | + build_openssl() |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize("cache_setting", [EMPTY_CACHE, RETAIN_CACHE]) |
| 136 | +def test_build_withclcache(clcache_envs, cache_setting): |
| 137 | + """ |
| 138 | + Time an openssl build with a cold cache |
| 139 | + :param clcache_envs: clcache environment vars fixture |
| 140 | + :param cache_setting: if True, delete the cache from disk |
| 141 | + :return: |
| 142 | + """ |
| 143 | + if cache_setting == EMPTY_CACHE: |
| 144 | + retry_delete(clcache_envs["CLCACHE_DIR"]) |
| 145 | + build_openssl(DISTDIR, clcache_envs) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + pytest.main(sys.argv[1:]) |
0 commit comments