Created
May 3, 2013 13:37
-
-
Save nmccready/5509151 to your computer and use it in GitHub Desktop.
locust via lamda?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from locust import Locust, TaskSet | |
urls = ["someurl1", "someurl2"] | |
lamda_set = {} | |
for url in urls: | |
lamda_set[lambda l: l.client.get(url)] = 1 | |
class UserBehavior(TaskSet): | |
tasks = lamda_set | |
def on_start(self): | |
pass | |
class WebsiteUser(Locust): | |
task_set = UserBehavior | |
min_wait = 1000 | |
max_wait = 1000 |
part is not working is on mine it only executes one url not both... not sure why
Aah, I see. I think it's because of how python scope:ing works. The url variable will always be the same as the last url in the list, once the lambdas are called.
You could use my proposal together with something like this:
urls_dict = {"some url":1, "some other url":2}
urls = []
for url, weight in urls_dict.iteritems():
for i in range(weight):
urls.append(url)
GOT it!!! With help from here, http://stackoverflow.com/questions/452610/how-do-i-create-a-list-of-python-lambdas-in-a-list-comprehension-for-loop
Basically the lambda was being stored to the same reference. So by defining a function to return a lambda, it creates a new one each time.
from locust import Locust, TaskSet
urls = ("url1", "url2")
def local_print(arg):
print arg
def create_lamda(url):
print url
return lambda l: l.client.get(url)
lambda_set = {}
weight = 1
for url in urls:
lamda_set[create_lamda(url)] = weight
weight += 1
class UserBehavior(TaskSet):
print lambda_set
tasks = lambda_set
def on_start(self):
pass
class WebsiteUser(Locust):
task_set = UserBehavior
min_wait = 1000
max_wait = 1000
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nvm it is partially working, i'll figure it out