|
| 1 | +# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Performance benchmark for the jailer.""" |
| 4 | + |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from framework.jailer import DEFAULT_CHROOT_PATH, JailerContext |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.nonci |
| 15 | +@pytest.mark.parametrize("jailers", [1, 100, 300, 500]) |
| 16 | +@pytest.mark.parametrize("mounts", [0, 100, 300, 500]) |
| 17 | +def test_jailer_startup(jailer_time_bin, microvm_factory, jailers, mounts, metrics): |
| 18 | + """ |
| 19 | + Test the overhead of jailer startup without and with bind mounts |
| 20 | + """ |
| 21 | + |
| 22 | + jailer_binary = microvm_factory.jailer_binary_path |
| 23 | + |
| 24 | + # Create bind mount points. The exact location of them |
| 25 | + # does not matter, they just need to exist. |
| 26 | + mounts_paths = "/tmp/mounts" |
| 27 | + os.makedirs(mounts_paths) |
| 28 | + for m in range(mounts): |
| 29 | + mount_path = f"{mounts_paths}/mount{m}" |
| 30 | + os.makedirs(mount_path) |
| 31 | + subprocess.run( |
| 32 | + ["mount", "--bind", f"{mount_path}", f"{mount_path}"], check=True |
| 33 | + ) |
| 34 | + |
| 35 | + metrics.set_dimensions( |
| 36 | + { |
| 37 | + "performance_test": "test_boottime", |
| 38 | + "jailers": jailers, |
| 39 | + "mounts": mounts, |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + # Testing 1 jailer will give 1 data point which is not enough, |
| 44 | + # so do 100 runs in this case. |
| 45 | + if jailers == 1: |
| 46 | + iterations = 100 |
| 47 | + else: |
| 48 | + iterations = 1 |
| 49 | + |
| 50 | + for i in range(iterations): |
| 51 | + processes = [] |
| 52 | + for j in range(jailers): |
| 53 | + jailer = JailerContext( |
| 54 | + jailer_id=f"fakefc{i}{j}", |
| 55 | + exec_file=jailer_time_bin, |
| 56 | + # Don't deamonize to get the stdout |
| 57 | + daemonize=False, |
| 58 | + ) |
| 59 | + jailer.setup() |
| 60 | + |
| 61 | + cmd = [str(jailer_binary), *jailer.construct_param_list()] |
| 62 | + processes.append( |
| 63 | + subprocess.Popen( |
| 64 | + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + for p in processes: |
| 69 | + r = p.communicate()[0] |
| 70 | + e, s = r.split() |
| 71 | + metrics.put_metric( |
| 72 | + "startup", |
| 73 | + int(e) - int(s), |
| 74 | + unit="Microseconds", |
| 75 | + ) |
| 76 | + |
| 77 | + # Cleanup mounts and jailer dirs |
| 78 | + for d in os.listdir(mounts_paths): |
| 79 | + subprocess.run(["umount", f"{mounts_paths}/{d}"], check=True) |
| 80 | + shutil.rmtree(mounts_paths) |
| 81 | + shutil.rmtree(DEFAULT_CHROOT_PATH) |
0 commit comments