f670612c72436e9c52670342c692e4655584a817
[vswitchperf.git] / tools / pkt_gen / testcenter / testcenter.py
1 # Copyright 2015 Spirent Communications.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 Code to integrate Spirent TestCenter with the vsperf test framework.
17
18 Provides a model for Spirent TestCenter as a test tool for implementing
19 various performance tests of a virtual switch.
20 """
21
22 from __future__ import print_function
23
24 from tools.pkt_gen import trafficgen
25 from core.results.results_constants import ResultsConstants
26 import subprocess
27 import os
28 import csv
29 from conf import settings
30
31
32 class TestCenter(trafficgen.ITrafficGenerator):
33     """
34     Spirent TestCenter
35     """
36     def connect(self):
37         """
38         Do nothing.
39         """
40         return self
41
42     def disconnect(self):
43         """
44         Do nothing.
45         """
46         pass
47
48     def send_burst_traffic(self, traffic=None, numpkts=100, duration=20, framerate=100):
49         """
50         Do nothing.
51         """
52         return None
53
54     def send_cont_traffic(self, traffic=None, duration=30, framerate=0,
55                           multistream=False):
56         """
57         Do nothing.
58         """
59         return None
60
61     def send_rfc2544_throughput(self, traffic=None, trials=3, duration=20,
62                                 lossrate=0.0, multistream=False):
63         """
64         Send traffic per RFC2544 throughput test specifications.
65         """
66         verbose = False
67
68         args = [settings.getValue("TRAFFICGEN_STC_PYTHON2_PATH"),
69                 os.path.join(settings.getValue("TRAFFICGEN_STC_TESTCENTER_PATH"),
70                              settings.getValue("TRAFFICGEN_STC_RFC2544_TPUT_TEST_FILE_NAME")),
71                 "--lab_server_addr",
72                 settings.getValue("TRAFFICGEN_STC_LAB_SERVER_ADDR"),
73                 "--license_server_addr",
74                 settings.getValue("TRAFFICGEN_STC_LICENSE_SERVER_ADDR"),
75                 "--east_chassis_addr",
76                 settings.getValue("TRAFFICGEN_STC_EAST_CHASSIS_ADDR"),
77                 "--east_slot_num",
78                 settings.getValue("TRAFFICGEN_STC_EAST_SLOT_NUM"),
79                 "--east_port_num",
80                 settings.getValue("TRAFFICGEN_STC_EAST_PORT_NUM"),
81                 "--west_chassis_addr",
82                 settings.getValue("TRAFFICGEN_STC_WEST_CHASSIS_ADDR"),
83                 "--west_slot_num",
84                 settings.getValue("TRAFFICGEN_STC_WEST_SLOT_NUM"),
85                 "--west_port_num",
86                 settings.getValue("TRAFFICGEN_STC_WEST_PORT_NUM"),
87                 "--test_session_name",
88                 settings.getValue("TRAFFICGEN_STC_TEST_SESSION_NAME"),
89                 "--results_dir",
90                 settings.getValue("TRAFFICGEN_STC_RESULTS_DIR"),
91                 "--csv_results_file_prefix",
92                 settings.getValue("TRAFFICGEN_STC_CSV_RESULTS_FILE_PREFIX"),
93                 "--num_trials",
94                 settings.getValue("TRAFFICGEN_STC_NUMBER_OF_TRIALS"),
95                 "--trial_duration_sec",
96                 settings.getValue("TRAFFICGEN_STC_TRIAL_DURATION_SEC"),
97                 "--traffic_pattern",
98                 settings.getValue("TRAFFICGEN_STC_TRAFFIC_PATTERN"),
99                 "--search_mode",
100                 settings.getValue("TRAFFICGEN_STC_SEARCH_MODE"),
101                 "--learning_mode",
102                 settings.getValue("TRAFFICGEN_STC_LEARNING_MODE"),
103                 "--rate_lower_limit_pct",
104                 settings.getValue("TRAFFICGEN_STC_RATE_LOWER_LIMIT_PCT"),
105                 "--rate_upper_limit_pct",
106                 settings.getValue("TRAFFICGEN_STC_RATE_UPPER_LIMIT_PCT"),
107                 "--rate_initial_pct",
108                 settings.getValue("TRAFFICGEN_STC_RATE_INITIAL_PCT"),
109                 "--rate_step_pct",
110                 settings.getValue("TRAFFICGEN_STC_RATE_STEP_PCT"),
111                 "--resolution_pct",
112                 settings.getValue("TRAFFICGEN_STC_RESOLUTION_PCT"),
113                 "--frame_size_list",
114                 settings.getValue("TRAFFICGEN_STC_FRAME_SIZE"),
115                 "--acceptable_frame_loss_pct",
116                 settings.getValue("TRAFFICGEN_STC_ACCEPTABLE_FRAME_LOSS_PCT"),
117                 "--east_intf_addr",
118                 settings.getValue("TRAFFICGEN_STC_EAST_INTF_ADDR"),
119                 "--east_intf_gateway_addr",
120                 settings.getValue("TRAFFICGEN_STC_EAST_INTF_GATEWAY_ADDR"),
121                 "--west_intf_addr",
122                 settings.getValue("TRAFFICGEN_STC_WEST_INTF_ADDR"),
123                 "--west_intf_gateway_addr",
124                 settings.getValue("TRAFFICGEN_STC_WEST_INTF_GATEWAY_ADDR")]
125         if settings.getValue("TRAFFICGEN_STC_VERBOSE") is "True":
126             args.append("--verbose")
127             verbose = True
128             print("Arguments used to call test: %s" % args)
129
130         subprocess.check_call(map(os.path.expanduser, args))
131
132         file = os.path.join(settings.getValue("TRAFFICGEN_STC_RESULTS_DIR"),
133                             settings.getValue("TRAFFICGEN_STC_CSV_RESULTS_FILE_PREFIX") + ".csv")
134         if verbose:
135             print("file: %s" % file)
136
137         result = {}
138
139         with open(file, "r") as csvfile:
140             csvreader = csv.DictReader(csvfile)
141             for row in csvreader:
142                 print("Row: %s" % row)
143                 result[ResultsConstants.TX_RATE_FPS] = 0.0
144                 result[ResultsConstants.THROUGHPUT_RX_FPS] = 0.0
145                 result[ResultsConstants.TX_RATE_MBPS] = 0.0
146                 result[ResultsConstants.THROUGHPUT_RX_MBPS] = 0.0
147                 result[ResultsConstants.TX_RATE_PERCENT] = float(row["OfferedLoad(%)"])
148                 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(row["Throughput(%)"])
149                 result[ResultsConstants.MIN_LATENCY_NS] = float(row["MinimumLatency(us)"]) * 1000
150                 result[ResultsConstants.MAX_LATENCY_NS] = float(row["MaximumLatency(us)"]) * 1000
151                 result[ResultsConstants.AVG_LATENCY_NS] = float(row["AverageLatency(us)"]) * 1000
152         return result
153
154 if __name__ == '__main__':
155     TRAFFIC = {
156         'l3': {
157             'proto': 'tcp',
158             'srcip': '1.1.1.1',
159             'dstip': '90.90.90.90',
160         },
161     }
162
163     with TestCenter() as dev:
164         print(dev.send_rfc2544_throughput(traffic=TRAFFIC))