xref: /aosp_15_r20/external/autotest/client/tests/netpipe/netpipe.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1*9c5db199SXin Liimport os, time, logging
2*9c5db199SXin Lifrom autotest_lib.client.bin import test, utils
3*9c5db199SXin Lifrom autotest_lib.client.common_lib import error
4*9c5db199SXin Li
5*9c5db199SXin Li
6*9c5db199SXin Liclass netpipe(test.test):
7*9c5db199SXin Li    version = 1
8*9c5db199SXin Li    NP_FILE = '/tmp/np.out'
9*9c5db199SXin Li
10*9c5db199SXin Li    # http://www.scl.ameslab.gov/netpipe/code/NetPIPE-3.7.1.tar.gz
11*9c5db199SXin Li    def setup(self, tarball='NetPIPE-3.7.1.tar.gz'):
12*9c5db199SXin Li        tarball = utils.unmap_url(self.bindir, tarball, self.tmpdir)
13*9c5db199SXin Li        utils.extract_tarball_to_dir(tarball, self.srcdir)
14*9c5db199SXin Li        os.chdir(self.srcdir)
15*9c5db199SXin Li        utils.system('patch -p1 < ../makefile.patch')
16*9c5db199SXin Li        utils.make()
17*9c5db199SXin Li
18*9c5db199SXin Li
19*9c5db199SXin Li    def initialize(self):
20*9c5db199SXin Li        self.job.require_gcc()
21*9c5db199SXin Li
22*9c5db199SXin Li        # Add arguments later
23*9c5db199SXin Li        self.server_path = '%s %%s' % os.path.join(self.srcdir, 'NPtcp')
24*9c5db199SXin Li        # Add server_ip and arguments later
25*9c5db199SXin Li        base_path = os.path.join(self.srcdir, 'NPtcp -h')
26*9c5db199SXin Li        self.client_path = '%s %%s -o %s %%s' % (base_path, self.NP_FILE)
27*9c5db199SXin Li        self.results = []
28*9c5db199SXin Li
29*9c5db199SXin Li    def cleanup(self):
30*9c5db199SXin Li        # Just in case...
31*9c5db199SXin Li        utils.system('killall -9 NPtcp', ignore_status=True)
32*9c5db199SXin Li
33*9c5db199SXin Li
34*9c5db199SXin Li    def run_once(self, server_ip, client_ip, role, bidirectional=False,
35*9c5db199SXin Li                 buffer_size=None, upper_bound=None,
36*9c5db199SXin Li                 perturbation_size=3):
37*9c5db199SXin Li        self.role = role
38*9c5db199SXin Li
39*9c5db199SXin Li        # Any arguments used must be the same on both the client and the server
40*9c5db199SXin Li        args = '-p %d ' % perturbation_size
41*9c5db199SXin Li        if bidirectional:
42*9c5db199SXin Li            args += '-2 '
43*9c5db199SXin Li        if buffer_size:
44*9c5db199SXin Li            args += '-b %d ' % buffer_size
45*9c5db199SXin Li        if upper_bound:
46*9c5db199SXin Li            args += '-u %d ' % upper_bound
47*9c5db199SXin Li
48*9c5db199SXin Li
49*9c5db199SXin Li        server_tag = server_ip + '#netpipe-server'
50*9c5db199SXin Li        client_tag = client_ip + '#netpipe-client'
51*9c5db199SXin Li        all = [server_tag, client_tag]
52*9c5db199SXin Li
53*9c5db199SXin Li        if role == 'server':
54*9c5db199SXin Li            # Wait up to ten minutes for both to reach this point.
55*9c5db199SXin Li            self.job.barrier(server_tag, 'start', 600).rendezvous(*all)
56*9c5db199SXin Li            self.server_start(args)
57*9c5db199SXin Li            # Both the client and server should be closed so just to make
58*9c5db199SXin Li            # sure they are both at the same point wait at most five minutes.
59*9c5db199SXin Li            self.job.barrier(server_tag, 'stop', 300).rendezvous(*all)
60*9c5db199SXin Li        elif role == 'client':
61*9c5db199SXin Li            # Wait up to ten minutes for the server to start
62*9c5db199SXin Li            self.job.barrier(client_tag, 'start', 600).rendezvous(*all)
63*9c5db199SXin Li            # Sleep 10 seconds to make sure the server is started
64*9c5db199SXin Li            time.sleep(10)
65*9c5db199SXin Li            self.client(server_ip, args)
66*9c5db199SXin Li            # Wait up to five minutes for the server to also reach this point
67*9c5db199SXin Li            self.job.barrier(client_tag, 'stop', 300).rendezvous(*all)
68*9c5db199SXin Li        else:
69*9c5db199SXin Li            raise error.TestError('invalid role specified')
70*9c5db199SXin Li
71*9c5db199SXin Li
72*9c5db199SXin Li    def server_start(self, args):
73*9c5db199SXin Li        cmd = self.server_path % args
74*9c5db199SXin Li        self.results.append(utils.system_output(cmd, retain_output=True))
75*9c5db199SXin Li
76*9c5db199SXin Li
77*9c5db199SXin Li    def client(self, server_ip, args):
78*9c5db199SXin Li        cmd = self.client_path % (server_ip, args)
79*9c5db199SXin Li
80*9c5db199SXin Li        try:
81*9c5db199SXin Li            # We don't care about the actual output since the important stuff
82*9c5db199SXin Li            # goes to self.NP_FILE
83*9c5db199SXin Li            utils.system(cmd)
84*9c5db199SXin Li        except error.CmdError, e:
85*9c5db199SXin Li            """ Catch errors due to timeout, but raise others
86*9c5db199SXin Li            The actual error string is:
87*9c5db199SXin Li              "Command did not complete within %d seconds"
88*9c5db199SXin Li            called in function join_bg_job in the file common_lib/utils.py
89*9c5db199SXin Li
90*9c5db199SXin Li            Looking for 'within' is probably not the best way to do this but
91*9c5db199SXin Li            works for now"""
92*9c5db199SXin Li
93*9c5db199SXin Li            if ('within' in e.additional_text
94*9c5db199SXin Li                or 'non-zero' in e.additional_text):
95*9c5db199SXin Li                logging.debug(e.additional_text)
96*9c5db199SXin Li            else:
97*9c5db199SXin Li                raise
98*9c5db199SXin Li
99*9c5db199SXin Li
100*9c5db199SXin Li    def postprocess(self):
101*9c5db199SXin Li        if self.role == 'client':
102*9c5db199SXin Li            try:
103*9c5db199SXin Li                output = open(self.NP_FILE)
104*9c5db199SXin Li                for line in output.readlines():
105*9c5db199SXin Li                    buff, bandwidth, latency = line.split()
106*9c5db199SXin Li                    attr = {'buffer_size':buff}
107*9c5db199SXin Li                    keyval = {'bandwidth':bandwidth, 'latency':latency}
108*9c5db199SXin Li                    self.write_iteration_keyval(attr, keyval)
109*9c5db199SXin Li            finally:
110*9c5db199SXin Li                output.close()
111*9c5db199SXin Li                os.remove(self.NP_FILE)
112