Reconf tests: Fix async measurements
[csit.git] / resources / tools / trex / trex_stateless_stop.py
1 #!/usr/bin/python3
2
3 # Copyright (c) 2019 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """This script uses T-REX stateless API to drive t-rex instance.
17
18 Requirements:
19 - T-REX: https://github.com/cisco-system-traffic-generator/trex-core
20  - compiled and running T-REX process (eg. ./t-rex-64 -i)
21  - trex.stl.api library
22 - Script must be executed on a node with T-REX instance
23
24 Functionality:
25 1. Stop any running traffic
26 2. Optionally restore reference counter values.
27 3. Return conter differences.
28 """
29
30 import argparse
31 from collections import OrderedDict  # Needed to parse xstats representation.
32 import sys
33 import json
34
35 sys.path.insert(0, "/opt/trex-core-2.54/scripts/automation/"+\
36                    "trex_control_plane/interactive/")
37 from trex.stl.api import *
38
39
40 def main():
41     """Stop traffic if any is running. Report xstats."""
42     parser = argparse.ArgumentParser()
43     parser.add_argument(
44         "--xstat0", type=str, default="", help="Reference xstat object if any.")
45     parser.add_argument(
46         "--xstat1", type=str, default="", help="Reference xstat object if any.")
47     args = parser.parse_args()
48
49     client = STLClient()
50     try:
51         # connect to server
52         client.connect()
53
54         client.acquire(force=True)
55         # TODO: Support unidirection.
56         client.stop(ports=[0, 1])
57
58         # Read the stats after the test,
59         # we need to update values before the last trial started.
60         if args.xstat0:
61             snapshot = eval(args.xstat0)
62             client.ports[0].get_xstats().reference_stats = snapshot
63         if args.xstat1:
64             snapshot = eval(args.xstat1)
65             client.ports[1].get_xstats().reference_stats = snapshot
66         # Now we can call the official method to get differences.
67         xstats0 = client.get_xstats(0)
68         xstats1 = client.get_xstats(1)
69
70     # If STLError happens, let the script fail with stack trace.
71     finally:
72         client.disconnect()
73
74     print("##### statistics port 0 #####")
75     print(json.dumps(xstats0, indent=4, separators=(',', ': ')))
76     print("##### statistics port 1 #####")
77     print(json.dumps(xstats1, indent=4, separators=(',', ': ')))
78
79     tx_0, rx_0 = xstats0["tx_good_packets"], xstats0["rx_good_packets"]
80     tx_1, rx_1 = xstats1["tx_good_packets"], xstats1["rx_good_packets"]
81     lost_a, lost_b = tx_0 - rx_1, tx_1 - rx_0
82
83     print("\npackets lost from 0 --> 1:   {0} pkts".format(lost_a))
84     print("packets lost from 1 --> 0:   {0} pkts".format(lost_b))
85
86     total_rcvd, total_sent = rx_0 + rx_1, tx_0 + tx_1
87     total_lost = total_sent - total_rcvd
88     # TODO: Add latency.
89     print(
90         "rate='unknown', totalReceived={rec}, totalSent={sen}, frameLoss={los},"
91         " latencyStream0(usec)=-1/-1/-1, latencyStream1(usec)=-1/-1/-1,"
92         " targetDuration='manual'".format(
93             rec=total_rcvd, sen=total_sent, los=total_lost))
94
95 if __name__ == "__main__":
96     main()