-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathresources_test.py
More file actions
117 lines (107 loc) · 3.84 KB
/
resources_test.py
File metadata and controls
117 lines (107 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest
from parameterized import param
from parameterized import parameterized
from apache_beam import PTransform
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import StandardOptions
from apache_beam.testing.test_pipeline import TestPipeline
from apache_beam.transforms.resources import ResourceHint
class ResourcesTest(unittest.TestCase):
@parameterized.expand([
param(
name='min_ram',
val='100 MiB',
urn='beam:resources:min_ram_bytes:v1',
bytestr=b'104857600'),
param(
name='minRam',
val='100MB',
urn='beam:resources:min_ram_bytes:v1',
bytestr=b'100000000'),
param(
name='min_ram',
val='6.5 GiB',
urn='beam:resources:min_ram_bytes:v1',
bytestr=b'6979321856'),
param(
name='accelerator',
val='gpu',
urn='beam:resources:accelerator:v1',
bytestr=b'gpu'),
param(
name='cpu_count',
val='4',
urn='beam:resources:cpu_count:v1',
bytestr=b'4'),
param(
name='max_active_bundles_per_worker',
val='2',
urn='beam:resources:max_active_bundles_per_worker:v1',
bytestr=b'2'),
param(
name='max_active_bundle_per_worker',
val='20',
urn='beam:resources:max_active_bundles_per_worker:v1',
bytestr=b'20'),
param(
name='MaxActiveBundlePerWorker',
val='30',
urn='beam:resources:max_active_bundles_per_worker:v1',
bytestr=b'30'),
param(
name='MaxActiveBundlesPerWorker',
val='3',
urn='beam:resources:max_active_bundles_per_worker:v1',
bytestr=b'3'),
])
def test_known_resource_hints(self, name, val, urn, bytestr):
t = PTransform()
t = t.with_resource_hints(**{name: val})
self.assertTrue(ResourceHint.is_registered(name))
self.assertEqual(t.get_resource_hints(), {urn: bytestr})
@parameterized.expand([
param(name='min_ram', val='3,500G'),
param(name='accelerator', val=1),
param(name='cpu_count', val=1),
param(name='unknown_hint', val=1)
])
def test_resource_hint_parsing_fails_early(self, name, val):
t = PTransform()
with self.assertRaises(ValueError):
_ = t.with_resource_hints(**{name: val})
def test_resource_hints_from_options(self):
options = PipelineOptions()
standard_options = options.view_as(StandardOptions)
standard_options.resource_hints = {
"min_ram": "16GB",
"accelerator": "gpu",
"cpu_count": "4",
"max_active_bundles_per_worker": "2"
}
p = TestPipeline(options=options)
self.assertEqual(
p._root_transform().resource_hints,
{
'beam:resources:min_ram_bytes:v1': b'16000000000',
'beam:resources:accelerator:v1': b'gpu',
'beam:resources:cpu_count:v1': b'4',
'beam:resources:max_active_bundles_per_worker:v1': b'2'
})
if __name__ == '__main__':
unittest.main()