diff options
Diffstat (limited to 'qtip')
-rw-r--r-- | qtip/base/__init__.py | 19 | ||||
-rw-r--r-- | qtip/base/constant.py | 25 | ||||
-rw-r--r-- | qtip/base/error.py | 11 | ||||
-rw-r--r-- | qtip/cli/commands/cmd_metric.py | 2 | ||||
-rw-r--r-- | qtip/collector/__init__.py | 25 | ||||
-rw-r--r-- | qtip/collector/logfile.py | 63 | ||||
-rw-r--r-- | qtip/collector/parser/__init__.py | 0 | ||||
-rw-r--r-- | qtip/collector/parser/grep.py | 33 | ||||
-rw-r--r-- | qtip/driver/ansible/__init__.py | 0 | ||||
-rw-r--r-- | qtip/driver/ansible/ansible.py (renamed from qtip/driver/ansible.py) | 2 | ||||
-rw-r--r-- | qtip/driver/ansible/playbook/bwn_ng.yaml | 23 | ||||
-rw-r--r-- | qtip/driver/ansible/playbook/inxi.yaml | 23 | ||||
-rw-r--r-- | qtip/driver/ansible/playbook/prepare_env.yaml | 42 | ||||
-rw-r--r-- | qtip/driver/ansible/playbook/top.yaml (renamed from qtip/collector/base.py) | 10 | ||||
-rw-r--r-- | qtip/loader/file.py | 10 | ||||
-rw-r--r-- | qtip/loader/plan.py | 35 | ||||
-rw-r--r-- | qtip/loader/yaml_file.py | 19 | ||||
-rw-r--r-- | qtip/runner/__init__.py | 6 |
18 files changed, 263 insertions, 85 deletions
diff --git a/qtip/base/__init__.py b/qtip/base/__init__.py index e69de29b..909703ed 100644 --- a/qtip/base/__init__.py +++ b/qtip/base/__init__.py @@ -0,0 +1,19 @@ +############################################################################## +# 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 +############################################################################## + + +class BaseActor(object): + """abstract actor class""" + + def __init__(self, config, parent=None): + self._config = config + self._parent = parent + + def get_config(self, key, default=None): + return self._config.get(key, default) diff --git a/qtip/base/constant.py b/qtip/base/constant.py index 39572040..09c635ac 100644 --- a/qtip/base/constant.py +++ b/qtip/base/constant.py @@ -35,6 +35,7 @@ class BaseProp(object): # content DESCRIPTION = 'description' WORKLOADS = 'workloads' + TYPE = 'type' class SpecProp(BaseProp): @@ -46,29 +47,5 @@ class SpecProp(BaseProp): WORKLOADS = 'workloads' -class PlanProp(BaseProp): - # plan - INFO = 'info' - - FACILITY = 'facility' - ENGINEER = 'engineer' - - CONFIG = 'config' - - DRIVER = 'driver' - COLLECTOR = 'collector' - REPORTER = 'reporter' - - QPIS = 'QPIs' - - -class CollectorProp(BaseProp): - LOGS = 'logs' - FILENAME = 'filename' - GREP = 'grep' - REGEX = 'regex' - CAPTURE = 'capture' - - class ReporterBaseProp(BaseProp): TRANSFORMER = 'transformer' diff --git a/qtip/base/error.py b/qtip/base/error.py index 01a7f7a6..a055aa8d 100644 --- a/qtip/base/error.py +++ b/qtip/base/error.py @@ -8,22 +8,23 @@ ############################################################################## -class QtipError(Exception): +class BaseError(Exception): pass -class InvalidFormat(QtipError): - def __init__(self, filename): +class InvalidContent(BaseError): + def __init__(self, filename, excinfo=None): self.filename = filename + self.excinfo = excinfo -class NotFound(QtipError): +class NotFound(BaseError): def __init__(self, module, package='qtip'): self.package = package self.module = module -class ToBeDoneError(QtipError): +class ToBeDoneError(BaseError): """something still to be done""" def __init__(self, method, module): self.method = method diff --git a/qtip/cli/commands/cmd_metric.py b/qtip/cli/commands/cmd_metric.py index d2fbd58f..aa4df1f4 100644 --- a/qtip/cli/commands/cmd_metric.py +++ b/qtip/cli/commands/cmd_metric.py @@ -27,7 +27,7 @@ def cmd_list(ctx): pass -@cli.command('run', help='Run tests to collect Performance Metrics') +@cli.command('run', help='Run tests to run Performance Metrics') @click.argument('name') @pass_context def cmd_run(ctx, name): diff --git a/qtip/collector/__init__.py b/qtip/collector/__init__.py index e69de29b..cc957ba4 100644 --- a/qtip/collector/__init__.py +++ b/qtip/collector/__init__.py @@ -0,0 +1,25 @@ +############################################################################## +# 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 qtip.base.constant import BaseProp +from qtip.collector.parser.grep import GrepParser + + +class CollectorProp(BaseProp): + TYPE = 'type' + PARSERS = 'parsers' + PATHS = 'paths' + + +def load_parser(type_name): + if type_name == GrepParser.TYPE: + return GrepParser + else: + raise Exception("Invalid parser type: {}".format(type_name)) diff --git a/qtip/collector/logfile.py b/qtip/collector/logfile.py index 19780aaa..2c2e532f 100644 --- a/qtip/collector/logfile.py +++ b/qtip/collector/logfile.py @@ -7,32 +7,49 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from base import BaseCollector +from itertools import chain +from six.moves import reduce +import os -from qtip.base.constant import CollectorProp as CProp +from qtip.base import BaseActor +from qtip.collector import load_parser +from qtip.collector import CollectorProp as CProp from qtip.loader.file import FileLoader -class LogfileCollector(BaseCollector): - """collect performance metrics from log files""" +class LogItem(BaseActor): + def find(self, filename, paths=None): + return self._parent.find(filename, paths) - def __init__(self, config, paths=None): + +class LogfileCollector(BaseActor): + """run performance metrics from log files""" + TYPE = 'logfile' + LOGS = 'logs' + PATHS = 'paths' + + def __init__(self, config, parent=None): super(LogfileCollector, self).__init__(config) - self.loader = FileLoader('.', paths) - - def collect(self): - captured = {} - for item in self._config[CProp.LOGS]: - captured.update(self._parse_log(item)) - return captured - - def _parse_log(self, log_item): - captured = {} - # TODO(yujunz) select parser by name - if CProp.GREP in log_item: - for rule in log_item[CProp.GREP]: - captured.update(self._grep(log_item[CProp.FILENAME], rule)) - return captured - - def _grep(self, filename, rule): - return {} + self._parent = parent # plan + # TODO(yujunz) handle exception of invalid parent + dirname = os.path.dirname(self._parent.abspath) + paths = [os.path.join(dirname, p) for p in config.get(self.PATHS, [])] + self._loader = FileLoader('.', paths) + + def run(self): + collected = [] + for log_item_config in self._config[self.LOGS]: + log_item = LogItem(log_item_config, self) + matches = [load_parser(c[CProp.TYPE])(c, log_item).run() + for c in log_item.get_config(CProp.PARSERS)] + collected = chain(collected, reduce(chain, matches)) + return reduce(merge_matchobj_to_dict, collected, {'groups': (), 'groupdict': {}}) + + def find(self, filename, paths=None): + return self._loader.find(filename, paths) + + +def merge_matchobj_to_dict(d, m): + d['groups'] = chain(d['groups'], m.groups()) + d['groupdict'].update(m.groupdict()) + return d diff --git a/qtip/collector/parser/__init__.py b/qtip/collector/parser/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/qtip/collector/parser/__init__.py diff --git a/qtip/collector/parser/grep.py b/qtip/collector/parser/grep.py new file mode 100644 index 00000000..d7ada485 --- /dev/null +++ b/qtip/collector/parser/grep.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 +############################################################################## + + +import re + + +from qtip.base.constant import BaseProp +from qtip.base import BaseActor + + +class GrepProp(BaseProp): + FILENAME = 'filename' + REGEX = 'regex' + + +class GrepParser(BaseActor): + TYPE = 'grep' + + def run(self): + filename = self._parent.get_config(GrepProp.FILENAME) + return grep_in_file(self._parent.find(filename), self._config[GrepProp.REGEX]) + + +def grep_in_file(filename, regex): + with open(filename, 'r') as f: + return filter(lambda x: x is not None, [re.search(regex, line) for line in f]) diff --git a/qtip/driver/ansible/__init__.py b/qtip/driver/ansible/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/qtip/driver/ansible/__init__.py diff --git a/qtip/driver/ansible.py b/qtip/driver/ansible/ansible.py index 04e9f9bd..cd17625d 100644 --- a/qtip/driver/ansible.py +++ b/qtip/driver/ansible/ansible.py @@ -7,7 +7,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from base import BaseDriver +from qtip.driver.base import BaseDriver class AnsibleDriver(BaseDriver): diff --git a/qtip/driver/ansible/playbook/bwn_ng.yaml b/qtip/driver/ansible/playbook/bwn_ng.yaml new file mode 100644 index 00000000..99477856 --- /dev/null +++ b/qtip/driver/ansible/playbook/bwn_ng.yaml @@ -0,0 +1,23 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation 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 +############################################################################## +- name: Install bwm-ng when CentOS + yum: + name: bwm-ng + state: present + when: ansible_os_family == "RedHat" + +- name: Install bwm-ng when Ubuntu + apt: + name: bwm-ng + state: present + update_cache: yes + when: ansible_os_family == "Debian" + +- name: Run bwm-ng + shell: bwm-ng -o plain -c 1 > $HOME/qtip_result/bwm-dump.log
\ No newline at end of file diff --git a/qtip/driver/ansible/playbook/inxi.yaml b/qtip/driver/ansible/playbook/inxi.yaml new file mode 100644 index 00000000..f6a0311d --- /dev/null +++ b/qtip/driver/ansible/playbook/inxi.yaml @@ -0,0 +1,23 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation 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 +############################################################################## +- name: Install Inxi when CentOS + yum: + name: inxi + state: present + when: ansible_os_family == "RedHat" + +- name: Install Inxi when Ubuntu + apt: + name: inxi + state: present + update_cache: yes + when: ansible_os_family == "Debian" + +- name: Run inxi + shell: inxi -b -c0 -n > $HOME/qtip_result/inxi.log diff --git a/qtip/driver/ansible/playbook/prepare_env.yaml b/qtip/driver/ansible/playbook/prepare_env.yaml new file mode 100644 index 00000000..0595d988 --- /dev/null +++ b/qtip/driver/ansible/playbook/prepare_env.yaml @@ -0,0 +1,42 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation 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 +############################################################################## +- name: Epel Release install when CentOS + yum: + name: epel-release + state: present + when: ansible_os_family == "RedHat" + +- name: Software Properties Common + apt: + name: software-properties-common + state: present + when: ansible_os_family == "Debian" + +- name: Adding ubuntu backport main repo + apt_repository: + repo: deb http://archive.ubuntu.com/ubuntu/ {{ansible_distribution_release}}-backports main restricted universe multiverse + state: present + when: ansible_os_family == "Debian" + +- name: Adding ubuntu main repo + apt_repository: + repo: deb http://archive.ubuntu.com/ubuntu/ {{ansible_distribution_release}} main restricted universe multiverse + when: ansible_os_family == "Debian" + +- name: Install ansible copy dependencies if remote host has selinux enabled + yum: + name: libselinux-python + state: present + when: ansible_os_family == "RedHat" + +- name: Install ansiblle copy dependencies if remote host has selinux enaled + apt: + name: python-selinux + state: present + when: ansible_os_family == "Debian" diff --git a/qtip/collector/base.py b/qtip/driver/ansible/playbook/top.yaml index 2a25455c..8de7e3d6 100644 --- a/qtip/collector/base.py +++ b/qtip/driver/ansible/playbook/top.yaml @@ -1,14 +1,10 @@ ############################################################################## -# Copyright (c) 2016 ZTE Corp and others. +# Copyright (c) 2017 ZTE Corporation 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 ############################################################################## - - -class BaseCollector(object): - """performance metrics collector""" - def __init__(self, config): - self._config = config +- name: Collect cpu usage + shell: top -bn1 > $HOME/qtip_result/top.log diff --git a/qtip/loader/file.py b/qtip/loader/file.py index 00f94818..0ea4d5b6 100644 --- a/qtip/loader/file.py +++ b/qtip/loader/file.py @@ -25,12 +25,12 @@ class FileLoader(BaseLoader): _paths = [ROOT_DIR] def __init__(self, name, paths=None): - self._file = name - self._abspath = self._find(name, paths=paths) + self._filename = name + self.abspath = self.find(name, paths=paths) - def _find(self, name, paths=None): + def find(self, name, paths=None): """find a specification in searching paths""" - paths = self._paths if paths is None else paths + paths = [self.abspath] if paths is None else paths for p in paths: abspath = path.join(p, self.RELATIVE_PATH, name) if path.exists(abspath): @@ -47,4 +47,4 @@ class FileLoader(BaseLoader): item = cls(name, paths=paths) yield { BaseProp.NAME: name, - BaseProp.ABSPATH: item._abspath} + BaseProp.ABSPATH: item.abspath} diff --git a/qtip/loader/plan.py b/qtip/loader/plan.py index 6f1764e2..e15651a3 100644 --- a/qtip/loader/plan.py +++ b/qtip/loader/plan.py @@ -8,12 +8,21 @@ ############################################################################## -from qtip.base.constant import PlanProp +from qtip.base.constant import BaseProp +from qtip.collector import CollectorProp as CProp from qtip.collector.logfile import LogfileCollector from qtip.loader.yaml_file import YamlFileLoader from qtip.loader.qpi import QPISpec +# TODO(yujunz) more elegant way to load module dynamically +def load_collector(type_name): + if type_name == LogfileCollector.TYPE: + return LogfileCollector + else: + raise Exception("Invalid collector type: {}".format(type_name)) + + class Plan(YamlFileLoader): """ a benchmark plan is consist of configuration and a QPI list @@ -24,10 +33,26 @@ class Plan(YamlFileLoader): def __init__(self, name, paths=None): super(Plan, self).__init__(name, paths) + _config = self.content[PlanProp.CONFIG] + + self.collectors = [load_collector(c[CProp.TYPE])(c, self) + for c in _config[PlanProp.COLLECTORS]] + 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) + +class PlanProp(BaseProp): + # plan + INFO = 'info' + + FACILITY = 'facility' + ENGINEER = 'engineer' + + CONFIG = 'config' + + DRIVER = 'driver' + COLLECTORS = 'collectors' + REPORTER = 'reporter' + + QPIS = 'QPIs' diff --git a/qtip/loader/yaml_file.py b/qtip/loader/yaml_file.py index f1cd4614..8b78a47c 100644 --- a/qtip/loader/yaml_file.py +++ b/qtip/loader/yaml_file.py @@ -7,11 +7,10 @@ # 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.error import InvalidContent from qtip.base.constant import BaseProp from qtip.loader.file import FileLoader @@ -21,13 +20,11 @@ class YamlFileLoader(FileLoader): def __init__(self, name, paths=None): super(YamlFileLoader, self).__init__(name, paths) - content = defaultdict(lambda: None) + abspath = self.abspath - 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 + with open(abspath, 'r') as stream: + content = yaml.safe_load(stream) + if not isinstance(content, dict): + raise InvalidContent(abspath) + self.content = content + self.name = content.get(BaseProp.NAME, path.splitext(name)[0]) diff --git a/qtip/runner/__init__.py b/qtip/runner/__init__.py index 1db8498e..79c38850 100644 --- a/qtip/runner/__init__.py +++ b/qtip/runner/__init__.py @@ -28,16 +28,16 @@ class Runner(object): if driver_name == 'random': self.driver = RandomDriver() else: - raise NotFound(driver_name, package=PkgName.DRIVER) + raise NotFound(driver_name, heystack=PkgName.DRIVER) if collector_name == 'stdout': self.collector = StdoutCollector() else: raise NotFound(collector_name, - package=PkgName.COLLECTOR) + heystack=PkgName.COLLECTOR) if reporter_name == 'console': self.reporter = ConsoleReporter() else: raise NotFound(reporter_name, - package=PkgName.REPORTER) + heystack=PkgName.REPORTER) |