Merge "Support Storage Capacity Test"
[yardstick.git] / yardstick / benchmark / scenarios / networking / netperf.py
1 ##############################################################################
2 # Copyright (c) 2015 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 # bulk data test and req/rsp test are supported
10 import pkg_resources
11 import logging
12 import json
13
14 import yardstick.ssh as ssh
15 from yardstick.benchmark.scenarios import base
16
17 LOG = logging.getLogger(__name__)
18
19
20 class Netperf(base.Scenario):
21     """Execute netperf between two hosts
22
23   Parameters
24     testname - to specify the test you wish to perform.
25     the valid testnames are TCP_STREAM, TCP_RR, UDP_STREAM, UDP_RR
26         type:    string
27         unit:    na
28         default: TCP_STREAM
29     send_msg_size - value set the local send size to value bytes.
30         type:    int
31         unit:    bytes
32         default: na
33     recv_msg_size - setting the receive size for the remote system.
34         type:    int
35         unit:    bytes
36         default: na
37     req_rsp_size - set the request and/or response sizes based on sizespec.
38         type:    string
39         unit:    na
40         default: na
41     duration - duration of the test
42         type:    int
43         unit:    seconds
44         default: 20
45
46     read link below for more netperf args description:
47     http://www.netperf.org/netperf/training/Netperf.html
48     """
49     __scenario_type__ = "Netperf"
50
51     TARGET_SCRIPT = 'netperf_benchmark.bash'
52
53     def __init__(self, scenario_cfg, context_cfg):
54         self.scenario_cfg = scenario_cfg
55         self.context_cfg = context_cfg
56         self.setup_done = False
57
58     def setup(self):
59         '''scenario setup'''
60         self.target_script = pkg_resources.resource_filename(
61             'yardstick.benchmark.scenarios.networking',
62             Netperf.TARGET_SCRIPT)
63         host = self.context_cfg['host']
64         host_user = host.get('user', 'ubuntu')
65         host_ip = host.get('ip', None)
66         host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
67         target = self.context_cfg['target']
68         target_user = target.get('user', 'ubuntu')
69         target_ip = target.get('ip', None)
70         target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
71
72         # netserver start automatically during the vm boot
73         LOG.info("user:%s, target:%s", target_user, target_ip)
74         self.server = ssh.SSH(target_user, target_ip,
75                               key_filename=target_key_filename)
76         self.server.wait(timeout=600)
77
78         LOG.info("user:%s, host:%s", host_user, host_ip)
79         self.client = ssh.SSH(host_user, host_ip,
80                               key_filename=host_key_filename)
81         self.client.wait(timeout=600)
82
83         # copy script to host
84         self.client.run("cat > ~/netperf.sh",
85                         stdin=open(self.target_script, "rb"))
86
87         self.setup_done = True
88
89     def run(self, result):
90         """execute the benchmark"""
91
92         if not self.setup_done:
93             self.setup()
94
95         # get global options
96         ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
97         options = self.scenario_cfg['options']
98         testname = options.get("testname", 'TCP_STREAM')
99         duration_time = self.scenario_cfg["runner"].get("duration", None) \
100             if "runner" in self.scenario_cfg else None
101         arithmetic_time = options.get("duration", None)
102         if duration_time:
103             testlen = duration_time
104         elif arithmetic_time:
105             testlen = arithmetic_time
106         else:
107             testlen = 20
108
109         cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
110
111         # get test specific options
112         default_args = "-O 'THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY'"
113         cmd_args += " -- %s" % default_args
114         option_pair_list = [("send_msg_size", "-m"),
115                             ("recv_msg_size", "-M"),
116                             ("req_rsp_size", "-r")]
117         for option_pair in option_pair_list:
118             if option_pair[0] in options:
119                 cmd_args += " %s %s" % (option_pair[1],
120                                         options[option_pair[0]])
121
122         cmd = "sudo bash netperf.sh %s" % (cmd_args)
123         LOG.debug("Executing command: %s", cmd)
124         status, stdout, stderr = self.client.execute(cmd)
125
126         if status:
127             raise RuntimeError(stderr)
128
129         result.update(json.loads(stdout))
130
131         if result['mean_latency'] == '':
132             raise RuntimeError(stdout)
133
134         # sla check
135         mean_latency = float(result['mean_latency'])
136         if "sla" in self.scenario_cfg:
137             sla_max_mean_latency = int(
138                 self.scenario_cfg["sla"]["mean_latency"])
139
140             assert mean_latency <= sla_max_mean_latency, \
141                 "mean_latency %f > sla_max_mean_latency(%f); " % \
142                 (mean_latency, sla_max_mean_latency)
143
144
145 def _test():
146     '''internal test function'''
147     key_filename = pkg_resources.resource_filename("yardstick.resources",
148                                                    "files/yardstick_key")
149     ctx = {
150         "host": {
151             "ip": "10.229.47.137",
152             "user": "root",
153             "key_filename": key_filename
154         },
155         "target": {
156             "ip": "10.229.47.137",
157             "user": "root",
158             "key_filename": key_filename,
159             "ipaddr": "10.229.47.137"
160         }
161     }
162
163     logger = logging.getLogger("yardstick")
164     logger.setLevel(logging.DEBUG)
165
166     options = {
167         "testname": 'TCP_STREAM'
168     }
169
170     args = {"options": options}
171     result = {}
172
173     netperf = Netperf(args, ctx)
174     netperf.run(result)
175     print result
176
177 if __name__ == '__main__':
178     _test()