Skip to content

Instantly share code, notes, and snippets.

@nmccready
Created May 3, 2013 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmccready/5509151 to your computer and use it in GitHub Desktop.
Save nmccready/5509151 to your computer and use it in GitHub Desktop.
locust via lamda?
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
@heyman
Copy link

heyman commented May 3, 2013

As far as I can tell, I think that should work. For that simple use-case though, you could do something like this (I haven't run this code so there could be typos):

import random
from locust import Locust, TaskSet, task

urls = ["someurl1", "someurl2"]

class UserBehavior(TaskSet):
    @task
    def load_url(self):
        self.client.get(random.choice(urls))

class WebsiteUser(Locust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 1000

@nmccready
Copy link
Author

Yours works where mine does not, random is great for a for a 1:1 weight, but if I wanted to specify the weight that is what I was attempting to do above. IE, I could increase the weights with the last function added being the largest weighted function.

weight=1
for url in urls:
    lamda_set[lambda l: l.client.get(url)] = weight
    weight+=1

@nmccready
Copy link
Author

nvm it is partially working, i'll figure it out

@nmccready
Copy link
Author

part is not working is on mine it only executes one url not both... not sure why

@heyman
Copy link

heyman commented May 3, 2013

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)

@nmccready
Copy link
Author

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