scala 加特林喂食器的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27381850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of gatling feeders
提问by N LAMY
I'm trying to use two gatling feeders for producing http post request data :
我正在尝试使用两个 gatling feeder 来生成 http post 请求数据:
First file contains some fields. One of them is a counter. With this value I'd like to add to my post body as far as lines from second feeder.
第一个文件包含一些字段。其中之一是柜台。有了这个值,我想添加到我的帖子正文中,直到从第二个馈线开始。
For example :
例如 :
fileA.csv
---------
fileAId,counter
value,3
fileB.csv
---------
fileBId
stack
overflow
I want to construct this string : "value stack overflow stack".
我想构造这个字符串:“值堆栈溢出堆栈”。
I created a scenario :
我创建了一个场景:
object Actions {
val search = forever() {
group("Test") {
exec(feed(FeederUtils.fileAFeeder))
.exec(
http("Test")
.post(uri)
.body(StringBody("""${fileAId} """ + FeederUtils.generateItems(${counter}.toInt)))
)
.pause(20 seconds)
}
}
}
And an object FeederUtils :
和一个对象 FeederUtils :
object FeederUtils {
val fileAFeeder= csv("fileA.csv").circular
var fileBFeeder = csv("fileB.csv").circular
def generateItems(itemsNumber: Int) : String = {
var i = 0;
var returnedString = "";
for(i <- 0 to itemsNumber) {
exec(feed(fileBFeeder))
returnedString = returnedString + """${fileBId} """
}
return returnedString ;
}
}
I have two problems : function call doesn't compile (not found: value $) and feeder variables doesn't exist in generateItems.
我有两个问题:函数调用无法编译(未找到:value $)并且 generateItems 中不存在 feeder 变量。
I'm new to Gatling & Scala so I think this is evident but I don't understand how exec and feed functions work.
我是 Gatling 和 Scala 的新手,所以我认为这很明显,但我不明白 exec 和 feed 函数是如何工作的。
Thanks !
谢谢 !
EDIT : Functionnal code is below :
编辑:功能代码如下:
object FeederUtils {
val fileAFeeder= csv("fileA.csv").circular
var fileBVector = csv("fileB.csv").records
var fileBIterator = 0;
def generateItems(itemsNumber: Int) : String = {
var i = 0;
var returnedString = "";
for(i <- 0 to itemsNumber) {
var currentItem = fileBVector(fileBIterator)
//Circular read
if (fileBIterator < fileBVector.size) {
fileBIterator+=1
} else {
fileBIterator=0
}
returnedString = returnedString + currentItem("fileBId")
}
return returnedString ;
}
}
object Actions {
val search = forever() {
group("Test") {
exec(feed(FeederUtils.fileAFeeder))
.exec({session => session.set("generatedString",feederUtils.generateItems(session("counter").as[String].toInt))})
.exec(
http("Test")
.post(uri)
.body(StringBody("""${fileAId} ${generatedString}"""))
)
.pause(20 seconds)
}
}
}
The concept below is : feed function stores data into session attributes, which can be readed from gatling EL expressions or manually with Session API. I had to combine both.
下面的概念是:feed 函数将数据存储到会话属性中,可以从 gatling EL 表达式或使用 Session API 手动读取。我不得不将两者结合起来。
Links :
链接:
采纳答案by Stephane Landelle
You can't use a feeder for your second file. At best, you can pull multiple records at once, but names will be translated (fileBId1, fileBId2...).
您不能为第二个文件使用进纸器。充其量,您可以一次提取多条记录,但名称将被翻译(fileBId1、fileBId2...)。
Load the second file content with Gatling csv parser, so you'll be able to access the records (records field) and store it into a global val.
使用 Gatling csv 解析器加载第二个文件内容,这样您就可以访问记录(记录字段)并将其存储到全局 val 中。
Feed from the first file.
从第一个文件馈送。
Then write an exec(function)where you:
然后写一个exec(function)你:
- fetch the counter from the session
- generate a random offset (ThreadLocalRandom) if you want something like the random strategy, or an AtomicInteger that you would increment if you want something like the circular strategy.
- fetch records from the second file (use a modulo on the records vector size to get proper indices)
- compute your final String
- 从会话中获取计数器
- 如果您想要随机策略之类的东西,则生成随机偏移量 (ThreadLocalRandom),或者如果您想要循环策略之类的东西,您将增加一个 AtomicInteger。
- 从第二个文件中获取记录(对记录向量大小使用模数以获得正确的索引)
- 计算你的最终字符串
Don't try to use Gatling EL in custom code. See doc.
不要尝试在自定义代码中使用 Gatling EL。见文档。

