How do I bulk/chunk paginate existing Seq[(String)] session value in Gatling? - gatling

I am executing a call that saves a lot of values into a Seq[(String)], it looks as follows:
.exec(session => {session.set("Ids", session("externalIds").as[Seq[String]])})
There is a reason why I have to create another session variable called Ids our of externalIds but I wont get into it now.
I than have to execute another call and paginate 10 values out of ${Ids} until I send them all.
(So in case of 100 values, I'll have to execute this call 10 times)
The JSON looks as follows:
..."Ids": [
"962950",
"962955",
"962959",
"962966",
"962971",
"962974",
"962978",
"962983",
"962988",
"962991"
],...
What I usually do when I have to iterate through one value each time is simply:
.foreach("${Ids}", "id") {
exec(getSomething)
}
But since I need to send a [...] Of 10 values each, I am not sure if it should even be in the scenario level. Help! :)

Use transform in your check to transform your Seq[String] into chunks, eg with Seq#grouped.

I couldn't figure out how to go about this within the session so I took it
outside to a function and here is the solution:
.exec(session => {session.set("idSeqList", convertFileIdSeqToFileIdSeqList(session("idsSeq").as[Seq[String]]))})
def convertFileIdSeqToFileIdSeqList(idSeq: Seq[String]): Seq[Seq[String]] = {
idSeq.grouped(10).toList
}
Note that when placing your list within a JSON body, you will need to use .jsonStringify() to format it correctly in the JSON context like so:
"ids": ${ids.jsonStringify()},

Related

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

Gatling: Save random attribute in another attribute

Is it possible to save an attribute at runtime and then save it as another attribute? For instance, I have an ID that is used in the URL, I've captured it from one page, however there are a list of 5 on the page. I can use findAll to select them all, and then ${AttributeName.random()} to select one at random.
However how do I then go and save that as an attribute and then use it elsewhere? As it needs to be the same each time and if I run random again obviously it'll change string each time.
I could do an ${AttributeName(storedRandomNumber)} but the code could start to be a little messy and was wondering if there was something a little cleaner to use?
You could make another exec() right after this request to assign the random value you want with the session.set() method, this value then is saved for the entire thread to be reused.
EX :
val scenario = scenario("scenarioName")
.exec(
http("<-- Name Of Request -->")
.get("<LINK _TO_FIRST_REQ>")
.check(jsonPath("$.items[*].id").findAll.optional.saveAs("ListOfAttributeNames"))
)
.exec( session => session.set("randomAttributeNameSelected", session("ListOfAttributeNames").as[Seq[String]]
.apply(scala.util.Random
.nextInt((session("ListOfAttributeNames").as[Seq[String]].size - 0) + 1)))
)
.exec(
http("We use the ID here")
.get(session => "http://domain.something.com/api/" + session("randomAttributeNameSelected").as[String])
)
Thus anytime in the same thread if you access session("randomAttributeNameSelected").as[String] it will give you random ID.

How may I check an empty feeder in gatling before moving ahead with actual execution

I want to run my simulation against two set of data. One set gives empty feeder issue and other one don't. I want to write a generic code in gatling which can handle both data sets. I would like to avoid simple if-else conditions for the variable I am setting in feeder. Also changing the data is not an option available to me.
In short, I want my execution to be skipped if my feeder is empty. Is it possible through gatling way ?
exec(
randomSwitch(33.0 -> feed(data1.random).exec(step1),
33.0 -> feed(data2.random).exec(step2),
34.0 -> feed(data3.random).exec(step3)
))
You can try something like this
scenario("Requests").feed(orderRefs).group("Groups") {
asLongAs(session => jobsQue.length > 0) {
exec { session => you code
}

Can I use same param name multiple times in the URL for codeigniter-restserver?

http://example.com/api/transfer/transfers/code/456/code/234
When using $this->get('code') on a url like above I expect the REST library to return an array or a list of the codes.
Instead it returns the last one.
Is there a way to return both values in a list, or is there another recommandation for formating the URL.
Thank you
I know it has been long since you posted the question. However it could help other people looking for the same thing.
Assuming that transfer is your controller and transfers is the function, another way to format your url could be:
http://example.com/api/transfer/transfers?code[]=456&code[]=234
This was you perform $this->get('code') you'll get an array back.
If you are creating the url via code then you may use http_build_query(). It handles the necessary escaping. It means it will replace [ for %5B and ] for %5D, in this case.
The code would be like:
$codes = array(456, 234);
$query = http_build_query(array('code' => $data));
$url = 'http://example.com/api/transfer/transfers?' . $query;

Fetch query from Magento database -- mysql_num_rows

What function is equal to mysql_num_rows in Magento?
For Magento, the proper equivalent is PHP's count() function.
Why?
Magento usually uses Varien_Data_Collection instances to fetch result sets containing multiple records. Varien implements the Lazy Load Pattern for these collections, that is, no result set will be fetched before you really need it.
If you take a look at the Varien_Data_Collection class, you'll see, that this class does implement PHP's Countable interface and the proper count() method for this interface:
class Varien_Data_Collection implements IteratorAggregate, Countable
{
:
public function count()
{
$this->load();
return count($this->_items);
}
:
}
If you're asking yourself now, what got lazy loading to do with counting records, then you need to know that querying a collection the usual Magento way, e.g. like this:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter(
'status',
Mage_Catalog_Model_Product_Status::STATUS_ENABLED
);
does not fetch the result set at all. But, how do you count records of a result set which hasn't been fetched yet? Right, you can't. And neither can mysql_num_rows. It fetches the result set first.
Now, when you call count() on the collection, e.g. by
$n = count($collection);
PHP's core count() function will detect that the passed argument $collection implements a Countable interface and has its own count() method defined, so it will call that one.
This leads to really fetching the result set* and storing it to $this->_items, which finally allows counting the records and return the number.
* In Magento you can also call foreach ($collection as $product) to really fetch the result set, but that's another story.

Resources