From f2d021c72b38845954755bab54aa13b4b2aad725 Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Thu, 19 Jan 2017 16:58:46 +0800 Subject: Refactor loader classes - rename BaseLoader to YamlFileLoader as base class of QTIP specs loader - create an abstract BaseLoader - create FileLoader for logfile collector Change-Id: I0c992cd847fc0dce4fdd73a13c1cdbc406c84532 Signed-off-by: Yujun Zhang --- qtip/loader/base.py | 53 +----------------------------------------------- qtip/loader/file.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ qtip/loader/metric.py | 4 ++-- qtip/loader/plan.py | 9 ++++++-- qtip/loader/qpi.py | 4 ++-- qtip/loader/yaml_file.py | 33 ++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 58 deletions(-) create mode 100644 qtip/loader/file.py create mode 100644 qtip/loader/yaml_file.py (limited to 'qtip/loader') diff --git a/qtip/loader/base.py b/qtip/loader/base.py index a0e5d031..abc254bf 100644 --- a/qtip/loader/base.py +++ b/qtip/loader/base.py @@ -7,57 +7,6 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from collections import defaultdict -from itertools import chain -from os import listdir -from os import path -import yaml - -from qtip.base.error import InvalidFormat, NotFound -from qtip.base.constant import BaseProp - - -ROOT_DIR = path.join(path.dirname(__file__), path.pardir, path.pardir, - 'benchmarks') - class BaseLoader(object): - """Abstract class of QTIP benchmark loader""" - RELATIVE_PATH = '.' - _paths = [ROOT_DIR] - - def __init__(self, name, paths=None): - self._file = name - self._abspath = self._find(name, paths=paths) - content = defaultdict(lambda: None) - - try: - content.update(yaml.safe_load(file(self._abspath))) - except yaml.YAMLError: - # TODO(yujunz) log yaml error - raise InvalidFormat(self._abspath) - - self.name = content[BaseProp.NAME] or path.splitext(name)[0] - self.content = content - - def _find(self, name, paths=None): - """find a benchmark in searching paths""" - paths = self._paths if paths is None else paths - for p in paths: - abspath = path.join(p, self.RELATIVE_PATH, name) - if path.exists(abspath): - return abspath - raise NotFound(name, paths) - - @classmethod - def list_all(cls, paths=None): - """list all available benchmarks""" - paths = cls._paths if paths is None else paths - names = chain.from_iterable([listdir(path.join(p, cls.RELATIVE_PATH)) - for p in paths]) - for name in names: - item = cls(name, paths=paths) - yield { - BaseProp.NAME: name, - BaseProp.ABSPATH: item._abspath, - BaseProp.CONTENT: item.content} + """Abstract class of QTIP loader""" diff --git a/qtip/loader/file.py b/qtip/loader/file.py new file mode 100644 index 00000000..00f94818 --- /dev/null +++ b/qtip/loader/file.py @@ -0,0 +1,50 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from itertools import chain +from os import listdir +from os import path + +from qtip.base.constant import BaseProp +from qtip.base.error import NotFound +from qtip.loader.base import BaseLoader + + +ROOT_DIR = path.join(path.dirname(__file__), path.pardir, path.pardir, + 'benchmarks') + + +class FileLoader(BaseLoader): + RELATIVE_PATH = '.' + _paths = [ROOT_DIR] + + def __init__(self, name, paths=None): + self._file = name + self._abspath = self._find(name, paths=paths) + + def _find(self, name, paths=None): + """find a specification in searching paths""" + paths = self._paths if paths is None else paths + for p in paths: + abspath = path.join(p, self.RELATIVE_PATH, name) + if path.exists(abspath): + return abspath + raise NotFound(name, paths) + + @classmethod + def list_all(cls, paths=None): + """list all available specification""" + paths = cls._paths if paths is None else paths + names = chain.from_iterable([listdir(path.join(p, cls.RELATIVE_PATH)) + for p in paths]) + for name in names: + item = cls(name, paths=paths) + yield { + BaseProp.NAME: name, + BaseProp.ABSPATH: item._abspath} diff --git a/qtip/loader/metric.py b/qtip/loader/metric.py index 8b6fa5d3..842fcdbf 100644 --- a/qtip/loader/metric.py +++ b/qtip/loader/metric.py @@ -7,10 +7,10 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from base import BaseLoader +from yaml_file import YamlFileLoader -class MetricSpec(BaseLoader): +class MetricSpec(YamlFileLoader): """metrics in QTIP are categorized by performance test tools, such as dhrystone, whetstone and etc""" RELATIVE_PATH = 'metric' diff --git a/qtip/loader/plan.py b/qtip/loader/plan.py index c1ee00be..6f1764e2 100644 --- a/qtip/loader/plan.py +++ b/qtip/loader/plan.py @@ -9,11 +9,12 @@ from qtip.base.constant import PlanProp -from qtip.loader.base import BaseLoader +from qtip.collector.logfile import LogfileCollector +from qtip.loader.yaml_file import YamlFileLoader from qtip.loader.qpi import QPISpec -class Plan(BaseLoader): +class Plan(YamlFileLoader): """ a benchmark plan is consist of configuration and a QPI list """ @@ -26,3 +27,7 @@ class Plan(BaseLoader): self.qpis = [QPISpec(qpi, paths=paths) for qpi in self.content[PlanProp.QPIS]] self.info = self.content[PlanProp.INFO] + _config = self.content[PlanProp.CONFIG] + + # TODO(yujunz) create collector by name + self.collector = LogfileCollector(_config[PlanProp.COLLECTOR], paths) diff --git a/qtip/loader/qpi.py b/qtip/loader/qpi.py index ef6e065e..73da61e9 100644 --- a/qtip/loader/qpi.py +++ b/qtip/loader/qpi.py @@ -7,14 +7,14 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from base import BaseLoader +from yaml_file import YamlFileLoader from metric import MetricSpec from qtip.base.constant import SpecProp from qtip.util.formula import Formula -class QPISpec(BaseLoader): +class QPISpec(YamlFileLoader): """ a QPI specification defines how to calculate a performance index from collected metrics. diff --git a/qtip/loader/yaml_file.py b/qtip/loader/yaml_file.py new file mode 100644 index 00000000..f1cd4614 --- /dev/null +++ b/qtip/loader/yaml_file.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from collections import defaultdict +from os import path +import yaml + +from qtip.base.error import InvalidFormat +from qtip.base.constant import BaseProp +from qtip.loader.file import FileLoader + + +class YamlFileLoader(FileLoader): + """load content from yaml file""" + + def __init__(self, name, paths=None): + super(YamlFileLoader, self).__init__(name, paths) + content = defaultdict(lambda: None) + + try: + content.update(yaml.safe_load(file(self._abspath))) + except yaml.YAMLError: + # TODO(yujunz) log yaml error + raise InvalidFormat(self._abspath) + + self.name = content[BaseProp.NAME] or path.splitext(name)[0] + self.content = content -- cgit 1.2.3-korg