# Load Testing Extensions

Load testing an Extension backend service with Locust

Source: https://dev.twitch.tv/docs/extensions/load-testing-extensions/

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](https://locust.io/) and [Elastic Beanstalk](https://aws.amazon.com/elasticbeanstalk/). We have edited [this AWS guide](https://aws.amazon.com/blogs/devops/using-locust-on-aws-elastic-beanstalk-for-distributed-load-generation-and-testing/) to work specifically for extensions backend testing.

## Prerequisites

* Create an AWS account.
* Set up a [load balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-getting-started.html).

## Setting up EBS testing on AWS

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

* Set up Locust
* Run your Locust test
* Scale your load tests

### 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](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html)\:<br>`pip install awsebcli --upgrade --user`
3. [Configure your CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html#cli-quick-configuration)\:<br>`aws configure`
4. Install git in your t2 micro instance:<br>`sudo yum install git`
5. Clone the repository that contains the sample source code by running<br>`**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`.<br>For information on how to create an IAM instance profile, see [Create an IAM Instance Profile for Your Amazon EC2 Instances](https://docs.aws.amazon.com/codedeploy/latest/userguide/how-to-create-iam-instance-profile.html).
7. Add the following policy to your instance profile:
  <pre class="language-bash"><code>{
    "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/*"
        }
    ]
  }</code></pre>

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:<br>`eb init -r <region> -p "Java 8"`<br>Replace &lt;region&gt; with a region identifier such as us-west-2. For a full list of region identifiers, see[Regions and Endpoints](https://docs.amazonaws.cn/en_us/general/latest/gr/rande.html#elasticbeanstalk_region).

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**.<br>**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 &lt;Test URL*&gt;* with the URL of the web app that you want to test.<br>`**eb create <env-name> -i c4.large --scale 1 --envvars TARGET_URL=<Test-URL> --instance_profile aws-elasticbeanstalk-locust-role**`<br>**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](https://docs.google.com/document/d/1HNzz9N17fhB_E06fEW5JKHHHq3VD15o_gwubgyXJEpE/edit#heading=h.2hzx3yowgb2)” 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](https://docs.locust.io/en/latest/writing-a-locustfile.html). 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/aws-samples/eb-locustio-sample/blob/master/locustfile.py) file with the following:

   <pre class="language-bash"><code>import os
   import string
   from locust import HttpLocust, TaskSet, task
   class UserBehavior(TaskSet):
       @task
       def put_tests(self):
           response = self.client.post("/&lt;Your path here&gt;",
           headers={
   #Your header values here
           },
   data={
   #Your POST data here
   })
   class WebsiteUser(HttpLocust):
       host = os.getenv(
           'TARGET_URL', "&lt;DEFAULT URL HERE&gt;")
       task_set = UserBehavior</code></pre>
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 *&lt;*number of instances*&gt;* 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](https://locust.io/) dashboard and start your tests, in a web browser go to the URL you noted in Step 8 of the setup procedure.
