Running Locust inside Lando Dev Tool

Submitted by galactus on Sun, 01/12/2020 - 06:20

In this blog post, I'm going to talk about how to setup Locust inside the Lando Dev Tool.

First, let's discuss what are these tools are.

Lando vastly simplifies local development and DevOps so you can focus on what's important; delivering value to your clients and customers. - https://lando.dev/ (What I love here is I don't need to setup my docker container from the scratch it is already bundle and configured in Lando. I can easily destroy the instance whenever I wanted and spin up new one.)

Locust is an open source load testing tool. - https://locust.io/

Note: I'm not going to cover how to setup Lando in a local machine I assume you already have. If you don't have Lando installed in your machine you can check the guide here: https://docs.lando.dev/basics/installation.html 

Let's get started...

  1. In your local machine create a folder name mylocust.
    mkdir mylocust
  2. Navigate inside the folder.
    cd mylocust
  3. Create .lando.yml file copy and paste the content below in your file.
    name: mylocust
    
    proxy:
      appserver:
        - locust.lndo.site:8089
    
    services:
      appserver:
        type: python:3.7
        port: 80
        ssl: false
        command: locust
        install_dependencies_as_me:
          - /usr/local/bin/python -m pip install --upgrade pip
          - pip3 install locust
    
    tooling:
      python:
        service: appserver
    
  4. Create locustfile.py file. Here we define a number of Locust tasks. We just copy the contents of the file here: https://docs.locust.io/en/stable/quickstart.html - you can replace it with your own site information.
    from locust import HttpLocust, TaskSet, between
    
    def login(l):
        l.client.post("/login", {"username":"ellen_key", "password":"education"})
    
    def logout(l):
        l.client.post("/logout", {"username":"ellen_key", "password":"education"})
    
    def index(l):
        l.client.get("/")
    
    def profile(l):
        l.client.get("/profile")
    
    class UserBehavior(TaskSet):
        tasks = {index: 2, profile: 1}
    
        def on_start(self):
            login(self)
    
        def on_stop(self):
            logout(self)
    
    class WebsiteUser(HttpLocust):
        task_set = UserBehavior
        wait_time = between(5.0, 9.0)
    
  5. Start Lando.
    lando start

    Initializing installation process...lando-start

  6. Visit Lando generated URL.locust-webpage

Congratulations! you now have Locust running inside your Lando instance.