Merge "Refactor userguide "Yardstick Installation""
[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 # yardstick comment: this is a modified copy of
17 # rally/rally/benchmark/scenarios/base.py
18
19 """ Scenario base class
20 """
21
22 from __future__ import absolute_import
23 import yardstick.common.utils as utils
24
25
26 class Scenario(object):
27
28     def setup(self):
29         """ default impl for scenario setup """
30         pass
31
32     def run(self, args):
33         """ catcher for not implemented run methods in subclasses """
34         raise RuntimeError("run method not implemented")
35
36     def teardown(self):
37         """ default impl for scenario teardown """
38         pass
39
40     @staticmethod
41     def get_types():
42         """return a list of known runner type (class) names"""
43         scenarios = []
44         for scenario in utils.itersubclasses(Scenario):
45             scenarios.append(scenario)
46         return scenarios
47
48     @staticmethod
49     def get_cls(scenario_type):
50         """return class of specified type"""
51         for scenario in utils.itersubclasses(Scenario):
52             if scenario_type == scenario.__scenario_type__:
53                 return scenario
54
55         raise RuntimeError("No such scenario type %s" % scenario_type)
56
57     @staticmethod
58     def get(scenario_type):
59         """Returns instance of a scenario runner for execution type.
60         """
61         for scenario in utils.itersubclasses(Scenario):
62             if scenario_type == scenario.__scenario_type__:
63                 return scenario.__module__ + "." + scenario.__name__
64
65         raise RuntimeError("No such scenario type %s" % scenario_type)
66
67     @classmethod
68     def get_scenario_type(cls):
69         """Return a string with the scenario type, if defined"""
70         return str(getattr(cls, '__scenario_type__', None))
71
72     @classmethod
73     def get_description(cls):
74         """Return a single line string with the class description
75
76         This function will retrieve the class docstring and return the first
77         line, or 'None' if it's empty.
78         """
79         return cls.__doc__.splitlines()[0] if cls.__doc__ else str(None)
80
81     def _push_to_outputs(self, keys, values):
82         return dict(zip(keys, values))
83
84     def _change_obj_to_dict(self, obj):
85         dic = {}
86         for k, v in vars(obj).items():
87             try:
88                 vars(v)
89             except TypeError:
90                 dic[k] = v
91         return dic