Contents

Load Testing Extensions Using Locust and Elastic Beanstalk 

Reviews for Extensions, organizations, games, and chatbot verification are temporarily paused while we revise our processes. We are working to resume reviews as quickly as possible and will share updates with you shortly. Thank you for your patience and understanding.

Extension developers often ask how to load test their EBS (Extensions Backend Service). In this guide, we discuss how to go about load testing using Locust and Elastic Beanstalk. We have edited this AWS guide to work specifically for extensions backend testing.

Prerequisites

Setting up EBS testing on AWS

There are three main aspects to setting up EBS testing on AWS.

Set up Locust

To set up Locust:

  1. Create a new t2 micro “Amazon Linux AMI 2018.03.0” instance and log into it.
  2. Install the Elastic Beanstalk CLI
    pip install awsebcli --upgrade --user
  3. Configure your CLI:
    aws configure
  4. Install git in your t2 micro instance:
    sudo yum install git
  5. Clone the repository that contains the sample source code by running
    **git clone** **https://www.github.com/awslabs/eb-locustio-sample**
  6. In the AWS Identity and Access Management (IAM) console, create an IAM instance profile named aws-elasticbeanstalk-locust-role.
    For information on how to create an IAM instance profile, see Create an IAM Instance Profile for Your Amazon EC2 Instances.
  7. Add the following policy to your instance profile:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "elasticbeanstalk:DescribeEnvironmentResources",
                "elasticbeanstalk:DescribeEnvironments",
                "dynamodb:ListGlobalTables",
                "dynamodb:ListTables",
                "autoscaling:DescribeAutoScalingGroups",
                "dynamodb:ListBackups",
                "cloudformation:ListStackResources"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "dynamodb:CreateTable",
                "dynamodb:GetItem",
                "dynamodb:UpdateItem"
            ],
            "Resource": "arn:aws:dynamodb:*:*:table/*"
        }
    ]
  }

Once you have accomplished the previous steps, continue in the directory where you cloned the sample code. All the EB commands that follow must be run in this directory:

  1. Navigate to the directory eb-locustio-sample
  2. Initialize the directory for use with the EB CLI by running the following command: 
    eb init -r <region> -p "Java 8"
    Replace <region> with a region identifier such as us-west-2. For a full list of region identifiers, see Regions and Endpoints.

Alternatively, start interactive mode by running eb init, then following this procedure: 

  1. Pick a region from the list.
  2. Choose Create New Application.
  3. Type a name for the application.
  4. When you are asked, “It appears you are using Python. Is this correct?” choose No.
  5. For platform, choose Java.
  6. For the platform version, choose Java 8.
  7. When you are asked, “Do you want to set up SSH for your instances?” choose No.
    Note: If you choose to enable SSH and do not already have an SSH key stored on AWS, the command ssh-keygen must be available on the path as it is used by the EB CLI to generate the SSH key to be used.
  8. Create your load generation environment by running the following command. Replace <Test URL> with the URL of the web app that you want to test.
    **eb create <env-name> -i c4.large --scale 1 --envvars TARGET_URL=<Test-URL> --instance_profile aws-elasticbeanstalk-locust-role** 
    Note: The scale CLI parameter shown previously here can be used to spin up more instances that can run as slaves to a master instance and provide higher throughput as needed for your testing needs. More details can be read in the “Scaling your Load Tests” section. 
  9. Type a name for the environment.
  10. Type the CNAME prefix that you want to use for this environment.

After the environment has been created, the logs will indicate the URL for the application. It usually follows a format like CNAME.<region>.elasticbeanstalk.com.

Run your Locust test

Locust is a python script based framework for writing tests. If you would like to learn more about writing tests using Locust, see Writing a locustfile. The sample code you cloned previously includes a sample test definition that tests the root of the provided test URL every 45-50 milliseconds. We are now going to use a test definition better suited to testing your EBS. 

  1. Navigate to the folder where you cloned the sample code.
  2. Replace the [locustfile.py](https://github.com/awslabs/eb-locustio-sample/blob/master/locustfile.py) file with the following:

    import os
    import string
    from locust import HttpLocust, TaskSet, task
    class UserBehavior(TaskSet):
        @task
        def put_tests(self):
            response = self.client.post("/<Your path here>",
            headers={
    #Your header values here
            },
    data={
    #Your POST data here
    })
    class WebsiteUser(HttpLocust):
        host = os.getenv(
            'TARGET_URL', "<DEFAULT URL HERE>")
        task_set = UserBehavior
  3. Save and commit the changes: git commit -am "POSTing to my EBS"
  4. Deploy your changes to the environment by running eb deploy --staged
  5. Once the environment update is complete, in a web browser, go to the URL you noted in Step 8 of the previous procedure.
  6. Run your test using the number of users and spawn rate as desired.

Scale your load tests

You can scale out your environment to more than one EC2 instance:

  1. Navigate to the folder where you cloned the sample code.
  2. Open the configuration file in the default editor by running eb config.
  3. Under the aws:elasticbeanstalk:command namespace, set the BatchSize to 100.
  4. Save the configuration file, and exit the editor to start the environment update.
  5. After the update completes, run eb scale <number of instances>. Replace <number of instances> with the number of EC2 instances that you want the environment to scale out to.
  6. After the additional instances have been added, run eb deploy --staged. Since Locust requires a single master, this step ensures that a single instance is selected as master after the environment has scaled out.
  7. To open the Locust dashboard and start your tests, in a web browser go to the URL you noted in Step 8 of the setup procedure.