性能工具之locust工具get與post請求

最近在學習 locust 性能工具,發現locust性能工具腳本須要python基礎才能寫腳本,可是對於性能測試人員來講 python 是基本功夫。python

在 locust 中get腳本怎麼寫,爲了方便直接在代碼運行調試,採用關閉web模式,經過參考官方文檔本身實驗get/post代碼,參考代碼如:
@task(1)web

def get_7dTest(self):
    # 定義請求頭
    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')

post請求寫法以下
# 登錄json

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

結果:app

結果:

[2021-04-24 21:36:49,495] liwen.local/INFO/locust.main: Run time limit set to 1 seconds
[2021-04-24 21:36:49,495] liwen.local/INFO/locust.main: Starting Locust 1.4.4
[2021-04-24 21:36:49,496] liwen.local/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-04-24 21:36:49,496] liwen.local/INFO/locust.runners: All 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 disabledless

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


Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00工具

Response json: {'msg': 'success', 'code': 0, 'data': '登錄成功'}
successful
Response json: {'msg': 'success', 'code': 0, 'data': '登錄成功'}
Response json: {'msg': 'success', 'code': 0, 'data': '登錄成功'}
Response json: {'msg': 'success', 'code': 0, 'data': '登錄成功'}
Response json: {'msg': 'success', 'code': 0, 'data': '登錄成功'}post

post請求json請求寫法:
@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())

結果

Response json: {'msg': 'success', 'code': 0, 'data': '登錄成功'}學習

實驗請求資源代碼:測試

@GetMapping("/7d")
@ResponseBody
public R indexPage() {
    HashMap<String, Object> map = new HashMap<>();
    map.put("success", "歡迎來到性能實戰課堂");
    map.put("Data", new Date());
    return R.ok().put("data", map);
}

/**

* 登錄
 *
 * @param memberEntity
 * @return
 */
@PostMapping("/login")
@ResponseBody
public R login(MemberEntity memberEntity) {

    if ("7d".equals(memberEntity.getUserName()) && "123456".equals(memberEntity.getPassWord())) {
        return R.ok().put("data", "登錄成功");
    }
    return R.error().put("data", "用戶名或者密碼失敗");
}


/**
 * 登錄
 *
 * @param requestBody
 * @return
 */
@PostMapping(value = "/login/json", produces = "application/json")
@ResponseBody
public R login_json(@RequestBody String requestBody) {
    log.info("json數據:{}", 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", "登錄成功");
    }
    return R.error().put("data", "用戶名或者密碼失敗");
}

locust直接在 python 代碼調試代碼關閉web頁面,這樣調試很方便,參考以下命令:

os.system("locust -f demoLcou.py --host=http://127.0.0.1:8080 --headless -u 1 -r 1 -t 1s")
# –no-web 表示不使用Web界面運行測試。
# -c 設置虛擬用戶數。
# -r 設置每秒啓動虛擬用戶數。
# -t 設置設置運行時間。

# 結果參考:
[2021-04-24 22:23:59,688] liwen.local/INFO/locust.main: Run time limit set to 1 seconds
[2021-04-24 22:23:59,688] liwen.local/INFO/locust.main: Starting Locust 1.4.4
[2021-04-24 22:23:59,688] liwen.local/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-04-24 22:23:59,688] liwen.local/INFO/locust.runners: All 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': '未知異常,請聯繫管理員', 'code': 500, 'data': '用戶名或者密碼失敗'}
。。。。。中間省略。。。。
successful
[2021-04-24 22:24:00,454] liwen.local/INFO/locust.main: Time limit reached. Stopping Locust.
[2021-04-24 22:24:00,454] liwen.local/INFO/locust.runners: Stopping 1 users
[2021-04-24 22:24:00,455] liwen.local/INFO/locust.runners: 1 Users have been stopped, 0 still running
[2021-04-24 22:24:00,455] liwen.local/INFO/locust.main: Running teardowns...
[2021-04-24 22:24:00,455] liwen.local/INFO/locust.main: Shutting down (exit code 0), bye.
[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(0.00%) | 1 1 2 2 | 113.55 0.00

Aggregated 440 0(0.00%) | 1 1 12 2 | 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

總結:locust 官方文檔仍是比較詳細,只要慢慢看就能掌握locust工具怎麼操做,可是在老師性能工程中,工具只要能發壓就行。

相關文章
相關標籤/搜索