add option to connect to non-standard ssh port
[yardstick.git] / yardstick / benchmark / scenarios / compute / perf.py
index 62b4297..6c827ef 100644 (file)
@@ -14,7 +14,6 @@ import yardstick.ssh as ssh
 from yardstick.benchmark.scenarios import base
 
 LOG = logging.getLogger(__name__)
-LOG.setLevel(logging.DEBUG)
 
 
 class Perf(base.Scenario):
@@ -37,20 +36,24 @@ class Perf(base.Scenario):
 
     TARGET_SCRIPT = 'perf_benchmark.bash'
 
-    def __init__(self, context):
-        self.context = context
+    def __init__(self, scenario_cfg, context_cfg):
+        self.scenario_cfg = scenario_cfg
+        self.context_cfg = context_cfg
         self.setup_done = False
 
     def setup(self):
         """scenario setup"""
         self.target_script = pkg_resources.resource_filename(
             'yardstick.benchmark.scenarios.compute', Perf.TARGET_SCRIPT)
-        user = self.context.get('user', 'ubuntu')
-        host = self.context.get('host', None)
-        key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
-
-        LOG.debug("user:%s, host:%s", user, host)
-        self.client = ssh.SSH(user, host, key_filename=key_filename)
+        host = self.context_cfg['host']
+        user = host.get('user', 'ubuntu')
+        ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
+        ip = host.get('ip', None)
+        key_filename = host.get('key_filename', '~/.ssh/id_rsa')
+
+        LOG.info("user:%s, host:%s", user, ip)
+        self.client = ssh.SSH(user, ip, key_filename=key_filename,
+                              port=ssh_port)
         self.client.wait(timeout=600)
 
         # copy script to host
@@ -59,13 +62,13 @@ class Perf(base.Scenario):
 
         self.setup_done = True
 
-    def run(self, args):
+    def run(self, result):
         """execute the benchmark"""
 
         if not self.setup_done:
             self.setup()
 
-        options = args['options']
+        options = self.scenario_cfg['options']
         events = options.get('events', ['task-clock'])
 
         events_string = ""
@@ -73,7 +76,8 @@ class Perf(base.Scenario):
             events_string += event + " "
 
         # if run by a duration runner
-        duration_time = self.context.get("duration", None)
+        duration_time = self.scenario_cfg["runner"].get("duration", None) \
+            if "runner" in self.scenario_cfg else None
         # if run by an arithmetic runner
         arithmetic_time = options.get("duration", None)
         if duration_time:
@@ -97,43 +101,46 @@ class Perf(base.Scenario):
         if status:
             raise RuntimeError(stdout)
 
-        output = json.loads(stdout)
+        result.update(json.loads(stdout))
 
-        if "sla" in args:
-            metric = args['sla']['metric']
-            exp_val = args['sla']['expected_value']
-            smaller_than_exp = 'smaller_than_expected' in args['sla']
+        if "sla" in self.scenario_cfg:
+            metric = self.scenario_cfg['sla']['metric']
+            exp_val = self.scenario_cfg['sla']['expected_value']
+            smaller_than_exp = 'smaller_than_expected' \
+                               in self.scenario_cfg['sla']
 
-            if metric not in output:
+            if metric not in result:
                 assert False, "Metric (%s) not found." % metric
             else:
                 if smaller_than_exp:
-                    assert output[metric] < exp_val, "%s %d >= %d (sla)" \
-                        % (metric, output[metric], exp_val)
+                    assert result[metric] < exp_val, "%s %d >= %d (sla); " \
+                        % (metric, result[metric], exp_val)
                 else:
-                    assert output[metric] >= exp_val, "%s %d < %d (sla)" \
-                        % (metric, output[metric], exp_val)
-        return output
+                    assert result[metric] >= exp_val, "%s %d < %d (sla); " \
+                        % (metric, result[metric], exp_val)
 
 
 def _test():
     """internal test function"""
     key_filename = pkg_resources.resource_filename('yardstick.resources',
                                                    'files/yardstick_key')
-    ctx = {'host': '172.16.0.137',
-           'user': 'ubuntu',
-           'key_filename': key_filename
-           }
+    ctx = {
+        'host': {
+            'ip': '10.229.47.137',
+            'user': 'root',
+            'key_filename': key_filename
+        }
+    }
 
     logger = logging.getLogger('yardstick')
     logger.setLevel(logging.DEBUG)
 
-    p = Perf(ctx)
-
     options = {'load': True}
     args = {'options': options}
+    result = {}
 
-    result = p.run(args)
+    p = Perf(args, ctx)
+    p.run(result)
     print result
 
 if __name__ == '__main__':