0

I am able to generate csv file from below code but there is no data in it (it has data cross-checked with sqlYog with same query). It only prints "Asset Ids" in one column and throws ClassCastException. Can anyone help me to solve this? Thanks.

enter image description here

Here is my script.

import java.io._
import au.com.bytecode.opencsv.CSVWriter
import io.gatling.jdbc.Predef._
import scala.collection.mutable.ArrayBuffer

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class generateCsv extends Simulation {

    val username = System.getProperty("username", "user1")
    val password = System.getProperty("password", "user1")
    val testUrl = System.getProperty("testUrl", "https://someurl.net")

    val csvFileLocation = "D:/myDir/myfile.csv"
    val dbQuery = jdbcFeeder("jdbc:mysql://10.10.40.10:3306/master", "admin", "admin", "SELECT ID_ assetID FROM TableName")

    var recordCount = dbQuery.records.length

    val out = new BufferedWriter(new FileWriter(csvFileLocation))
    val writer = new CSVWriter(out)
    var Ids = new Array[String](recordCount)

    Ids(0) = "Asset Ids"

    var i = 1

    val httpProtocol = http
        .baseURL(testUrl)
        .inferHtmlResources()
        .acceptHeader("""*/*""")
        .basicAuth(username, password)
        .acceptEncodingHeader("""gzip, deflate""")
        .acceptLanguageHeader("""en-US,en;q=0.8""")
        .connection("""keep-alive""")
        .userAgentHeader("""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36""")

    val scn = scenario("Database Query")
    .repeat (recordCount) {
        feed(dbQuery)
        .exec { session =>
                Ids(i)  = session("assetID").as[String]
                        i = i + 1
                        session
            }
    }
    after{
                writer.writeNext(Ids)
                writer.close()
            }

    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

Error:

84057 [GatlingSystem-akka.actor.default-dispatcher-6] ERROR i.g.c.a.b.SessionHookBuilder$$anonfun$build$1$$anon$1 - 'sessionHook-2' crashed on session Session(Database Query,8451783577631963111-0,Map(b1075cf5-e3c8-47d6-96bd-ff74284b4e7c -> 4, timestamp.b1075cf5-e3c8-47d6-96bd-ff74284b4e7c -> 1488276658897, assetID -> 6),1488276658892,0,KO,List(ExitOnCompleteLoopBlock(b1075cf5-e3c8-47d6-96bd-ff74284b4e7c)),<function1>), forwarding to the next one
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    at com.abc.gatling.generateCsv$$anonfun$2.apply(generateCsv.scala:57) ~[na:na]
    at com.abc.gatling.generateCsv$$anonfun$2.apply(generateCsv.scala:56) ~[na:na]
    at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:35) ~[gatling-core-2.1.7.jar:2.1.7]
    at io.gatling.core.action.Failable$class.execute(Actions.scala:71) ~[gatling-core-2.1.7.jar:2.1.7]
    at io.gatling.core.action.SessionHook.execute(SessionHook.scala:28) ~[gatling-core-2.1.7.jar:2.1.7]
    at io.gatling.core.action.Action$$anonfun$receive$1.applyOrElse(Actions.scala:29) ~[gatling-core-2.1.7.jar:2.1.7]
    at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:171) ~[scala-library-2.11.7.jar:na]
    at akka.actor.Actor$class.aroundReceive(Actor.scala:467) ~[akka-actor_2.11-2.3.12.jar:na]
    at io.gatling.core.akka.BaseActor.aroundReceive(BaseActor.scala:22) ~[gatling-core-2.1.7.jar:2.1.7]
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) ~[akka-actor_2.11-2.3.12.jar:na]
    at akka.actor.ActorCell.invoke(ActorCell.scala:487) ~[akka-actor_2.11-2.3.12.jar:na]
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238) ~[akka-actor_2.11-2.3.12.jar:na]
    at akka.dispatch.Mailbox.run(Mailbox.scala:220) ~[akka-actor_2.11-2.3.12.jar:na]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.12.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.7.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.7.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.7.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.7.jar:na]
84072 [GatlingSystem-akka.actor.default-dispatcher-6] INFO  i.g.core.controller.Controller - End user #8451783577631963111-0
84179 [main] INFO  i.g.c.result.reader.FileDataReader - Collected List(D:\gatling-charts-highcharts-bundle-2.1.7\results\generateCsv-1488276658757\simulation.log) from generateCsv-1488276658757
Peter
  • 855
  • 2
  • 15
  • 35

1 Answers1

1

The first line in the stacktrace shows where the error is happening: Line 57 in generateCsv.scala. Since your code example doesn't have line numbers and is shorter than 57 lines, I assume this is the line

Ids(i)  = session("assetID").as[String]

where you try to case a the assetID to String, but which is a value of type Long according to the error message. You could use the toString method to transform the Long into a String:

Ids(i)  = session("assetID").as[Long].toString
Harald Gliebe
  • 7,236
  • 3
  • 33
  • 38
  • Yes that line number is correct one. I have replaced .as[String] to .toString. in output i get this for all the lines: SessionAttribute(Session(Database Query,949002059540811697-0,Map(4a27dcfa-986d-41c6-81b9-c965ff0f6c40 -> 0, timestamp.4a27dcfa-986d-41c6-81b9-c965ff0f6c40 -> 1488284879536, assetID -> -1),1488284879532,0,OK,List(ExitOnCompleteLoopBlock(4a27dcfa-986d-41c6-81b9-c965ff0f6c40)),),assetID) – Peter Feb 28 '17 at 12:33
  • could you try Ids(i) = session("assetID").as[Long].toString – Harald Gliebe Feb 28 '17 at 12:34
  • Thanks a lot Harald. That worked. Please edit your answer. will accept it. in excel file data prints in Row, do you know how do I change it to column? – Peter Feb 28 '17 at 12:40
  • Thanks, you can use writeAll instead of writeNext – Harald Gliebe Feb 28 '17 at 12:48
  • Tried but Error says, type mismatch; found : Array[String] required: java.util.List[Array[String]] writer.writeAll(Ids) – Peter Feb 28 '17 at 12:56
  • Sorry, Data prints in Column (Horizontally). I need to print data in rows (vertically) – Peter Feb 28 '17 at 13:50
  • 1
    You can convert to the Ids to the right java type for the writeAll method as follows: `import collection.JavaConverters._` and then replace the writeNext call with `writer.writeAll(Ids.iterator.toBuffer[String].map(s => Array(s)).asJava)` – Harald Gliebe Feb 28 '17 at 14:06