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