简体   繁体   中英

How to write a value from the response to a file in Gatling?

I have a script which creates new referenceId each time its executed. I used

.check(regex("orders.(.*?)\"").saveAs("referenceId")))

to extract the referenceId. Now, how can I write/append it to a file without impacting the script even if I run it as a load test?

I used session in .exec to write my value into a file. Here it is:

.exec( session => {
                scala.tools.nsc.io.File("../user-files/data/refenceId.csv").appendAll(session("refenceId").as[String]+"\n")
                session}
      )

You solution works, but...

First of all do not use anything (if you don't have to) from scala.tools.nsc.io package. It is internal package only for Scala compiler. It is not public API included in Scala runtime library (official Scaladoc). More about the topic here . Scala do not have any own abstraction for writing to file, hence one need to use normal java.io.File & co.

Secondly opening a file in each execution can (may not) slow down your load-test. It strongly depends on at which rate you are making the requests. At higher rates you can experience contention when more concurrent executions will be trying to write to same file. Simplest solution to this is to write to different files, but you can still run out of maximum possible number of opened files. Another solution is to use shared java.io.FileOutputStream resp. java.io.FileWriter to desired target file with proper synchronisation (will be accessed from various threads), which is still blocking IO. Yet another solution will be use Java NIO API to write to shared file via Channel (non-blocking) or OutputStream (not sure if non-blocking).

Of course solutions differ in difficulty of implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM