summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRex Lee <limingjiang@huawei.com>2017-01-23 02:52:24 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-01-23 02:52:24 +0000
commit5babed1aa6b2f867e639193841b231c3a0200b87 (patch)
tree0157186e4e3c94762bc80c3372a9b94254854d7d
parent75f55c05789beb4fbe2391d33349058bba4ea5c0 (diff)
parent0d95404dcdbda326a3fffa344a8295bf696f9626 (diff)
Merge "Record task status if running via CLI"
-rw-r--r--yardstick/benchmark/core/task.py2
-rw-r--r--yardstick/cmd/commands/task.py22
-rw-r--r--yardstick/common/constants.py2
-rw-r--r--yardstick/common/utils.py10
-rw-r--r--yardstick/dispatcher/file.py51
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 bf616a1f6..e6faf6828 100644
--- a/yardstick/common/constants.py
+++ b/yardstick/common/constants.py
@@ -59,3 +59,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)