Benchmarks: Code Revision - Revise subprocess invoke (#178)

**Description**
Package frequently-used subprocess invoke into function.
This commit is contained in:
guoshzhao 2021-08-31 15:34:04 +08:00 коммит произвёл GitHub
Родитель b97197f08e
Коммит 8cd264fdeb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 27 добавлений и 10 удалений

Просмотреть файл

@ -4,12 +4,11 @@
"""Module of the micro-benchmark base class."""
import os
import subprocess
import shutil
import statistics
from abc import abstractmethod
from superbench.common.utils import logger
from superbench.common.utils import logger, run_command
from superbench.benchmarks import BenchmarkType, ReturnCode
from superbench.benchmarks.base import Benchmark
@ -170,14 +169,8 @@ class MicroBenchmarkWithInvoke(MicroBenchmark):
self._curr_run_index, self._name, self._commands[cmd_idx]
)
)
output = subprocess.run(
self._commands[cmd_idx],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
check=False,
universal_newlines=True
)
output = run_command(self._commands[cmd_idx])
if output.returncode != 0:
self._result.set_return_code(ReturnCode.MICROBENCHMARK_EXECUTION_FAILURE)
logger.error(

Просмотреть файл

@ -6,6 +6,7 @@
from superbench.common.utils.logging import SuperBenchLogger, logger
from superbench.common.utils.file_handler import rotate_dir, create_sb_output_dir, get_sb_config
from superbench.common.utils.lazy_import import LazyImport
from superbench.common.utils.process import run_command
nv_helper = LazyImport('superbench.common.utils.nvidia_helper')
@ -18,4 +19,5 @@ __all__ = [
'network',
'nv_helper',
'rotate_dir',
'run_command',
]

Просмотреть файл

@ -0,0 +1,22 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Process Utility."""
import subprocess
def run_command(command):
"""Run command in string format, return the result with stdout and stderr.
Args:
command (str): command to run.
Return:
result (subprocess.CompletedProcess): The return value from subprocess.run().
"""
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, check=False, universal_newlines=True
)
return result