Coverage for mlos_bench/mlos_bench/tests/environments/local/__init__.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 01:52 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Tests for mlos_bench.environments.local. 

7 

8Used to make mypy happy about multiple conftest.py modules. 

9""" 

10 

11from typing import Any, Dict, List 

12 

13from mlos_bench.environments.composite_env import CompositeEnv 

14from mlos_bench.environments.local.local_env import LocalEnv 

15from mlos_bench.services.config_persistence import ConfigPersistenceService 

16from mlos_bench.services.local.local_exec import LocalExecService 

17from mlos_bench.tunables.tunable_groups import TunableGroups 

18 

19 

20def create_local_env(tunable_groups: TunableGroups, config: Dict[str, Any]) -> LocalEnv: 

21 """ 

22 Create a LocalEnv with the given configuration. 

23 

24 Parameters 

25 ---------- 

26 tunable_groups : TunableGroups 

27 Tunable parameters (usually come from a fixture). 

28 config : Dict[str, Any] 

29 Environment configuration. 

30 

31 Returns 

32 ------- 

33 env : LocalEnv 

34 A new instance of the local environment. 

35 """ 

36 return LocalEnv( 

37 name="TestLocalEnv", 

38 config=config, 

39 tunables=tunable_groups, 

40 service=LocalExecService(parent=ConfigPersistenceService()), 

41 ) 

42 

43 

44def create_composite_local_env( 

45 tunable_groups: TunableGroups, 

46 global_config: Dict[str, Any], 

47 params: Dict[str, Any], 

48 local_configs: List[Dict[str, Any]], 

49) -> CompositeEnv: 

50 """ 

51 Create a CompositeEnv with several LocalEnv instances. 

52 

53 Parameters 

54 ---------- 

55 tunable_groups : TunableGroups 

56 Tunable parameters (usually come from a fixture). 

57 global_config : Dict[str, Any] 

58 Global configuration parameters. 

59 params: Dict[str, Any] 

60 Additional config params for the CompositeEnv. 

61 local_configs: List[Dict[str, Any]] 

62 Configurations of the local environments. 

63 

64 Returns 

65 ------- 

66 env : CompositeEnv 

67 A new instance of the local environment. 

68 """ 

69 return CompositeEnv( 

70 name="TestCompositeEnv", 

71 config={ 

72 **params, 

73 "children": [ 

74 { 

75 "name": f"TestLocalEnv{i}", 

76 "class": "mlos_bench.environments.local.local_env.LocalEnv", 

77 "config": config, 

78 } 

79 for (i, config) in enumerate(local_configs) 

80 ], 

81 }, 

82 tunables=tunable_groups, 

83 global_config=global_config, 

84 service=LocalExecService(parent=ConfigPersistenceService()), 

85 )