"""Build the isolated mixed-workload client before the timed measurement starts.""" from pathlib import Path import re from common import REPO, command, digest, read_json, write_json def compiled_entry(path: Path) -> bool: # Same module depth as backend/src/tools, including for the fixture guard. entry = path.resolve() return (entry.name == 'soakDatabaseLookups.js' and entry.parent.name == 'tools' and entry.parent.parent.parent == REPO / 'backend' and re.fullmatch(r'\.soak-client-[a-z0-9_]+', entry.parent.parent.name) is not None) def compile_client(destination: Path, run: Path) -> Path: backend = REPO / 'backend' entry = destination / 'tools/soakDatabaseLookups.js' if not compiled_entry(entry): raise ValueError('owned temporary mixed-client output required') config = destination / 'tsconfig.json' write_json(config, {'extends': '../tsconfig.json', 'compilerOptions': {'outDir': '.', 'declaration': False, 'sourceMap': False, 'noEmitOnError': True}, 'include': ['../src/tools/soakDatabaseLookups.ts'], 'exclude': []}) with (run / 'mixed-client-build.log').open('xb') as output: command([str(backend / 'node_modules/.bin/tsc'), '-p', str(config)], output_stream=output, merge_stderr=True, timeout=180) if not entry.is_file(): raise RuntimeError('mixed-client compiler did not emit its entrypoint') files = output_digests(destination) write_json(run / 'mixed-client-build.json', {'entrypoint': 'tools/soakDatabaseLookups.js', 'executableSha256': files['tools/soakDatabaseLookups.js'], 'files': files, 'sourceSha256': digest(backend / 'src/tools/soakDatabaseLookups.ts'), 'lockSha256': digest(backend / 'package-lock.json')}) return entry def output_digests(directory: Path): return {str(file.relative_to(directory)): digest(file) for file in sorted(directory.rglob('*')) if file.is_file() and file != directory / 'tsconfig.json'} def verify_client(entry: Path, run: Path): if not compiled_entry(entry) or not entry.is_file(): raise ValueError('mixed soak requires its precompiled invocation-specific entrypoint') receipt = read_json(run / 'mixed-client-build.json') if (receipt.get('files') != output_digests(entry.parent.parent) or receipt.get('executableSha256') != digest(entry) or receipt.get('sourceSha256') != digest(REPO / 'backend/src/tools/soakDatabaseLookups.ts') or receipt.get('lockSha256') != digest(REPO / 'backend/package-lock.json')): raise ValueError('mixed soak compiler evidence differs from the executable or sources') return receipt['executableSha256']