I found my questions post to this group are always got answered very promptly. Thx for the quick response. Now I have another questions:
I have a scenario that need to send the httprequest first, then parsing the response html: When the html string regex( “”"“content{(\S+)}”"") matchs, it will extract some other substrings StringB from StringA; other wise it will get regex(""“data-object-id=”(\S+)"""") matching from html.
.feed(csv(“messagepool.csv”))
.exec(http("/message")
.get("/message/"+ “${messageID}”)
.check(status.is(200))
.check(regex("""“content{(\S+)}”"").saveAs(“fullJavascriptObj”))
.check(regex(""“data-object-id=”(\S+)"""").saveAs(“objectID”))
.exec((session:Session) => {
doIfOrElse(session.get(“fullJavascriptObj”) != null){
println(session.get(“fullJavascriptObj”))
// then do more string manipulations on fullJavascriptObj
}{
println(session.get(“objectID”))
}
But when I run this, if the response html does hot have regex("""“content{(\S+)}”"") matches, it will fail and won’t go on to extract the next one “objectID”. Is there a way to let it do all the extraction? I am using gatling 2.0
.exec((session:Session) => {
doIfOrElse(session.get(“fullJavascriptObj”) != null){
println(session.get(“fullJavascriptObj”))
// then do more string manipulations on fullJavascriptObj
}{
println(session.get(“objectID”))
}
Is this even compiling ?
You can’t use DSL element inside a .exec() statement as these exec() are made to execute a Scala function.
.exec((session:Session) => {
// I suppose that the type is String … change it if it’s not the case.
val fullJavascriptObj = session.getAttributeAsOptionString
if(fullJavascriptObj.isDefined)){
println(fullJavascriptObj.get)
// then do more string manipulations on fullJavascriptObj
} else {
println(session.get(“objectID”))
}
//It is important that the function returns the new Session, it’s a immutable object.
session
}
Thx for the response. My main problem is that I need to check more response body after this. Since I can’t use DSL inside of exec, what should I do: the logic is like below:
.exec((session:Session) => {
// I suppose that the type is String … change it if it’s not the case.
val fullJavascriptObj = session.getAttributeAsOptionString
if(fullJavascriptObj.isDefined)){
println(fullJavascriptObj.get)
// then do more string manipulations on fullJavascriptObj
} else {
// i need to get more checks on the response result
Not exactly. You sure can store the full body String into the Session, but beware that it might increase the chance it survives micro GC and gets promoted to old, thus increase full GC.
You’d better extract exactly what you want asap with transform.
If you use regex, beware that it performs substring that, until recent JDK updates, keeps a reference to the original char array, so you might have to make a new String(String).