3b88ade7d9f7e53a9f9b44664a02b786351976a1
[yardstick.git] / yardstick / benchmark / scenarios / base.py
1 # Copyright 2013: Mirantis Inc.
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 import abc
17
18 import six
19 from stevedore import extension
20
21 import yardstick.common.utils as utils
22
23
24 def _iter_scenario_classes(scenario_type=None):
25     """Generator over all 'Scenario' subclasses
26
27     This function will iterate over all 'Scenario' subclasses defined in this
28     project and will load any class introduced by any installed plugin project,
29     defined in 'entry_points' section, under 'yardstick.scenarios' subsection.
30     """
31     extension.ExtensionManager(namespace='yardstick.scenarios',
32                                invoke_on_load=False)
33     for scenario in utils.itersubclasses(Scenario):
34         if not scenario_type:
35             yield scenario
36         elif getattr(scenario, '__scenario_type__', None) == scenario_type:
37             yield scenario
38
39
40 @six.add_metaclass(abc.ABCMeta)
41 class Scenario(object):
42
43     def setup(self):
44         """Default setup implementation for Scenario classes"""
45         pass
46
47     @abc.abstractmethod
48     def run(self, *args):
49         """Entry point for scenario classes, called from runner worker"""
50
51     def teardown(self):
52         """Default teardown implementation for Scenario classes"""
53         pass
54
55     @staticmethod
56     def get_types():
57         """return a list of known runner type (class) names"""
58         scenarios = []
59         for scenario in _iter_scenario_classes():
60             scenarios.append(scenario)
61         return scenarios
62
63     @staticmethod
64     def get_cls(scenario_type):
65         """return class of specified type"""
66         for scenario in _iter_scenario_classes(scenario_type):
67             return scenario
68
69         raise RuntimeError("No such scenario type %s" % scenario_type)
70
71     @staticmethod
72     def get(scenario_type):
73         """Returns instance of a scenario runner for execution type.
74         """
75         scenario = Scenario.get_cls(scenario_type)
76         return scenario.__module__ + "." + scenario.__name__
77
78     @classmethod
79     def get_scenario_type(cls):
80         """Return a string with the scenario type, if defined"""
81         return str(getattr(cls, '__scenario_type__', None))
82
83     @classmethod
84     def get_description(cls):
85         """Return a single line string with the class description
86
87         This function will retrieve the class docstring and return the first
88         line, or 'None' if it's empty.
89         """
90         return cls.__doc__.splitlines()[0] if cls.__doc__ else str(None)
91
92     @staticmethod
93     def _push_to_outputs(keys, values):
94         """Return a dictionary given the keys and the values"""
95         return dict(zip(keys, values))
96
97     @staticmethod
98     def _change_obj_to_dict(obj):
99         """Return a dictionary from the __dict__ attribute of an object"""
100         dic = {}
101         for k, v in vars(obj).items():
102             try:
103                 vars(v)
104             except TypeError:
105                 dic[k] = v
106         return dic