add option to connect to non-standard ssh port
[yardstick.git] / yardstick / benchmark / scenarios / networking / networkcapacity.py
1 ##############################################################################\r
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.\r
3 #\r
4 # All rights reserved. This program and the accompanying materials\r
5 # are made available under the terms of the Apache License, Version 2.0\r
6 # which accompanies this distribution, and is available at\r
7 # http://www.apache.org/licenses/LICENSE-2.0\r
8 ##############################################################################\r
9 import pkg_resources\r
10 import logging\r
11 import json\r
12 \r
13 import yardstick.ssh as ssh\r
14 from yardstick.benchmark.scenarios import base\r
15 \r
16 LOG = logging.getLogger(__name__)\r
17 \r
18 \r
19 class NetworkCapacity(base.Scenario):\r
20     """Measure Network capacity and scale.\r
21 \r
22     This scenario reads network status including number of connections,\r
23     number of frames sent/received.\r
24     """\r
25     __scenario_type__ = "NetworkCapacity"\r
26     TARGET_SCRIPT = "networkcapacity.bash"\r
27 \r
28     def __init__(self, scenario_cfg, context_cfg):\r
29         self.scenario_cfg = scenario_cfg\r
30         self.context_cfg = context_cfg\r
31         self.setup_done = False\r
32 \r
33     def setup(self):\r
34         """scenario setup"""\r
35         self.target_script = pkg_resources.resource_filename(\r
36             "yardstick.benchmark.scenarios.networking",\r
37             NetworkCapacity.TARGET_SCRIPT)\r
38 \r
39         host = self.context_cfg['host']\r
40         if host is None:\r
41             raise RuntimeError('No right node.please check the configuration')\r
42         host_user = host.get('user', 'ubuntu')\r
43         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)\r
44         host_ip = host.get('ip', None)\r
45         host_pwd = host.get('password', None)\r
46 \r
47         LOG.debug("user:%s, host:%s", host_user, host_ip)\r
48         self.client = ssh.SSH(host_user, host_ip, password=host_pwd,\r
49                               port=ssh_port)\r
50         self.client.wait(timeout=600)\r
51 \r
52         # copy script to host\r
53         self.client.run("cat > ~/networkcapacity.sh",\r
54                         stdin=open(self.target_script, 'rb'))\r
55 \r
56         self.setup_done = True\r
57 \r
58     def run(self, result):\r
59         """execute the benchmark"""\r
60 \r
61         if not self.setup_done:\r
62             self.setup()\r
63 \r
64         cmd = "sudo bash networkcapacity.sh"\r
65 \r
66         LOG.debug("Executing command: %s", cmd)\r
67         status, stdout, stderr = self.client.execute(cmd)\r
68         if status:\r
69             raise RuntimeError(stderr)\r
70 \r
71         result.update(json.loads(stdout))\r