-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtestTypeNetPerf.py
152 lines (122 loc) · 4.84 KB
/
testTypeNetPerf.py
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
from dataclasses import dataclass
from typing import Any
from typing import Optional
import task
import tftbase
from task import ClientTask
from task import ServerTask
from task import TaskOperation
from testSettings import TestSettings
from testType import TestTypeHandler
from tftbase import BaseOutput
from tftbase import FlowTestOutput
from tftbase import TestType
NETPERF_SERVER_EXE = "netserver"
NETPERF_CLIENT_EXE = "netperf"
def netperf_parse(testname: Any, data: str) -> dict[str, float]:
# Let's accept both the testname as string and as TestType enum.
s_testname = str(testname)
if s_testname.startswith("TestType."):
s_testname = s_testname[len("TestType.") :]
if s_testname.startswith("NETPERF_"):
s_testname = s_testname[len("NETPERF_") :]
if s_testname == "TCP_RR":
headers = [
"Socket Send Bytes",
"Size Receive Bytes",
"Request Size Bytes",
"Response Size Bytes",
"Elapsed Time Seconds",
"Transaction Rate Per Second",
]
elif s_testname == "TCP_STREAM":
headers = [
"Receive Socket Size Bytes",
"Send Socket Size Bytes",
"Send Message Size Bytes",
"Elapsed Time Seconds",
"Throughput 10^6bits/sec",
]
else:
raise TypeError(f'invalid testname "{testname}"')
lines = data.split("\n")
values: Optional[list[float]] = None
if len(lines) >= 7:
slist = [s.strip() for s in lines[6].split()]
try:
values = [float(s) for s in slist]
except ValueError:
pass
if not values or len(values) != len(headers):
raise ValueError("Cannot parse netperf output for tcp-stream: {repr(data)}")
return dict(zip(headers, values))
@dataclass(frozen=True)
class TestTypeHandlerNetPerf(TestTypeHandler):
def _create_server_client(self, ts: TestSettings) -> tuple[ServerTask, ClientTask]:
s = NetPerfServer(ts)
c = NetPerfClient(ts, server=s)
return (s, c)
TestTypeHandler.register_test_type(TestTypeHandlerNetPerf(TestType.NETPERF_TCP_STREAM))
TestTypeHandler.register_test_type(TestTypeHandlerNetPerf(TestType.NETPERF_TCP_RR))
class NetPerfServer(task.ServerTask):
def get_template_args(self) -> dict[str, str | list[str]]:
extra_args: dict[str, str | list[str]] = {}
if self.exec_persistent:
extra_args["args"] = [NETPERF_SERVER_EXE, "-p", f"{self.port}", "-N"]
return {
**super().get_template_args(),
**extra_args,
}
def _create_setup_operation_get_thread_action_cmd(self) -> str:
return f"{NETPERF_SERVER_EXE} -p {self.port} -N"
def _create_setup_operation_get_cancel_action_cmd(self) -> str:
return f"killall {NETPERF_SERVER_EXE}"
class NetPerfClient(task.ClientTask):
def _create_task_operation(self) -> TaskOperation:
assert not self.reverse
server_ip = self.get_target_ip()
if self.test_type == TestType.NETPERF_TCP_STREAM:
cmd = f"{NETPERF_CLIENT_EXE} -H {server_ip} -p {self.port} -t TCP_STREAM -l {self.get_duration()}"
else:
cmd = f"{NETPERF_CLIENT_EXE} -H {server_ip} -p {self.port} -t TCP_RR -l {self.get_duration()}"
def _thread_action() -> BaseOutput:
self.ts.clmo_barrier.wait()
r = self.run_oc_exec(cmd)
self.ts.event_client_finished.set()
success_result = False
parsed_data: dict[str, float] = {}
bitrate_gbps = tftbase.Bitrate.NA
if r.success:
data = r.out
try:
parsed_data = netperf_parse(self.test_type, data)
except ValueError:
pass
else:
success_result = True
if success_result:
if self.test_type == TestType.NETPERF_TCP_STREAM:
try:
x = float(parsed_data["Throughput 10^6bits/sec"])
except Exception:
success_result = False
else:
bitrate_gbps = tftbase.Bitrate(tx=x / 1000.0)
else:
try:
x = float(parsed_data["Transaction Rate Per Second"])
except Exception:
success_result = False
else:
bitrate_gbps = tftbase.Bitrate(tx=x / 1000.0)
return FlowTestOutput(
success=success_result,
tft_metadata=self.ts.get_test_metadata(),
command=cmd,
result=parsed_data,
bitrate_gbps=bitrate_gbps,
)
return TaskOperation(
log_name=self.log_name,
thread_action=_thread_action,
)