-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathschedule_performance_benchmarks.py
373 lines (331 loc) · 11.2 KB
/
schedule_performance_benchmarks.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
# Copyright(C) 2019, 2020 Francesco Murdaca
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Schedule Inspections for testing Performance Benchmarks to be evaluated using Amun."""
import sys
import logging
import os
import subprocess
import json
from thoth.python import Source
from thoth.python import Project
from thoth.python import PackageVersion
from pathlib import Path
from exceptions import NotInstalledIndexException
from exceptions import FileCreationException
from exceptions import ScriptFrameworkIncompatibilityException
import click
import daiquiri
from pipefile2json import pipfile2dict
daiquiri.setup(level=logging.INFO)
_LOGGER = logging.getLogger(__name__)
def verify_framework_version_installed(framework_name: str, path: str, index_url: str):
"""Verify framework/version installed provenance."""
p = subprocess.Popen(
["pipenv", "run", "pip", "show", framework_name],
cwd=path,
stdout=subprocess.PIPE,
)
out, err = p.communicate()
_LOGGER.info(out.decode("utf-8"))
if not index_url == "https://pypi.org/simple":
if "Red Hat Inc." in out.decode("utf-8"):
_LOGGER.info(
"The index used for installation is from Red Hat AICoE Thoth Project"
)
else:
raise NotInstalledIndexException(
f"The index used for installation is not the one requested: {out}"
)
def create_amun_api_input(
name_inspection: str,
base_image: str,
native_packages: str,
python_packages: str,
framework: str,
framework_version: str,
index_url: str,
benchmark: str,
) -> dict:
"""Create specification for Amun API input."""
with open("./inspection.json") as json_file:
specification = json.load(json_file)
# Name of the inspection/s
specification["identifier"] = name_inspection
specification["base"] = base_image
if native_packages:
native_packages = native_packages.split(",")
else:
native_packages = []
specification["packages"] = native_packages
if python_packages:
python_packages = python_packages.split(",")
else:
python_packages = []
specification["python_packages"] = python_packages
create_pipfile_and_pipfile_lock_inputs(
framework=framework, framework_version=framework_version, index_url=index_url
)
# Insert Pipfile and Pipfile.lock and make them str for input
current_path = Path.cwd()
new_dir_path = current_path.joinpath("amun")
pipfile_path = new_dir_path.joinpath("Pipfile")
pipfile_lock_path = new_dir_path.joinpath("Pipfile.lock")
specification["python"]["requirements"] = pipfile2dict(pipfile_path=pipfile_path)
with open(pipfile_lock_path) as json_file:
requirements_locked = json.load(json_file)
specification["python"]["requirements_locked"] = requirements_locked
# Insert script for performance test
specification["script"] = benchmark
update_json_specification(
path_template_specification="./inspection.json", content=specification
)
_LOGGER.info(f"Updated the new json specification file for Amun API.")
return specification
def update_json_specification(path_template_specification: str, content: object):
"""Update the json specification with new changes."""
os.remove("{}".format(path_template_specification))
with open("{}".format(path_template_specification), "w") as outfile:
json.dump(content, outfile, indent=4)
def create_pipfile(
index_url: str, framework: str, framework_version: str, pipfile_path: str
):
"""Create Pipfile from inputs."""
packages = [
PackageVersion(
name=f"{framework}",
version=f"=={framework_version}",
develop=False,
index=Source(index_url),
)
]
project = Project.from_package_versions(packages)
if not index_url == "https://pypi.org/simple":
project.add_source("https://pypi.org/simple")
project.set_python_version("3.6")
_LOGGER.info(f"Pipfile created:\n {project.pipfile.to_string()}")
with open(pipfile_path, "w+") as pipfile:
pipfile.write(project.pipfile.to_string())
def create_pipfile_and_pipfile_lock_inputs(
framework: str, framework_version: str, index_url: str
):
"""Create requirements and requirements_locked."""
current_path = Path.cwd()
new_dir_path = current_path.joinpath("amun")
os.makedirs(new_dir_path, exist_ok=True)
pipfile_path = new_dir_path.joinpath("Pipfile")
pipfile_lock_path = new_dir_path.joinpath("Pipfile.lock")
if pipfile_path.exists():
os.remove(pipfile_path)
else:
_LOGGER.info("Pipfile was not present!")
if os.path.exists(pipfile_lock_path):
os.remove(pipfile_lock_path)
else:
_LOGGER.info("Pipfile.lock was not present!")
create_pipfile(
index_url=index_url,
framework=framework,
framework_version=framework_version,
pipfile_path=pipfile_path,
)
if pipfile_path.exists():
_LOGGER.info("Pipfile was created!")
else:
raise FileCreationException("Pipfile was not created!")
_LOGGER.info(" ".join(["Running...", "pipenv", "install"]))
subprocess.call(["pipenv", "install"], cwd=new_dir_path)
verify_framework_version_installed(
framework_name=framework, path=new_dir_path, index_url=index_url
)
if os.path.exists(pipfile_lock_path):
_LOGGER.info("Pipfile.lock was created!")
else:
raise FileCreationException("Pipfile.lock was not created!")
def verify_script_framework_compatibility(framework: str, script: str):
"""Verify compatibility between framework and script to used for performances."""
_LOGGER.info(f"Verifying compatibility between framework and script...")
if framework in script:
_LOGGER.info(
f"Script selected for performance testing: {script}, is compatible with ML framework chosen: '{framework}'."
)
else:
raise ScriptFrameworkIncompatibilityException(
f"Script selected for performance testing: {script}, is not compatible with ML framework chosen: '{framework}'."
)
def schedule_performance_benchmarks(
amun_api_url: str,
name_inspection: str,
framework: str,
framework_version: str,
benchmark: str,
base_image: str,
native_packages: str,
python_packages: str,
index_url: str,
count: int,
dry_run: bool,
):
"""Schedule Performance benchmark."""
verify_script_framework_compatibility(framework=framework, script=benchmark)
_LOGGER.info(f"Platform/Base Image selected is {base_image}")
_LOGGER.info(f"Native packages to be installed on base image: {native_packages}")
_LOGGER.info(f"Python packages to be installed on base image: {python_packages}")
_LOGGER.info(f"Framework/Version selected is: {framework}:{framework_version}")
_LOGGER.info(f"Index source is: {index_url}")
_LOGGER.info(f"Performance test selected is: {benchmark}")
_LOGGER.info(f"Number of inspections requested is: {count}")
specification = create_amun_api_input(
name_inspection=name_inspection,
base_image=base_image,
native_packages=native_packages,
python_packages=python_packages,
framework=framework,
framework_version=framework_version,
index_url=index_url,
benchmark=benchmark,
)
_LOGGER.info(f"Scheduling inspection at {amun_api_url}")
_LOGGER.info(f"Specification input for Amun API is: {specification}")
for inspection_n in range(0, count):
_LOGGER.info(inspection_n + 1)
if not dry_run:
subprocess.call(
[
"curl",
"-X",
"POST",
"--header",
"Content-Type: application/json",
"--header",
"Accept: application/json",
"-d",
"@inspection.json",
amun_api_url,
]
)
@click.command()
@click.option(
"--amun-api-url",
"-a",
required=True,
type=str,
default="http://amun-api-thoth-amun-api-stage.cloud.paas.psi.redhat.com/api/v1/inspect",
show_default=True,
help="Amun API URL to talk to.",
)
@click.option(
"--name-inspection",
"-n",
required=True,
type=str,
help="User' name given to the inspections.",
)
@click.option(
"--base-image",
"-i",
required=True,
type=str,
help="Platform/Base_image used to run the inspection.",
)
@click.option(
"--native-packages",
"-r",
type=str,
default="",
show_default=True,
help="List of native packages (RPM or Deb packages) that should be installed into the requested base image.",
)
@click.option(
"--python-packages",
"-p",
type=str,
default="",
show_default=True,
help="List of python packages that should be installed into the requested base image.",
)
@click.option(
"--framework",
"-f",
required=True,
type=str,
help="Framework to be installed for the performance test.",
)
@click.option(
"--framework-version",
"-v",
required=True,
type=str,
help="Framework version to be installed for the performance test.",
)
@click.option(
"--index-url",
"-u",
required=True,
type=str,
help="URL of the framework:version to be installed for the performance test.",
)
@click.option(
"--benchmark",
"-b",
required=True,
type=str,
help="URL of the benchmark to be used to test performances of the python package.",
)
@click.option(
"--count",
"-c",
type=int,
default=100,
show_default=True,
help="Number of inspections to be scheduled.",
)
@click.option(
"--dry-run",
"-d",
type=bool,
default=False,
show_default=True,
help="Do not schedule inspections, just check all inputs are created.",
)
def cli(
amun_api_url: str,
name_inspection: str,
framework: str,
framework_version: str,
benchmark: str,
base_image: str,
native_packages: str,
python_packages: str,
index_url: str,
count: int,
dry_run: bool,
):
"""Trigger analysis of inspections for the selected platform/base_image, index_url and framework."""
schedule_performance_benchmarks(
amun_api_url=amun_api_url,
name_inspection=name_inspection,
base_image=base_image,
native_packages=native_packages,
python_packages=python_packages,
framework=framework,
framework_version=framework_version,
benchmark=benchmark,
index_url=index_url,
count=count,
dry_run=dry_run,
)
if __name__ == "__main__":
cli()