summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/agent/perf/netperf.py
blob: fab1fc1104d44b1fa6444719686c29d59416a2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
# -*- coding: utf8 -*-
# author:
# date: 2015-09-15
# see license for license details
import time
import subprocess
import vstf.common.constants as cst
import vstf.common.decorator as deco
from vstf.common import perfmark as mark
from vstf.common.utils import kill_by_name, my_popen

import logging

LOG = logging.getLogger(__name__)


class Netperf(object):
    def __init__(self):
        self._send_processes = []
        self._islat = False
        self._typemap = {
            "tcp_lat": "TCP_STREAM",
            "tcp_bw": "TCP_STREAM",
            "udp_lat": "UDP_STREAM",
            "udp_bw": "UDP_STREAM",
        }

    @deco.check("protocol", choices=cst.PROTOCOLS)
    @deco.check("namespace", defaults=None)
    @deco.check("dst")
    @deco.check("time", defaults=0)
    @deco.check("size", defaults=64)
    @deco.check("threads", defaults=1)
    def send_start(self, **kwargs):
        threads = kwargs.pop('threads')
        kwargs['buf'] = cst.SOCKET_BUF
        if kwargs['protocol'] in ['tcp_lat', 'udp_lat']:
            self._islat = True
        else:
            kwargs['time'] = 0

        cmd = self.format_send_start(**kwargs)
        LOG.info("cmd:%s", cmd)

        for _ in range(threads):
            process = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            self._send_processes.append(process)
        time.sleep(0.5)
        for process in self._send_processes:
            ret = process.poll()
            if ret is None:
                ret = 0
                error_str = "start netperf send success"
            else:
                error_str = "start netperf send failed, %s" % (str(kwargs))
                process.wait()
                self._send_processes.remove(process)

        return ret, error_str

    def send_stop(self, **kwargs):
        LOG.info("send_stop")
        results = []
        ret = 0
        for process in self._send_processes:
            poll = process.poll()
            if poll is None:
                if not self._islat:
                    process.kill()
                    read = "process is stopped by killed"
                else:
                    ret = process.wait()
                    read = process.stdout.read()
                    read = self._parse_data(read)
                results.append((ret, read))
        self._send_processes = []
        self._islat = False
        return results

    @staticmethod
    def _parse_data(data):
        buf = data.splitlines()
        data = buf[2].strip().split(',')
        result = {
            mark.minLatency: float(data[0]),
            mark.avgLatency: float(data[1]),
            mark.maxLatency: float(data[2])
        }
        return result

    @deco.namespace()
    def format_send_start(self, **kwargs):
        #       cmd = "netperf -H %(dst_ip)s -t %(type)s -l %(time)s -- -m %(pkt_size)s "
        cmd = "netperf -H %(dst_ip)s -t %(type)s -l %(time)s  " \
              "-- -m %(pkt_size)s -s %(buf)s -S %(buf)s -o  MIN_LATENCY,MEAN_LATENCY,MAX_LATENCY"
        context = {
            'dst_ip': kwargs['dst'][0]['ip'],
            'type': self._typemap[kwargs['protocol']],
            'time': kwargs['time'],
            'pkt_size': kwargs['size'],
            'buf': kwargs['buf'],
        }
        cmd = cmd % context
        return cmd

    @deco.namespace()
    def format_receive_start(self, **kwargs):
        cmd = 'netserver'
        return cmd

    @deco.check("namespace")
    def receive_start(self, **kwargs):

        cmd = self.format_receive_start(**kwargs)
        LOG.info("cmd:%s", cmd)

        process = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(0.5)
        ret = process.poll()
        if ret:
            error_str = "start netserver failed, %s" % (str(kwargs))
        else:
            ret = 0
            error_str = "start netserver success"

        return ret, error_str

    def receive_stop(self, **kwargs):
        LOG.info("receive_stop")
        ret = 0
        kill_by_name('netserver')
        time.sleep(0.5)
        error_str = "stop netserver success"
        return ret, error_str

    def clean(self):
        self.send_stop()
        self.receive_stop()
        return True

    def force_clean(self):
        LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
        kill_by_name('netserver')
        kill_by_name('netperf')
        self._send_processes = []
        self._receive_processes = []
        return True


def unit_test():
    perf = Netperf()
    ret = perf.receive_start(namespace='receive')
    print "*********receive_start***********"
    print ret
    send = {
        "namespace": "send",
        "protocol": "udp_lat",
        "dst": [
            {"ip": "192.168.1.102"}
        ],
        "size": 64,
        "threads": 1,
        "time": 10,
    }
    print perf.send_start(**send)
    print perf._send_processes
    time.sleep(10)
    print perf.send_stop()
    print perf.receive_stop()


if __name__ == "__main__":
    from vstf.common.log import setup_logging

    setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-netperf.log", clevel=logging.DEBUG)
    unit_test()