From 0d95404dcdbda326a3fffa344a8295bf696f9626 Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Thu, 19 Jan 2017 20:24:48 +0000 Subject: Record task status if running via CLI JIRA: YARDSTICK-542 Currently we do not record task status when using CLI to run task. So I add this function. If status=0, task is not done. if status=1, task is done. if status=2, there is an error. Change-Id: Ib9b3c8abd233909c04f792115199250419fa8d7a Signed-off-by: chenjiankun --- yardstick/benchmark/core/task.py | 2 -- yardstick/cmd/commands/task.py | 22 ++++++++++++++--- yardstick/common/constants.py | 2 ++ yardstick/common/utils.py | 10 ++++++++ yardstick/dispatcher/file.py | 51 +++++++--------------------------------- 5 files changed, 39 insertions(+), 48 deletions(-) diff --git a/yardstick/benchmark/core/task.py b/yardstick/benchmark/core/task.py index 14b46867c..87d70f42f 100644 --- a/yardstick/benchmark/core/task.py +++ b/yardstick/benchmark/core/task.py @@ -66,8 +66,6 @@ class Task(object): # pragma: no cover if args.parse_only: sys.exit(0) - if os.path.isfile(args.output_file): - os.remove(args.output_file) # parse task_files for i in range(0, len(task_files)): one_task_start_time = time.time() diff --git a/yardstick/cmd/commands/task.py b/yardstick/cmd/commands/task.py index d7c0a01fb..20ab086e5 100644 --- a/yardstick/cmd/commands/task.py +++ b/yardstick/cmd/commands/task.py @@ -1,4 +1,4 @@ -############################################################################## +############################################################################# # Copyright (c) 2015 Ericsson AB and others. # # All rights reserved. This program and the accompanying materials @@ -9,10 +9,12 @@ """ Handler for yardstick command 'task' """ from __future__ import print_function - from __future__ import absolute_import + from yardstick.benchmark.core.task import Task from yardstick.common.utils import cliargs +from yardstick.common.utils import write_json_to_file +from yardstick.common import constants as consts from yardstick.cmd.commands import change_osloobj_to_paras output_file_default = "/tmp/yardstick.out" @@ -42,4 +44,18 @@ class TaskCommands(object): action="store_true") def do_start(self, args, **kwargs): param = change_osloobj_to_paras(args) - Task().start(param, **kwargs) + + self._init_result_file() + + try: + Task().start(param) + except Exception as e: + self._write_error_data(e) + + def _init_result_file(self): + data = {'status': 0, 'result': []} + write_json_to_file(consts.DEFAULT_OUTPUT_FILE, data) + + def _write_error_data(self, error): + data = {'status': 2, 'result': str(error)} + write_json_to_file(consts.DEFAULT_OUTPUT_FILE, data) diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index ffca4b3e9..85ef9fcd0 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -57,3 +57,5 @@ ENV_ACTION_API = BASE_URL + '/yardstick/env/action' ASYNC_TASK_API = BASE_URL + '/yardstick/asynctask' SQLITE = 'sqlite:////tmp/yardstick.db' + +DEFAULT_OUTPUT_FILE = '/tmp/yardstick.out' diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 57ace14e6..473bbf540 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -30,6 +30,7 @@ from keystoneauth1 import identity from keystoneauth1 import session from neutronclient.v2_0 import client from oslo_utils import importutils +from oslo_serialization import jsonutils import yardstick @@ -145,3 +146,12 @@ def get_neutron_client(): sess = get_openstack_session() neutron_client = client.Client(session=sess) return neutron_client + + +def write_json_to_file(path, data, mode='w'): + write_file(path, jsonutils.dump_as_bytes(data), mode) + + +def write_file(path, data, mode='w'): + with open(path, mode) as f: + f.write(data) diff --git a/yardstick/dispatcher/file.py b/yardstick/dispatcher/file.py index 9c728e983..8d3c3693d 100644 --- a/yardstick/dispatcher/file.py +++ b/yardstick/dispatcher/file.py @@ -18,28 +18,9 @@ from __future__ import absolute_import -import logging -import logging.handlers - -from oslo_serialization import jsonutils -from oslo_config import cfg - from yardstick.dispatcher.base import Base as DispatchBase - -CONF = cfg.CONF -OPTS = [ - cfg.StrOpt('file_path', - default='/tmp/yardstick.out', - help='Name and the location of the file to record ' - 'data.'), - cfg.IntOpt('max_bytes', - default=0, - help='The max size of the file.'), - cfg.IntOpt('backup_count', - default=0, - help='The max number of the files to keep.'), -] -CONF.register_opts(OPTS, group="dispatcher_file") +from yardstick.common import constants as consts +from yardstick.common import utils class FileDispatcher(DispatchBase): @@ -50,29 +31,13 @@ class FileDispatcher(DispatchBase): def __init__(self, conf): super(FileDispatcher, self).__init__(conf) - self.log = None - - # if the directory and path are configured, then log to the file - if CONF.dispatcher_file.file_path: - dispatcher_logger = logging.Logger('dispatcher.file') - dispatcher_logger.setLevel(logging.INFO) - # create rotating file handler which logs result - rfh = logging.handlers.RotatingFileHandler( - self.conf.get('file_path', CONF.dispatcher_file.file_path), - maxBytes=CONF.dispatcher_file.max_bytes, - backupCount=CONF.dispatcher_file.backup_count, - encoding='utf8') - - rfh.setLevel(logging.INFO) - # Only wanted the data to be saved in the file, not the - # project root logger. - dispatcher_logger.propagate = False - dispatcher_logger.addHandler(rfh) - self.log = dispatcher_logger + self.result = [] def record_result_data(self, data): - if self.log: - self.log.info(jsonutils.dump_as_bytes(data)) + self.result.append(data) def flush_result_data(self): - pass + file_path = self.conf.get('file_path', consts.DEFAULT_OUTPUT_FILE) + + data = {'status': 1, 'result': self.result} + utils.write_json_to_file(file_path, data) -- cgit 1.2.3-korg