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