Coverage for mlos_bench/mlos_bench/tests/environments/include_tunables_test.py: 100%

43 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +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.""" 

6 

7from mlos_bench.environments.mock_env import MockEnv 

8from mlos_bench.services.config_persistence import ConfigPersistenceService 

9from mlos_bench.tests import BUILT_IN_ENV_VAR_DEFAULTS 

10from mlos_bench.tunables.tunable_groups import TunableGroups 

11 

12 

13def test_one_group(tunable_groups: TunableGroups) -> None: 

14 """Make sure only one tunable group is available to the environment.""" 

15 env = MockEnv( 

16 name="Test Env", 

17 config={"tunable_params": ["provision"]}, 

18 tunables=tunable_groups, 

19 ) 

20 assert env.tunable_params.get_param_values() == { 

21 "vmSize": "Standard_B4ms", 

22 } 

23 

24 

25def test_two_groups(tunable_groups: TunableGroups) -> None: 

26 """Make sure only the selected tunable groups are available to the environment.""" 

27 env = MockEnv( 

28 name="Test Env", 

29 config={"tunable_params": ["provision", "kernel"]}, 

30 tunables=tunable_groups, 

31 ) 

32 assert env.tunable_params.get_param_values() == { 

33 "vmSize": "Standard_B4ms", 

34 "kernel_sched_migration_cost_ns": -1, 

35 "kernel_sched_latency_ns": 2000000, 

36 } 

37 

38 

39def test_two_groups_setup(tunable_groups: TunableGroups) -> None: 

40 """Make sure only the selected tunable groups are available to the environment, the 

41 set is not changed after calling the `.setup()` method. 

42 """ 

43 env = MockEnv( 

44 name="Test Env", 

45 config={ 

46 "tunable_params": ["provision", "kernel"], 

47 "const_args": { 

48 "const_param1": 10, 

49 "const_param2": "foo", 

50 }, 

51 }, 

52 tunables=tunable_groups, 

53 ) 

54 expected_params = { 

55 "vmSize": "Standard_B4ms", 

56 "kernel_sched_migration_cost_ns": -1, 

57 "kernel_sched_latency_ns": 2000000, 

58 } 

59 assert env.tunable_params.get_param_values() == expected_params 

60 

61 with env as env_context: 

62 assert env_context.setup(tunable_groups) 

63 

64 # Make sure the set of tunables does not change after the setup: 

65 assert env.tunable_params.get_param_values() == expected_params 

66 assert env.parameters == { 

67 **expected_params, 

68 "const_param1": 10, 

69 "const_param2": "foo", 

70 **BUILT_IN_ENV_VAR_DEFAULTS, 

71 } 

72 

73 

74def test_zero_groups_implicit(tunable_groups: TunableGroups) -> None: 

75 """Make sure that no tunable groups are available to the environment by default.""" 

76 env = MockEnv(name="Test Env", config={}, tunables=tunable_groups) 

77 assert env.tunable_params.get_param_values() == {} 

78 

79 

80def test_zero_groups_explicit(tunable_groups: TunableGroups) -> None: 

81 """Make sure that no tunable groups are available to the environment when explicitly 

82 specifying an empty list of tunable_params. 

83 """ 

84 env = MockEnv(name="Test Env", config={"tunable_params": []}, tunables=tunable_groups) 

85 assert env.tunable_params.get_param_values() == {} 

86 

87 

88def test_zero_groups_implicit_setup(tunable_groups: TunableGroups) -> None: 

89 """Make sure that no tunable groups are available to the environment by default and 

90 it does not change after the setup. 

91 """ 

92 env = MockEnv( 

93 name="Test Env", 

94 config={ 

95 "const_args": { 

96 "const_param1": 10, 

97 "const_param2": "foo", 

98 }, 

99 }, 

100 tunables=tunable_groups, 

101 ) 

102 assert env.tunable_params.get_param_values() == {} 

103 

104 with env as env_context: 

105 assert env_context.setup(tunable_groups) 

106 

107 # Make sure the set of tunables does not change after the setup: 

108 assert env.tunable_params.get_param_values() == {} 

109 assert env.parameters == { 

110 "const_param1": 10, 

111 "const_param2": "foo", 

112 **BUILT_IN_ENV_VAR_DEFAULTS, 

113 } 

114 

115 

116def test_loader_level_include() -> None: 

117 """Make sure only the selected tunable groups are available to the environment, the 

118 set is not changed after calling the `.setup()` method. 

119 """ 

120 env_json = { 

121 "class": "mlos_bench.environments.mock_env.MockEnv", 

122 "name": "Test Env", 

123 "include_tunables": ["environments/os/linux/boot/linux-boot-tunables.jsonc"], 

124 "config": { 

125 "tunable_params": ["linux-kernel-boot"], 

126 "const_args": { 

127 "const_param1": 10, 

128 "const_param2": "foo", 

129 }, 

130 }, 

131 } 

132 loader = ConfigPersistenceService( 

133 { 

134 "config_path": [ 

135 "mlos_bench/config", 

136 "mlos_bench/examples", 

137 ] 

138 } 

139 ) 

140 env = loader.build_environment(config=env_json, tunables=TunableGroups()) 

141 expected_params = { 

142 "align_va_addr": "on", 

143 "idle": "halt", 

144 "ima.ahash_bufsize": 4096, 

145 "noautogroup": "", 

146 "nohugevmalloc": "", 

147 "nohalt": "", 

148 "nohz": "", 

149 "no-kvmapf": "", 

150 "nopvspin": "", 

151 } 

152 assert env.tunable_params.get_param_values() == expected_params 

153 

154 expected_params["align_va_addr"] = "off" 

155 tunables = env.tunable_params.copy().assign({"align_va_addr": "off"}) 

156 

157 with env as env_context: 

158 assert env_context.setup(tunables) 

159 

160 # Make sure the set of tunables does not change after the setup: 

161 assert env.parameters == { 

162 **expected_params, 

163 "const_param1": 10, 

164 "const_param2": "foo", 

165 **BUILT_IN_ENV_VAR_DEFAULTS, 

166 } 

167 assert env.tunable_params.get_param_values() == expected_params