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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 """This script uses T-REX stateless API to drive t-rex instance.
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
25 1. Stop any running traffic
26 2. Optionally restore reference counter values.
27 3. Return conter differences.
34 from collections import OrderedDict # Needed to parse xstats representation.
37 0, u"/opt/trex-core-2.73/scripts/automation/trex_control_plane/interactive/"
39 from trex.stl.api import *
43 """Stop traffic if any is running. Report xstats."""
44 parser = argparse.ArgumentParser()
46 u"--xstat0", type=str, default=u"",
47 help=u"Reference xstat object if any."
50 u"--xstat1", type=str, default=u"",
51 help=u"Reference xstat object if any."
53 args = parser.parse_args()
60 client.acquire(force=True)
61 # TODO: Support unidirection.
62 client.stop(ports=[0, 1])
64 # Read the stats after the test,
65 # we need to update values before the last trial started.
67 snapshot = eval(args.xstat0)
68 client.ports[0].get_xstats().reference_stats = snapshot
70 snapshot = eval(args.xstat1)
71 client.ports[1].get_xstats().reference_stats = snapshot
72 # Now we can call the official method to get differences.
73 xstats0 = client.get_xstats(0)
74 xstats1 = client.get_xstats(1)
76 # If STLError happens, let the script fail with stack trace.
80 print(u"##### statistics port 0 #####")
81 print(json.dumps(xstats0, indent=4, separators=(u",", u": ")))
82 print(u"##### statistics port 1 #####")
83 print(json.dumps(xstats1, indent=4, separators=(u",", u": ")))
85 tx_0, rx_0 = xstats0[u"tx_good_packets"], xstats0[u"rx_good_packets"]
86 tx_1, rx_1 = xstats1[u"tx_good_packets"], xstats1[u"rx_good_packets"]
87 lost_a, lost_b = tx_0 - rx_1, tx_1 - rx_0
89 print(f"\npackets lost from 0 --> 1: {lost_a} pkts")
90 print(f"packets lost from 1 --> 0: {lost_b} pkts")
92 total_rcvd, total_sent = rx_0 + rx_1, tx_0 + tx_1
93 total_lost = total_sent - total_rcvd
96 f"rate='unknown', totalReceived={total_rcvd}, totalSent={total_sent}, "
97 f"frameLoss={total_lost}, latencyStream0(usec)=-1/-1/-1, "
98 f"latencyStream1(usec)=-1/-1/-1, targetDuration='manual'"
102 if __name__ == u"__main__":