Merge "Replace neutron floating ip deletion with shade."
[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 import time
18
19 import six
20 from stevedore import extension
21
22 import yardstick.common.utils as utils
23
24
25 def _iter_scenario_classes(scenario_type=None):
26     """Generator over all 'Scenario' subclasses
27
28     This function will iterate over all 'Scenario' subclasses defined in this
29     project and will load any class introduced by any installed plugin project,
30     defined in 'entry_points' section, under 'yardstick.scenarios' subsection.
31     """
32     extension.ExtensionManager(namespace='yardstick.scenarios',
33                                invoke_on_load=False)
34     for scenario in utils.itersubclasses(Scenario):
35         if not scenario_type:
36             yield scenario
37         elif getattr(scenario, '__scenario_type__', None) == scenario_type:
38             yield scenario
39
40
41 @six.add_metaclass(abc.ABCMeta)
42 class Scenario(object):
43
44     def setup(self):
45         """Default setup implementation for Scenario classes"""
46         pass
47
48     @abc.abstractmethod
49     def run(self, *args):
50         """Entry point for scenario classes, called from runner worker"""
51
52     def teardown(self):
53         """Default teardown implementation for Scenario classes"""
54         pass
55
56     def pre_run_wait_time(self, time_seconds):
57         """Time waited before executing the run method"""
58         pass
59
60     def post_run_wait_time(self, time_seconds):
61         """Time waited after executing the run method"""
62         time.sleep(time_seconds)
63
64     @staticmethod
65     def get_types():
66         """return a list of known runner type (class) names"""
67         scenarios = []
68         for scenario in _iter_scenario_classes():
69             scenarios.append(scenario)
70         return scenarios
71
72     @staticmethod
73     def get_cls(scenario_type):
74         """return class of specified type"""
75         for scenario in _iter_scenario_classes(scenario_type):
76             return scenario
77
78         raise RuntimeError("No such scenario type %s" % scenario_type)
79
80     @staticmethod
81     def get(scenario_type):
82         """Returns instance of a scenario runner for execution type.
83         """
84         scenario = Scenario.get_cls(scenario_type)
85         return scenario.__module__ + "." + scenario.__name__
86
87     @classmethod
88     def get_scenario_type(cls):
89         """Return a string with the scenario type, if defined"""
90         return str(getattr(cls, '__scenario_type__', None))
91
92     @classmethod
93     def get_description(cls):
94         """Return a single line string with the class description
95
96         This function will retrieve the class docstring and return the first
97         line, or 'None' if it's empty.
98         """
99         return cls.__doc__.splitlines()[0] if cls.__doc__ else str(None)
100
101     @staticmethod
102     def _push_to_outputs(keys, values):
103         """Return a dictionary given the keys and the values"""
104         return dict(zip(keys, values))
105
106     @staticmethod
107     def _change_obj_to_dict(obj):
108         """Return a dictionary from the __dict__ attribute of an object"""
109         dic = {}
110         for k, v in vars(obj).items():
111             try:
112                 vars(v)
113             except TypeError:
114                 dic[k] = v
115         return dic