Coverage for mlos_bench/mlos_bench/tests/environments/include_tunables_test.py: 100%
42 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Test the selection of tunables / tunable groups for the environment."""
7from mlos_bench.environments.mock_env import MockEnv
8from mlos_bench.services.config_persistence import ConfigPersistenceService
9from mlos_bench.tunables.tunable_groups import TunableGroups
12def test_one_group(tunable_groups: TunableGroups) -> None:
13 """Make sure only one tunable group is available to the environment."""
14 env = MockEnv(
15 name="Test Env",
16 config={"tunable_params": ["provision"]},
17 tunables=tunable_groups,
18 )
19 assert env.tunable_params.get_param_values() == {
20 "vmSize": "Standard_B4ms",
21 }
24def test_two_groups(tunable_groups: TunableGroups) -> None:
25 """Make sure only the selected tunable groups are available to the environment."""
26 env = MockEnv(
27 name="Test Env",
28 config={"tunable_params": ["provision", "kernel"]},
29 tunables=tunable_groups,
30 )
31 assert env.tunable_params.get_param_values() == {
32 "vmSize": "Standard_B4ms",
33 "kernel_sched_migration_cost_ns": -1,
34 "kernel_sched_latency_ns": 2000000,
35 }
38def test_two_groups_setup(tunable_groups: TunableGroups) -> None:
39 """Make sure only the selected tunable groups are available to the environment, the
40 set is not changed after calling the `.setup()` method.
41 """
42 env = MockEnv(
43 name="Test Env",
44 config={
45 "tunable_params": ["provision", "kernel"],
46 "const_args": {
47 "const_param1": 10,
48 "const_param2": "foo",
49 },
50 },
51 tunables=tunable_groups,
52 )
53 expected_params = {
54 "vmSize": "Standard_B4ms",
55 "kernel_sched_migration_cost_ns": -1,
56 "kernel_sched_latency_ns": 2000000,
57 }
58 assert env.tunable_params.get_param_values() == expected_params
60 with env as env_context:
61 assert env_context.setup(tunable_groups)
63 # Make sure the set of tunables does not change after the setup:
64 assert env.tunable_params.get_param_values() == expected_params
65 assert env.parameters == {
66 **expected_params,
67 "const_param1": 10,
68 "const_param2": "foo",
69 }
72def test_zero_groups_implicit(tunable_groups: TunableGroups) -> None:
73 """Make sure that no tunable groups are available to the environment by default."""
74 env = MockEnv(name="Test Env", config={}, tunables=tunable_groups)
75 assert env.tunable_params.get_param_values() == {}
78def test_zero_groups_explicit(tunable_groups: TunableGroups) -> None:
79 """Make sure that no tunable groups are available to the environment when explicitly
80 specifying an empty list of tunable_params.
81 """
82 env = MockEnv(name="Test Env", config={"tunable_params": []}, tunables=tunable_groups)
83 assert env.tunable_params.get_param_values() == {}
86def test_zero_groups_implicit_setup(tunable_groups: TunableGroups) -> None:
87 """Make sure that no tunable groups are available to the environment by default and
88 it does not change after the setup.
89 """
90 env = MockEnv(
91 name="Test Env",
92 config={
93 "const_args": {
94 "const_param1": 10,
95 "const_param2": "foo",
96 },
97 },
98 tunables=tunable_groups,
99 )
100 assert env.tunable_params.get_param_values() == {}
102 with env as env_context:
103 assert env_context.setup(tunable_groups)
105 # Make sure the set of tunables does not change after the setup:
106 assert env.tunable_params.get_param_values() == {}
107 assert env.parameters == {
108 "const_param1": 10,
109 "const_param2": "foo",
110 }
113def test_loader_level_include() -> None:
114 """Make sure only the selected tunable groups are available to the environment, the
115 set is not changed after calling the `.setup()` method.
116 """
117 env_json = {
118 "class": "mlos_bench.environments.mock_env.MockEnv",
119 "name": "Test Env",
120 "include_tunables": ["environments/os/linux/boot/linux-boot-tunables.jsonc"],
121 "config": {
122 "tunable_params": ["linux-kernel-boot"],
123 "const_args": {
124 "const_param1": 10,
125 "const_param2": "foo",
126 },
127 },
128 }
129 loader = ConfigPersistenceService(
130 {
131 "config_path": [
132 "mlos_bench/config",
133 "mlos_bench/examples",
134 ]
135 }
136 )
137 env = loader.build_environment(config=env_json, tunables=TunableGroups())
138 expected_params = {
139 "align_va_addr": "on",
140 "idle": "halt",
141 "ima.ahash_bufsize": 4096,
142 "noautogroup": "",
143 "nohugevmalloc": "",
144 "nohalt": "",
145 "nohz": "",
146 "no-kvmapf": "",
147 "nopvspin": "",
148 }
149 assert env.tunable_params.get_param_values() == expected_params
151 expected_params["align_va_addr"] = "off"
152 tunables = env.tunable_params.copy().assign({"align_va_addr": "off"})
154 with env as env_context:
155 assert env_context.setup(tunables)
157 # Make sure the set of tunables does not change after the setup:
158 assert env.parameters == {
159 **expected_params,
160 "const_param1": 10,
161 "const_param2": "foo",
162 }
163 assert env.tunable_params.get_param_values() == expected_params