summaryrefslogtreecommitdiffstats
path: root/qtip
diff options
context:
space:
mode:
Diffstat (limited to 'qtip')
-rw-r--r--qtip/base/__init__.py19
-rw-r--r--qtip/base/constant.py26
-rw-r--r--qtip/base/error.py11
-rw-r--r--qtip/cli/commands/cmd_metric.py2
-rw-r--r--qtip/collector/__init__.py25
-rw-r--r--qtip/collector/base.py14
-rw-r--r--qtip/collector/logfile.py63
-rw-r--r--qtip/collector/parser/__init__.py0
-rw-r--r--qtip/collector/parser/grep.py33
-rw-r--r--qtip/loader/file.py10
-rw-r--r--qtip/loader/plan.py35
-rw-r--r--qtip/loader/yaml_file.py19
-rw-r--r--qtip/runner/__init__.py6
13 files changed, 172 insertions, 91 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 ddd07e9d..09c635ac 100644
--- a/qtip/base/constant.py
+++ b/qtip/base/constant.py
@@ -34,6 +34,8 @@ class BaseProp(object):
# content
DESCRIPTION = 'description'
+ WORKLOADS = 'workloads'
+ TYPE = 'type'
class SpecProp(BaseProp):
@@ -45,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/base.py b/qtip/collector/base.py
deleted file mode 100644
index 2a25455c..00000000
--- a/qtip/collector/base.py
+++ /dev/null
@@ -1,14 +0,0 @@
-##############################################################################
-# Copyright (c) 2016 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 BaseCollector(object):
- """performance metrics collector"""
- def __init__(self, config):
- self._config = config
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/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)