Skip to content

Instantly share code, notes, and snippets.

@pbezpal
Last active Sep 9, 2020
Embed
What would you like to do?
gatling.io
import BasicLoad.start
import Requests.usersLogin
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.scenario.Simulation
import io.socket.client.{Ack, IO, Socket}
import io.socket.emitter.Emitter
import org.json.JSONObject
import scala.concurrent.duration._
import scala.language.postfixOps
import play.api.libs.json._
class ServerLoad extends Simulation{
val socketUrl = "https://localhost:8080"
val optConnect = new IO.Options()
var status_ack: Boolean = false
optConnect.forceNew = false
optConnect.timeout = 5000
val socket = IO.socket(socketUrl, optConnect)
var start: Long = 0L
var jsonContacts = new JSONObject()
val users = csv( "users.csv" )
.queue
val httpConf = http.wsBaseUrl("wss://localhost:8080")
val scn = scenario("UsersLogin")
.feed(users)
.exec(
ws( "Send user-agent" ).connect( "/socket.io/?EIO=3&transport=websocket" )
.headers(
Map(
"User-Agent" -> "${device}"
)
)
)
.exec(
session_login => {
println(session_login)
val obj = new JSONObject()
status_ack = false
obj.put( "login", session_login( "login" ).as[String] )
obj.put( "password", session_login( "password" ).as[String] )
obj.put( "APILevel", session_login( "level" ).as[String] )
val printListenerLogin: Emitter.Listener = new Emitter.Listener {
override def call(args: AnyRef*): Unit = {
//println( "Login time: " + ((System.nanoTime - start) / 1000000) )
}
}
socket.on( Socket.EVENT_CONNECT, printListenerLogin )
socket.connect()
socket.emit( "login", obj, new Ack {
override def call(args: AnyRef*): Unit = {
val ackData = args.apply( 0 ).toString()
status_ack = (Json.parse( ackData ) \\ "status") ( 0 ).toString().contains( """ok""" )
if( ! status_ack){
exec( ws( "Close connect" ).close )
}else{
//println("Send emit get-user-info")
socket.emit("get-user-info", "", new Ack {
override def call(info: AnyRef*): Unit = {
//println( info )
}
})
//println("Send emit get-user-params")
socket.emit( "get-user-params", "", new Ack {
override def call(params: AnyRef*): Unit = {
//println( params )
}
} )
jsonContacts.put( "type", "global" )
socket.emit( "get-contacts", jsonContacts, new Ack {
override def call(args: AnyRef*): Unit = {
val getGlobalContacts = args.apply(0).toString
if ( (Json.parse( getGlobalContacts ) \\ "cid").length > 0 ){
println( (Json.parse( getGlobalContacts ) \\ "cid").length.toString())//( ).toString() )
}
}
} )
jsonContacts.put( "type", "local" )
socket.emit( "get-contacts", jsonContacts, new Ack {
override def call(args: AnyRef*): Unit = {
/*val getLocalContacts = args.apply(0).toString
println( (Json.parse( getLocalContacts ) \\ "cid") (0).toString())*/
}
} )
socket.emit( "get-sessions", "", new Ack {
override def call(params: AnyRef*): Unit = {
//println( params )
}
} )
jsonContacts.put( "count", "50" )
socket.emit( "get-chats", jsonContacts, new Ack {
override def call(args: AnyRef*): Unit = {
//print( args )
}
} )
}
}
}
)
session_login
}
)
.pause(15)
.exec(
session => {
socket.emit("logout")
session
}
)
.exec(
ws( "Close connect" ).close
)
setUp(scn.inject(rampUsers(5) during(10 seconds))
.protocols(httpConf))
.maxDuration(30 seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment