Recently, I learned about the Locust Performance Tool. I found that the script of the Locust Performance Tool requires basic Python to write the script, but Python is the basic skill for the performance tester.

How to write the GET script in Locust? In order to facilitate running debugging directly in the code, we use the closed web mode, and experiment the GET/POST code by referring to the official document. The reference code is: @Task (1).

Def get_7dTest(self): # define request header = {" user-agent ": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"} response = self.client. Get ("/7d/", headers=header, verify=False) # print("Response status code:", response.status_code) if response.status_code == 200: print("successful") # print("Response text:", response.json()) else: print('failure')

The POST request is written as # log in

@task(1)
def get_login(self):
    response = self.client.post("/login", {"userName": "7d", "passWord": "123456"})
    print("Response json:", response.json())

Results:

Results:

[the 21:36:49 2021-04-24, 495] liwen. Local/INFO/locust. Main: The Run time limit set to 1 seconds [the 21:36:49 2021-04-24, 495] liwen. Local/INFO/locust. Main: Starting Locust 1.4.4 [the 21:36:49 2021-04-24, 496] liwen. Local/INFO/Locust. The runners: Spawning 1 users at the rate 1 users/s (0 users already running)… [the 21:36:49 2021-04-24, 496] liwen. Local/INFO/locust. The runners: All the users spawned: webTestDunShan: 1 (1 total running) [2021-04-24 21:36:49,496] liwen.local/INFO/root: Terminal was not a tty. Keyboard input disabled

Name # reqs # fails | Avg Min Max Median | req/s failures/s


Aggregated 0 0 (0.00%) | 0 0 0 0 | 0.00 0.00

Json: {‘ MSG ‘: ‘success’, ‘code’: 0, ‘data’: ‘successful ‘} Successful Response json: {‘ MSG ‘: ‘success’, ‘code’: 0, ‘data’: ‘login successfully ‘} Response json: {‘ MSG ‘: ‘success’, ‘code’: 0, ‘data’:’ login successfully ‘} Response json: {‘ MSG ‘: ‘success’, ‘code’ : 0, ‘data’ : ‘landing successful} Response json: {‘ MSG’ : ‘success’, ‘code’ : 0, ‘data’ : ‘landing successful’}

Post request json request written: @ task (1)

def get_login_json(self):
    jsonData = {"userName": "7d", "passWord": "123456"}
    response = self.client.post("/login/json", json=json.dumps(jsonData))
    print("Response json:", response.json())

The results of

Response json: {‘ MSG ‘:’ success ‘, ‘code’ : 0, ‘data’ : ‘landing successful’}

Experiment request resource code:

@GetMapping("/7d") @ResponseBody public R indexPage() { HashMap<String, Object> map = new HashMap<>(); Map. put("success", "welcome to the performance class "); map.put("Data", new Date()); return R.ok().put("data", map); }

/ * *

@dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity @dememberentity  { if ("7d".equals(memberEntity.getUserName()) && "123456".equals(memberEntity.getPassWord())) { return R.k ().put("data", "login successful "); } return R.rror ().put("data", "user name or password failed "); } /** * Login ** @Param requestBody * @Return */ @PostMapping(value = "/login/json", Produces = "application/json") @responseBody public R login_json(@requestBody String requestBody) {log.info(" Produces = "application/json") @requestBody public R login_json(@requestBody String requestBody) {log.info(" {}", requestBody); Object parse = JSON.parse(requestBody); MemberEntity memberEntity = JSON.parseObject(parse.toString(), MemberEntity.class); if ("7d".equals(memberEntity.getUserName()) && "123456".equals(memberEntity.getPassWord())) { return R.ok().put("data", "Log in successfully "); } return R.rror ().put("data", "user name or password failed "); }

Locust closes the web page directly from Python code. This is easy to debug.

OS. System (“locust f demolcou.py –host=http://127.0.0.1:8080 — headless-u 1-r 1-t 1s”) # — No-web means to run the test without using the web interface. # -c Set the number of virtual users. # -r Sets the number of virtual users started per second. The # -t setting sets the running time.

Result # reference: [the 22:23:59 2021-04-24, 688] liwen. Local/INFO/locust. Main: The Run time limit set to 1 seconds [the 22:23:59 2021-04-24, 688] liwen. Local/INFO/locust. Main: Starting Locust 1.4.4 [the 22:23:59 2021-04-24, 688] liwen. Local/INFO/Locust. The runners: Spawning 1 users at the rate 1 users/s (0 users already running)… [the 22:23:59 2021-04-24, 688] liwen. Local/INFO/locust. The runners: All the users spawned: webTestDunShan: 1 (1 total running) [2021-04-24 22:23:59,689] liwen.local/INFO/root: Terminal was not a tty. Keyboard input disabled

Name                   # reqs      # fails  |     Avg     Min     Max  Median  |   req/s failures/s


Aggregated 0 0 (0.00%) | 0 0 0 0 | 0.00 0.00

Successful Response json: {‘ MSG ‘: ‘Unknown exception, please contact administrator ‘, ‘code’: 500, ‘data’:’ user name or password failed ‘}… Ellipsis in the middle… Successful [the 22:24:00 2021-04-24, 454] liwen. Local/INFO/locust. Main: Time limit reached. Stopping Locust. [the 2021-04-24 22:24:00, 454] liwen. Local/INFO/Locust. The runners: Stopping 1 users [22:24:00 2021-04-24, 455] liwen. Local/INFO/locust. The runners: 1 Users have had stopped, 0 still running [the 22:24:00 2021-04-24, 455] liwen. Local/INFO/locust. Main: running teardowns… [the 22:24:00 2021-04-24, 455] liwen. Local/INFO/locust. Main: Shutting down the exit code (0), bye. [the 2021-04-24 22:24:00, 455] liwen. Local/INFO/locust. Main: Cleaning up runner…

Name # reqs # fails | Avg Min Max Median | req/s failures/s

GET / 166 0(0.00%) | 1 1 5 2 | 216.65 0.00

GET /7d/ 89 0(0.00%) | 1 1 12 1 | 116.16 0.00

POST /login 98 0(0.00%) | 1 1 2 1 | 127.90 0.00

POST/login/json, 87 (0.00%) 1 1 2 2 | | 113.55 0.00

Aggregated, 440 (0.00%), 12 2 | | 1 1 574.26 0.00

Response time percentiles (approximated)

Type Name 50% 66% 75% 80% 90% 95% 98% 99% 99.9% 99.99% 100% # reqs

GET / 2 2 2 2 2 3 3 5 6 6 6 166

GET /7d/ 1 1 1 1 2 2 2 12 12 12 12 89

POST /login 1 1 1 1 2 2 2 2 2 2 2 98

POST /login/json 2 2 2 2 2 2 2 3 3 3 3 87

None Aggregated 2 2 2 2 2 2 3 3 12 12 12 440

Process finished with exit code 0

Summary: The official documentation of LOCUST is still relatively detailed, as long as you slowly look at the LOCUST tool how to operate, but in the teacher performance engineering, as long as the tool can send pressure line.