Skip to content

Commit 4ef338b

Browse files
committed
multiple profiles for astf mode
Signed-off-by: Leo Ma <[email protected]>
1 parent 2852230 commit 4ef338b

File tree

3 files changed

+139
-24
lines changed

3 files changed

+139
-24
lines changed

src/main/java/com/cisco/trex/stateful/AstfProfile.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* ASTF profile
1616
*/
1717
public class AstfProfile {
18+
1819
private static final Logger LOGGER = LoggerFactory.getLogger(AstfProfile.class);
1920

2021
private static final String L7_PRECENT = "l7_percent";
@@ -25,6 +26,7 @@ public class AstfProfile {
2526
private AstfGlobalInfo astfServerGlobalInfo;
2627
private List<AstfTemplate> astfTemplateList;
2728
private List<AstfCapInfo> astfCapInfoList;
29+
private String profileId;
2830

2931
/**
3032
* construct
@@ -141,6 +143,8 @@ public AstfProfile(AstfIpGen defaultIpGen, AstfGlobalInfo astfClientGlobalInfo,
141143
astfTemplateList.add(template);
142144
}
143145
}
146+
147+
this.profileId = "astf_profile_" + System.currentTimeMillis();
144148
}
145149

146150
/**
@@ -190,7 +194,8 @@ public void printStats() {
190194
JsonObject tempJson = astfTemplateList.get(i).toJson();
191195
int clientProgInd = tempJson.getAsJsonObject("client_template").get("program_index").getAsInt();
192196
int serverProgInd = tempJson.getAsJsonObject("server_template").get("program_index").getAsInt();
193-
int totalBytes = AstfTemplateBase.getTotalSendBytes(clientProgInd) + AstfTemplateBase.getTotalSendBytes(serverProgInd);
197+
int totalBytes = AstfTemplateBase.getTotalSendBytes(clientProgInd)
198+
+ AstfTemplateBase.getTotalSendBytes(serverProgInd);
194199

195200
float tempCps = tempJson.getAsJsonObject("client_template").get("cps").getAsFloat();
196201
float tempBps = totalBytes * tempCps * 8;
@@ -201,4 +206,8 @@ public void printStats() {
201206
LOGGER.info("total for all templates - cps:{} bps:{}", totalCps, totalBps);
202207
}
203208

209+
public String getProfileId() {
210+
return this.profileId;
211+
}
212+
204213
}

src/main/java/com/cisco/trex/stateful/TRexAstfClient.java

Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
import java.security.MessageDigest;
55
import java.security.NoSuchAlgorithmException;
66
import java.util.HashMap;
7+
import java.util.List;
78
import java.util.Map;
89
import java.util.Map.Entry;
910
import java.util.Set;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.StreamSupport;
1013

11-
import com.cisco.trex.stateless.model.ApiVersionHandler;
1214
import org.apache.commons.lang.StringUtils;
1315

1416
import com.cisco.trex.ClientBase;
1517
import com.cisco.trex.stateless.exception.TRexConnectionException;
18+
import com.cisco.trex.stateless.model.ApiVersionHandler;
1619
import com.cisco.trex.stateless.model.PortStatus;
1720
import com.cisco.trex.stateless.model.TRexClientResult;
21+
import com.google.gson.JsonArray;
1822
import com.google.gson.JsonElement;
1923
import com.google.gson.JsonParser;
2024

@@ -68,6 +72,14 @@ private Map<String, Object> createPayload() {
6872
return payload;
6973
}
7074

75+
protected Map<String, Object> createPayload(String profileId) {
76+
Map<String, Object> payload = createPayload();
77+
if (profileId != null && !profileId.isEmpty()) {
78+
payload.put("profile_id", profileId);
79+
}
80+
return payload;
81+
}
82+
7183
private static String calculateMd5(String profile) {
7284
try {
7385
MessageDigest md = MessageDigest.getInstance("MD5");
@@ -83,7 +95,7 @@ private static String calculateMd5(String profile) {
8395
}
8496

8597
/**
86-
* start traffic on all ports on the last loaded profile
98+
* start traffic on all ports on loaded profile associated with default profile id
8799
*
88100
* @param clientMask
89101
* @param duration
@@ -94,7 +106,23 @@ private static String calculateMd5(String profile) {
94106
*/
95107
public void startTraffic(long clientMask, double duration, boolean ipv6,
96108
int latencyPps, int mult, boolean nc) {
97-
Map<String, Object> payload = createPayload();
109+
startTraffic("", clientMask, duration, ipv6, latencyPps, mult, nc);
110+
}
111+
112+
/**
113+
* start traffic on all ports on loaded profile associated with specified profile id
114+
*
115+
* @param profileId
116+
* @param clientMask
117+
* @param duration
118+
* @param ipv6
119+
* @param latencyPps
120+
* @param mult
121+
* @param nc
122+
*/
123+
public void startTraffic(String profileId, long clientMask, double duration, boolean ipv6,
124+
int latencyPps, int mult, boolean nc) {
125+
Map<String, Object> payload = createPayload(profileId);
98126
payload.put("client_mask", clientMask);
99127
payload.put("duration", duration);
100128
payload.put("ipv6", ipv6);
@@ -124,13 +152,32 @@ public void startLatencyTraffic(long mask, int mult, String srcAddr, String dstA
124152
}
125153

126154
/**
127-
* Stop the active traffic
155+
* Stop the active traffic associated with default profile id
128156
*/
129157
public void stopTraffic() {
130-
Map<String, Object> payload = createPayload();
158+
stopTraffic("");
159+
}
160+
161+
/**
162+
* Stop the active traffic associated with specified profile id
163+
*
164+
* @param profileId
165+
*/
166+
public void stopTraffic(String profileId) {
167+
Map<String, Object> payload = createPayload(profileId);
131168
this.callMethod("stop", payload);
132169
}
133170

171+
/**
172+
* Stop all active traffic
173+
*/
174+
public void stopAllTraffic() {
175+
List<String> profileIds = getProfileIds();
176+
for (String profileId : profileIds) {
177+
stopTraffic(profileId);
178+
}
179+
}
180+
134181
/**
135182
* Stop active latency traffic
136183
*/
@@ -197,16 +244,27 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
197244

198245
/**
199246
* Load profile object as string and upload in fragments
200-
*
247+
*
201248
* @param profile
202249
*/
203250
public void loadProfile(String profile) {
251+
loadProfile(profile, "");
252+
}
253+
254+
/**
255+
* Load profile object as string and upload in fragments and associate it with specified profile
256+
* id
257+
*
258+
* @param profile
259+
* @param profileId
260+
*/
261+
public void loadProfile(String profileId, String profile) {
204262
int indexStart = 0;
205263
int fragmentLength = 1000; //shorter length the first time
206264
int totalLength = profile.length();
207265
while (totalLength > indexStart) {
208266
int indexEnd = indexStart + fragmentLength;
209-
Map<String, Object> payload = createPayload();
267+
Map<String, Object> payload = createPayload(profileId);
210268
if (indexStart == 0) { //is first fragment
211269
payload.put("frag_first", true);
212270
payload.put("total_size", totalLength);
@@ -224,28 +282,68 @@ public void loadProfile(String profile) {
224282
}
225283

226284
/**
227-
* clearProfile
285+
* clear profile on loaded state for default profile id
228286
*/
229287
public void clearProfile() {
230-
Map<String, Object> payload = createPayload();
288+
clearProfile("");
289+
}
290+
291+
/**
292+
* clear profile on loaded state for specified profile id
293+
*
294+
* @param profileId
295+
*/
296+
public void clearProfile(String profileId) {
297+
Map<String, Object> payload = createPayload(profileId);
231298
this.callMethod("profile_clear", payload);
232299
}
233-
300+
301+
/**
302+
* fetch all the associated profile ids
303+
*
304+
* @return profile id list
305+
*/
306+
public List<String> getProfileIds() {
307+
Map<String, Object> payload = createPayload();
308+
String json = callMethod("get_profile_list", payload);
309+
JsonElement response = new JsonParser().parse(json);
310+
JsonArray ids = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonArray();
311+
return StreamSupport.stream(ids.spliterator(), false)
312+
.map(JsonElement::getAsString)
313+
.collect(Collectors.toList());
314+
}
315+
234316
/**
235-
* Get Counter Metadata
317+
* Get Counter Metadata of profile associated default profile id
236318
* Not finished, needs to return counter object
237319
*/
238320
public void getCounterMetadata() {
239-
Map<String, Object> payload = createPayload();
321+
getCounterMetadata("");
322+
}
323+
324+
/**
325+
* Get Counter Metadata of profile associated specified profile id
326+
* Not finished, needs to return counter object
327+
*/
328+
public void getCounterMetadata(String profileId) {
329+
Map<String, Object> payload = createPayload(profileId);
240330
this.callMethod("get_counter_desc", payload);
241331
}
242332

243333
/**
244-
* Get Astf Counters
334+
* Get Astf Counters of profile associated default profile id
245335
* Not finished, needs to return counter object
246336
*/
247337
public void getAstfCounters() {
248-
Map<String, Object> payload = this.createPayload();
338+
getAstfCounters("");
339+
}
340+
341+
/**
342+
* Get Astf Counters of profile associated specified profile id
343+
* Not finished, needs to return counter object
344+
*/
345+
public void getAstfCounters(String profileId) {
346+
Map<String, Object> payload = this.createPayload(profileId);
249347
this.callMethod("get_counter_values", payload);
250348
}
251349

@@ -260,7 +358,7 @@ public void getLatencyStats() {
260358

261359
/**
262360
* Get Version
263-
*
361+
*
264362
* @return version
265363
*/
266364
public String getVersion() {

src/main/java/com/cisco/trex/stateless/TRexClient.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ public PGIdStatsRPCResult getPgidStats(int[] ids) {
271271
}
272272

273273
public void startTraffic(int portIndex, double duration, boolean force, Map<String, Object> mul, int coreMask) {
274-
List<String> profileIds = getProfileIds(portIndex);
275-
for (String profileId : profileIds) {
276-
startTraffic(portIndex, profileId, duration, force, mul, coreMask);
277-
}
274+
startTraffic(portIndex, "", duration, force, mul, coreMask);
278275
}
279276

280277
public void startTraffic(int portIndex, String profileId, double duration, boolean force, Map<String, Object> mul,
@@ -287,6 +284,13 @@ public void startTraffic(int portIndex, String profileId, double duration, boole
287284
callMethod("start_traffic", payload);
288285
}
289286

287+
public void startAllTraffic(int portIndex, double duration, boolean force, Map<String, Object> mul, int coreMask) {
288+
List<String> profileIds = getProfileIds(portIndex);
289+
for (String profileId : profileIds) {
290+
startTraffic(portIndex, profileId, duration, force, mul, coreMask);
291+
}
292+
}
293+
290294
public void pauseTraffic(int portIndex) {
291295
Map<String, Object> payload = createPayload(portIndex);
292296
callMethod("pause_traffic", payload);
@@ -645,17 +649,21 @@ private EthernetPacket buildIcmpV4Request(String srcMac, String dstMac, String s
645649
}
646650

647651
public void stopTraffic(int portIndex) {
648-
List<String> profileIds = getProfileIds(portIndex);
649-
for (String profileId : profileIds) {
650-
stopTraffic(portIndex, profileId);
651-
}
652+
stopTraffic(portIndex, "");
652653
}
653654

654655
public void stopTraffic(int portIndex, String profileId) {
655656
Map<String, Object> payload = createPayload(portIndex, profileId);
656657
callMethod("stop_traffic", payload);
657658
}
658659

660+
public void stopAllTraffic(int portIndex) {
661+
List<String> profileIds = getProfileIds(portIndex);
662+
for (String profileId : profileIds) {
663+
stopTraffic(portIndex, profileId);
664+
}
665+
}
666+
659667
public Map<String, Ipv6Node> scanIPv6(int portIndex) throws ServiceModeRequiredException {
660668
return new IPv6NeighborDiscoveryService(this).scan(portIndex, 10, null, null);
661669
}

0 commit comments

Comments
 (0)