有没有'没有' Gatling生成的.csv文件中的数据

时间:2017-02-28 10:48:28

标签: java scala csv gatling

我可以从下面的代码中生成 csv文件,但是里面没有数据(它的数据与带有相同查询的sqlYog交叉检查)。它只打印" Asset Ids"在一列中并抛出ClassCastException。任何人都可以帮我解决这个问题吗?感谢。

enter image description here

这是我的剧本。

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)
}

错误:

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

1 个答案:

答案 0 :(得分:1)

stacktrace中的第一行显示错误发生的位置:generateCsv.scala中的第57行。由于您的代码示例没有行号且短于57行,我假设这是行

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

您尝试将assetID设置为String,但根据错误消息,它是Long类型的值。您可以使用toString方法将Long转换为String:

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