[PYTHON] Locust-Load test

Locust-Load test

REF

[1] - Locust document


I will write in Japanese at the beginning. Thank you!


Difficulty of load test

--It is slow to send a Request with one Thread. --Even if Requests are sent by multiple Threads, the performance of one server (personal computer) is limited. --It is difficult to specify the load. --ResponseTime statistics are tedious. (To solve the second problem, it becomes more annoying when multiple servers are overloaded) --I want to check the contents of Response, but if I check it, sending Request will be delayed. It's hard to balance. ――I don't want to write such a load test program myself.

Locust can solve all of these problems. (Mysterious voice: ideal load test, Locust)

Basic features of Locust

In Locust, you can set how many users (clients), how often and how requests are sent. Then, you can display the number of requests per second at the time of execution, the number of times each request is sent, the average / maximum / minimum ResponseTime, and the failed requests.

Install Locust

Installing with pip and easy_install is the easiest way:

pip install locustio
easy_install locustio

Please refer to here for how to install Windows and Mac.

Start Locust

Load testing with Locust is easy. All you need to launch a load test is a python TestCase. Locust's Test Case is also easy to write. The following is one of the typical examples:

SimpleTask.py


from locust import HttpLocust, TaskSet, task

#Before the real Test Case, prepare the materials for Test
textureDirName = "texture"
userIdFileName = "user_id.txt"
userIdFilePath = os.path.join(os.path.abspath(os.path.dirname(__file__)), textureDirName, userIdFileName)

globalUserArr = list(line.rstrip('\n') for line in open(playerIdFilePath))

class SimpleTaskSet(TaskSet):

  #Method to invoke when starting test
  def on_start(self):
    randomIndex = random.randint(0, len(globalUserArr) - 1)
    self.testingUserId = globalUserArr[randomIndex]

  # test case 1
  @task(1)
  def testRequestGet(self):
    #Create the path part of the url
    requestPath = "/services/users/" + self.testingUserId + "/checkSomeThing"
    #Send request
    self.client.get(requestPath)
    
  # test case 2
  # @task(2)Specifies that this test case will be sent twice as often as test case 1 above.
  @task(2)
  def testRequestPost(self):
    requestPath = "/services/users/doSomeThing/checkin"
    #How to read Post request
    self.client.post(requestPath, {"user": self.testingUserId })

#Locust class
class SimpleLocust(HttpLocust):
  task_set = ReadStateTaskSet #Import the above test case
  #Setting how many seconds each user should wait after sending the previous request
  min_wait = int(1) 
  max_wait = int(2) 

There is one caveat. I think it is better not to have Members like * List * and * Dict * in the TaskSet.

And

python -H [you_url_or_ip_of_your_application] -f [path_of_your_SimpleTask]

You can visit the load test console on localhost 8089Port by running. Then the load is set and the test is started.

Distributed load test

With Locust, distributed load testing is easy.

First, prepare multiple servers, and then upload the above script to the server.

One server is responsible for ** Master ** and the other server is ** Slave **. The load set by ** Master ** is distributed to ** Slave **, and ** Slave ** sends the request.

In master:

python -H [you_url_or_ip_of_your_application] -f [path_of_your_simple] --master

With slave:

python -H [you_url_or_ip_of_your_application] -f [path_of_your_simple] --slave --master-port=[master_ip]

There are three points to note when conducting a distributed load test:

  1. Since there is communication between Master and Slave, you have to set Firewall and Security Group.
  2. If you test a heavy load, many files will open at the same time due to HealthCheck etc., so it may exceed Ulimit. So, on each Master and Slave server, run ʻulimit 4096` first before starting Locust.
  3. If you want to change the default 8089 port, add --master-bind-port = 5557 in Master and add --master-port = 5557 in Slave.

Recommended Posts

Locust-Load test
test
Django test
Post test
About the test
numpy unit test
Distribution and test
Python Integrity Test