add yardstick iruya 9.0.0 release notes
[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         options = self.scenario_cfg['options']
74         eth1 = options.get("eth1", "ens4")
75         eth2 = options.get("eth2", "ens5")
76         if not self.setup_done:
77             self.setup()
78
79         if not self.testpmd_args:
80             self.testpmd_args = utils.get_port_mac(self.client, eth2)
81
82         if not self.pktgen_args:
83             server_rev_mac = utils.get_port_mac(self.server, eth1)
84             server_send_mac = utils.get_port_mac(self.server, eth2)
85             client_src_ip = utils.get_port_ip(self.client, eth1)
86             client_dst_ip = utils.get_port_ip(self.client, eth2)
87
88             self.pktgen_args = [client_src_ip, client_dst_ip,
89                                 server_rev_mac, server_send_mac]
90
91         packetsize = options.get("packetsize", 64)
92         rate = options.get("rate", 100)
93
94         cmd = "screen sudo -E bash ~/testpmd_fwd.sh %s %s %s" % \
95             (self.testpmd_args, eth1, eth2)
96         LOG.debug("Executing command: %s", cmd)
97         self.server.send_command(cmd)
98
99         time.sleep(1)
100
101         cmd = "screen sudo -E bash ~/pktgen_dpdk.sh %s %s %s %s %s %s %s %s" % \
102             (self.pktgen_args[0], self.pktgen_args[1], self.pktgen_args[2],
103              self.pktgen_args[3], rate, packetsize, eth1, eth2)
104         LOG.debug("Executing command: %s", cmd)
105         self.client.send_command(cmd)
106
107         # wait for finishing test
108         time.sleep(60)
109
110         cmd = r"""\
111 cat ~/result.log -vT \
112 |awk '{match($0,/\[8;40H +[0-9]+/)} \
113 {print substr($0,RSTART,RLENGTH)}' \
114 |grep -v ^$ |awk '{if ($2 != 0) print $2}'\
115 """
116         _, client_stdout, _ = self.client.execute(cmd, raise_on_error=True)
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             self.verify_SLA(avg_latency <= sla_max_latency, debug_info)