Passing parameter - number of users - scala

I'm using gatling in linux terminal. When I pass parameter like described in github I get error:
value users is not a member of Integer
This is my code:
package mypackage
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.http.Headers.Names._
import scala.concurrent.duration._
import bootstrap._
import assertions._
import util.Random
class MySimulation extends Simulation {
val usersCount = Integer.getInteger("users", 1)
val links = csv("links.csv").random
val httpProtocol = http
.baseURL("http://mywebsite.com:8080/")
.acceptCharsetHeader("ISO-8859-1,utf-8;q=0.7,*;q=0.7")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3")
.disableFollowRedirect
val headers_1 = Map(
"Keep-Alive" -> "115")
val headers_3 = Map(
"Keep-Alive" -> "115",
"Content-Type" -> "application/x-www-form-urlencoded")
val scn = scenario("big project benchmark")
.repeat(50) {
feed(links)
.exec(
http("request_1")
.get("${pageUri}")
.headers(headers_1)).pause(1000 millisecond)
}
setUp(scn.inject(ramp(usersCount users) over (30 seconds)))
.protocols(httpProtocol)
.assertions(global.successfulRequests.percent.is(100), details("request_1").responseTime.max.lessThan(1000))
I start this in terminal using:
JAVA_OPTS="-Dusers=300" ./gatling.sh -s mypackage.mySimulation -on testing -sd test1
Please, be patient, because I'm totally new to scala and gatling. Thanks for any help.

The problem comes from the usersCount users part of the setUp.
In Scala, this is interpreted as usersCount.users which, in our case does not exist since Integer does not have a users method.
I think (but I'm not sure since I cannot test it right now), that you should make usersCount an Int like so: val usersCount: Int = Integer.getInteger("users", 1).toInt.
Hope this helps!
PS: The reason you should convert Integer to Int is because of implicit conversions. This is a really powerful feature of Scala.
PPS: The wiki documentation was valid for Gatling 1.X, it will be updated accordingly for Gatling 2.X

Related

Compilation problem between HttpRoutes[RIO[E, *]] and HttpRoutes[zio.Task]

Trying to compile this small ZIO friendly Tapir/Http4s Endpoint description
import io.circe.generic.auto._
import org.http4s.HttpRoutes
import sttp.tapir.json.circe
import sttp.tapir.ztapir._
import sttp.tapir.server.http4s.ztapir._
import sttp.tapir.endpoint
import zio.RIO
import zio.interop.catz._
case class HealthReplyDTO(message: String)
final class HealthEndpointZTapir[E]() {
private val prefixPath = "/health"
val healthOkReply = HealthReplyDTO("OK")
private val routeDescription: ZEndpoint[Unit, Unit, HealthReplyDTO] =
endpoint.get.in(prefixPath).out(circe.jsonBody[HealthReplyDTO]).description("Health Endpoint")
val route: HttpRoutes[RIO[E, *]]
= routeDescription.toRoutes( _ => RIO.succeed(healthOkReply))
}
and keep getting this on the last line.
Type mismatch. Required: HttpRoutes[RIO[E, *]], found: HttpRoutes[zio.Task]
A Task is a sub type of RIO so this should work fine right? Or am I missing something here.
A bit of a noob to this world, so some help would be much appreciated.
Task is indeed a subtype of RIO[R, *], but HttpRoutes from http4s is invariant, and hence the error message.
The .toRoutes method returns HttpRoutes[Task] as a consequence of integrating with http4s, which doesn't have typed errors. In general, http4s can throw any type of exceptions when setting up the server and handling the routes, so we need to work with Task.
I finally ended up doing something like this.
The compiler needs a bit of help in the last line, where implicits proved to be not enough. The ztapir part of tapir is fixed in ZIO effect type Task so that was not usable in my case
import io.circe.generic.auto._
import org.http4s.HttpRoutes
import sttp.tapir.json.circe
import sttp.tapir.server.http4s._
import sttp.tapir.endpoint
import zio.RIO
import zio.interop.catz._
import sttp.tapir._
final class HealthEndpointTapir[E]() extends TapirHttp4sServer {
private val prefixPath = "/health"
val healthOkReply: HealthReplyDTO = HealthReplyDTO("OK")
implicit val customServerOptions: Http4sServerOptions[RIO[E, *]] = Http4sServerOptions
.default[RIO[E, *]]
private val routeDescription: Endpoint[Unit, Unit, HealthReplyDTO, Any] =
endpoint.get.in(prefixPath).out(circe.jsonBody[HealthReplyDTO]).description("Health Endpoint")
val route: HttpRoutes[RIO[E, *]]
= RichHttp4sHttpEndpoint[Unit, Unit, HealthReplyDTO, RIO[E, *]
(routeDescription).toRoutes( _ => RIO.succeed(Right(healthOkReply)))
}

Gatling exec with session

I need to make a request in Gatling, in which I'm able to access session items (without the expression language). I need to do this, because I want to inject data into a ByteArrayBody request from a csv feeder. To demonstrate my problem, I have a small example (without the actual need of the session).
The following scenario runs fine:
val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
exec(http("Http Test test").get("http://google.de/"))
}
But that one doesn't (I get the exception There were no requests sent during the simulation, reports won't be generated):
val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
exec(session => {
http("Http Test test").get("http://google.de/")
session
})
}
I run my simulations in IntelliJ (which worked fine so far) and in the following (here minimized) simulation file:
package test.scala
import java.text.SimpleDateFormat
import java.util.Date
import io.gatling.core.Predef._
import io.gatling.core.body.ByteArrayBody
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import org.slf4j.LoggerFactory
import test.scala.TerminalTesterRequest.url
import test.scala.requests._
import test.scala.util.CharsetConverter
import scala.concurrent.duration._
import scala.language.postfixOps
class MySimulation extends Simulation {
//base URL (actually this URL is different, but it's not important)
val ecmsServerUri = "http://0.0.0.0"
//base Protocol
val httpProtocol: HttpProtocolBuilder = http
.baseUrl(ecmsServerUri)
.inferHtmlResources(BlackList(""".*\.js""", """.*\.css""", """.*\.gif""", """.*\.jpeg""", """.*\.jpg""", """.*\.ico""", """.*\.woff""", """.*\.(t|o)tf""", """.*\.png"""), WhiteList())
.acceptHeader("*/*")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en,en-US;q=0.7,de-DE;q=0.3")
.userAgentHeader("Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.8762)")
val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
exec(session => {
http("Http Test test").get("http://google.de/")
session
})
}
setUp(
scnBase.inject(constantUsersPerSec(1) during(1 seconds)).protocols(httpProtocol)
).maxDuration(5 minutes)
}
How can I run an exec request with the information of the session (or at least the data from the feeder)? I'm using Gatling 3.1.1
Build whatever you need in a function and put the result in the session, then refer that value in the actual request
val feeder = csv("foo.csv")
scenario("Test scenario")
.feed(feeder)
.exec(buildPostData)
.exec(http("Http Test test")
.post(createApiURL)
.body(ByteArrayBody("${postData}"))
.check(status.is(200))
)
def buildPostData: Expression[Session] = session => {
val postData: Array[Byte] =
... // getting values from csv record: session("csvHeader").as[String]
session.set("postData", postData)
}

Authentication Issue & Check Regex Issue Using Gatling

Want to Perform Load testing on Salesforce Platform. But it seems there is an error in authentication as well as regex part.
I am using Gatling testing tool & scala programming here.
If you guide me how to do load testing on salesforce that would be plus.
This is my code:
package default
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.core.Predef._
class requestUser extends Simulation {
val httpProtocol = http.baseURL("http://login.salesforce.com")
val Test = exec(http("Login")
.get("/")
.basicAuth("abc#gmail.com", "Password+Token")
.formParam("sessionId","*******")
.formParam("orgId","***********")
)
.exec(http("acc")
.get("/path")
.basicAuth("abc#gmail.com", "Password+Token")
.check(status.is(200))
**.check(css("#content"))
.check(regex("""<div id ="header">Choose a Username</div>"""))
.check(regex("""<td class = "datacell">Jaipur</td>"""))**
)
.exec(http("home")
.post("/home/home.jsp")
.formParam("Post", "Test from Gatling")
.check(regex())
)
var scn = scenario("scn").exec(Test)
setUp(
scn.inject(rampUsers(1) over (10 seconds))
).protocols(httpProtocol)
}

Gatling Get web service

I have tried to create a simple Gatling Script mentioned below,
package computerdatabase.advanced
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import scala.util.matching.Regex
import scala.concurrent.duration._
class getSampleTest extends Simulation{
val httpProtocol = http
.baseURL("https://xyz.com")
.header("Content-Type","application/json")
.header("Accept"," application/json ")
.header("Accept-Charset","utf-8n")
.acceptLanguageHeader("en-us","en;q=0.5")
.acceptEncodingHeader("gzip", "deflate")
.connection("keep-alive")
val scn = scenario("XYZ")
.group("XYZ Group") {
exec(http("XYZ-PAge").get("/profile/services").check(status.is(200)))
}
setUp(scn.inject(
rampUsersPerSec(1) to(10) during(5),
constantUsersPerSec(10) during(5)
).protocols(httpProtocol))
}
but i am getting an error saying that -->
value header is not a member of io.gatling.http.config.httpProtocolBuilder
may be a semicolon is missing before'value header'
.header("Content-Type","application/json")
No, this is not the compiler error message you get with such code (this is the error you got with the first tentative you posted on the Gatling mailing list).
Here, you get "too many arguments for method acceptLanguageHeader" (and acceptEncodingHeader) as those take only one parameter:
.acceptLanguageHeader("en-us, en;q=0.5")
.acceptEncodingHeader("gzip, deflate")

Modularising scenarios to run in sequence using Gatling

I'm trying to modularise a series of performance tests in Gatling.
Several of the tests execute the same initial path through the pages, so I thought that I could break them down into a series of scenarios, each scenario being a series of shared actions defined in its own file, and then a final Simulation definition that simply executed the specified scenarios one after the other.
What I then need is for my Simulation to run those scenarios in sequence; but I can only find how to run them either concurrently, or with a specified delay between each. Is there any Simulation setup option to run the defined scenarios one after the other without specifying an arbitrary delay?
EDIT
Currently, I have the following set of files:
homepageHeaders.scala
package advanced
object homepageHeaders {
val homepage_headers_1 = Map(
"Accept" -> """text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8""",
"If-Modified-Since" -> """Wed, 20 Mar 2013 15:36:31 +0000""",
"If-None-Match" -> """"1363793791""""
)
}
homepageChain.scala
package advanced
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import akka.util.duration._
import homepageHeaders._
object homepageChain {
val homepageChain =
//Homepage
exec(http("homepage")
.get("/")
.headers(homepageHeaders.homepage_headers_1)
)
}
pageHeaders.scala
package advanced
object pageHeaders {
val page_headers_1 = Map(
"Accept" -> """text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"""
)
}
pageChain.scala
package advanced
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import akka.util.duration._
import pageHeaders._
object pageChain {
val pageChain =
//Page Menu
exec(http("page request")
.get("/page1")
.headers(pageHeaders.page_headers_1)
)
}
pageSimulation.scala
package advanced
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import homepageChain._
import pageChain._
class pageSimulation extends Simulation {
val urlBase = "http://www.mytestsite.com"
val httpConf = httpConfig
.baseURL(urlBase)
.acceptHeader("image/png,image/*;q=0.8,*/*;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en-gb,en;q=0.5")
.userAgentHeader("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0")
val pageScenario = scenario("Bodycare Scenario")
.exec(homepageChain.homepageChain)
.exec(pageChain.pageChain)
setUp(
homepageScenario.users(1).protocolConfig(httpConf)
)
}
The error that I'm getting is:
14:40:50.800 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/advanced/pageChain.scala:13: not found: value exec
14:40:50.807 [ERROR] c.e.e.g.a.ZincCompiler$ - exec(http("page request")
14:40:50.808 [ERROR] c.e.e.g.a.ZincCompiler$ - ^
14:40:53.988 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/advanced/homepageChain.scala:13: not found: value exec
14:40:53.989 [ERROR] c.e.e.g.a.ZincCompiler$ - exec(http("homepage")
14:40:53.989 [ERROR] c.e.e.g.a.ZincCompiler$ - ^
14:41:17.274 [ERROR] c.e.e.g.a.ZincCompiler$ - two errors found
Exception in thread "main" Compilation failed
Clearly I'm missing something in my definition, but I just don't understand what it is
You can compose chains, not scenarios.
For example:
val login = exec(...)...
val foo = exec(...)...
val bar = exec(...)...
val scn1 = scenario("Scenario1").exec(login).exec(foo)
val scn2 = scenario("Scenario2").exec(login).exec(bar)
Clear?
You can cascade scenarios so that they execute in sequence as follows:
val allScenarios = scenario1.exec(scenario2).exec(scenario3)
Another option can be like this:
object GetAllRunDetails {
val getAllRunDetails = exec( ....)
}
object GetRunIdDetails{
val getRunIdDetails = exec( .... )
}
val scn1 = scenario("Scenario 1")
.exec(GetAllRunDetails.getAllRunDetails)
val scn2 = scenario("Scenario 2")
.exec(GetRunIdDetails.getRunIdDetails)
setUp(scn1.inject(atOnceUsers(1)),
scn2.inject(atOnceUsers(1)));
Thanks to Stephane, he also have given me a solution to create an object of multiple Chains and pass it to a scenario.
val login = exec(...)...
val foo = exec(...)...
val bar = exec(...)...
val scn_inpute = Seq(login, foo, bar)
val scn1 = scenario("Scenario1").exec(scn_inpute)
Since Gatling 3.4 Scenarios in the same simulation can now be executed sequentially with andThen.
setUp(
parent.inject(injectionProfile)
// child1 and child2 will start at the same time when last parent user will terminate
.andThen(
child1.inject(injectionProfile)
// grandChild will start when last child1 user will terminate
.andThen(grandChild.inject(injectionProfile)),
child2.inject(injectionProfile)
)
)
See official documentation.