Gatling 生成的 .csv 文件中有 'No' 数据
There is 'No' data in generated .csv file from Gatling
我能够从下面的代码生成 csv 文件,但其中没有数据(它使用相同查询与 sqlYog 交叉检查数据)。它只在一列中打印 "Asset Ids" 并抛出 ClassCastException。谁能帮我解决这个问题?谢谢
这是我的脚本。
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$$anon - '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.apply(generateCsv.scala:57) ~[na:na]
at com.abc.gatling.generateCsv$$anonfun.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.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
堆栈跟踪中的第一行显示错误发生的位置:generateCsv.scala 中的第 57 行。由于您的代码示例没有行号并且少于 57 行,我假设这是行
Ids(i) = session("assetID").as[String]
您尝试将 assetID 大小写为 String,但根据错误消息,它是 Long 类型的值。您可以使用 toString 方法将 Long 转换为字符串:
Ids(i) = session("assetID").as[Long].toString
我能够从下面的代码生成 csv 文件,但其中没有数据(它使用相同查询与 sqlYog 交叉检查数据)。它只在一列中打印 "Asset Ids" 并抛出 ClassCastException。谁能帮我解决这个问题?谢谢
这是我的脚本。
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$$anon - '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.apply(generateCsv.scala:57) ~[na:na]
at com.abc.gatling.generateCsv$$anonfun.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.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
堆栈跟踪中的第一行显示错误发生的位置:generateCsv.scala 中的第 57 行。由于您的代码示例没有行号并且少于 57 行,我假设这是行
Ids(i) = session("assetID").as[String]
您尝试将 assetID 大小写为 String,但根据错误消息,它是 Long 类型的值。您可以使用 toString 方法将 Long 转换为字符串:
Ids(i) = session("assetID").as[Long].toString