Merge "NSB: fix port topology"
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen_dpdk.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE corporation 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 from __future__ import absolute_import
10 import pkg_resources
11 import logging
12 import time
13
14 import yardstick.ssh as ssh
15 import yardstick.common.utils as utils
16 from yardstick.benchmark.scenarios import base
17
18
19 LOG = logging.getLogger(__name__)
20
21
22 class PktgenDPDKLatency(base.Scenario):
23     """Execute pktgen-dpdk on one vm and execute testpmd on the other vm
24
25   Parameters
26     packetsize - packet size in bytes without the CRC
27         type:    int
28         unit:    bytes
29         default: 64
30     """
31     __scenario_type__ = "PktgenDPDKLatency"
32
33     PKTGEN_DPDK_SCRIPT = 'pktgen_dpdk_latency_benchmark.bash'
34     TESTPMD_SCRIPT = 'testpmd_fwd.bash'
35
36     def __init__(self, scenario_cfg, context_cfg):
37         self.scenario_cfg = scenario_cfg
38         self.context_cfg = context_cfg
39         self.setup_done = False
40
41     def setup(self):
42         """scenario setup"""
43         self.pktgen_dpdk_script = pkg_resources.resource_filename(
44             'yardstick.benchmark.scenarios.networking',
45             PktgenDPDKLatency.PKTGEN_DPDK_SCRIPT)
46         self.testpmd_script = pkg_resources.resource_filename(
47             'yardstick.benchmark.scenarios.networking',
48             PktgenDPDKLatency.TESTPMD_SCRIPT)
49         host = self.context_cfg['host']
50         target = self.context_cfg['target']
51         LOG.info("user:%s, target:%s", target['user'], target['ip'])
52         self.server = ssh.SSH.from_node(target, defaults={"user": "ubuntu"})
53         self.server.wait(timeout=600)
54
55         # copy script to host
56         self.server._put_file_shell(self.testpmd_script, '~/testpmd_fwd.sh')
57
58         LOG.info("user:%s, host:%s", host['user'], host['ip'])
59         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
60         self.client.wait(timeout=600)
61
62         # copy script to host
63         self.client._put_file_shell(
64             self.pktgen_dpdk_script, '~/pktgen_dpdk.sh')
65
66         self.setup_done = True
67         self.testpmd_args = ''
68         self.pktgen_args = []
69
70     def run(self, result):
71         """execute the benchmark"""
72
73         if not self.setup_done:
74             self.setup()
75
76         if not self.testpmd_args:
77             self.testpmd_args = utils.get_port_mac(self.client, 'eth2')
78
79         if not self.pktgen_args:
80             server_rev_mac = utils.get_port_mac(self.server, 'eth1')
81             server_send_mac = utils.get_port_mac(self.server, 'eth2')
82             client_src_ip = utils.get_port_ip(self.client, 'eth1')
83             client_dst_ip = utils.get_port_ip(self.client, 'eth2')
84
85             self.pktgen_args = [client_src_ip, client_dst_ip,
86                                 server_rev_mac, server_send_mac]
87
88         options = self.scenario_cfg['options']
89         packetsize = options.get("packetsize", 64)
90         rate = options.get("rate", 100)
91
92         cmd = "screen sudo -E bash ~/testpmd_fwd.sh %s " % (self.testpmd_args)
93         LOG.debug("Executing command: %s", cmd)
94         self.server.send_command(cmd)
95
96         time.sleep(1)
97
98         cmd = "screen sudo -E bash ~/pktgen_dpdk.sh %s %s %s %s %s %s" % \
99             (self.pktgen_args[0], self.pktgen_args[1], self.pktgen_args[2],
100              self.pktgen_args[3], rate, packetsize)
101         LOG.debug("Executing command: %s", cmd)
102         self.client.send_command(cmd)
103
104         # wait for finishing test
105         time.sleep(1)
106
107         cmd = r"""\
108 cat ~/result.log -vT \
109 |awk '{match($0,/\[8;40H +[0-9]+/)} \
110 {print substr($0,RSTART,RLENGTH)}' \
111 |grep -v ^$ |awk '{if ($2 != 0) print $2}'\
112 """
113         client_status, client_stdout, client_stderr = self.client.execute(cmd)
114
115         if client_status:
116             raise RuntimeError(client_stderr)
117
118         avg_latency = 0
119         if client_stdout:
120             latency_list = client_stdout.split('\n')[0:-2]
121             LOG.info("10 samples of latency: %s", latency_list)
122             latency_sum = 0
123             for i in latency_list:
124                 latency_sum += int(i)
125             avg_latency = latency_sum / len(latency_list)
126
127         result.update({"avg_latency": avg_latency})
128
129         if avg_latency and "sla" in self.scenario_cfg:
130             sla_max_latency = int(self.scenario_cfg["sla"]["max_latency"])
131             LOG.info("avg_latency : %d ", avg_latency)
132             LOG.info("sla_max_latency: %d", sla_max_latency)
133             debug_info = "avg_latency %d > sla_max_latency %d" \
134                 % (avg_latency, sla_max_latency)
135             assert avg_latency <= sla_max_latency, debug_info