import os import rsa import uuid import base64 import json import time from locust import HttpUser, task, between, constant from colorama import Fore, Back, Style, init # locust -f test.py --csv=locust_report class VerifyInfoTestAPI(HttpUser): #wait_time = between(1, 5) # Khoảng thời gian ngẫu nhiên giữa các tác vụ wait_time = constant(0.1) # Chờ 1 giây giữa các yêu cầu host = "https://example.vn" @task def verify_info(self): with open('private_key.pem', 'rb') as privateKeyFile: privateKeyContent = privateKeyFile.read() privateKey = rsa.PrivateKey.load_pkcs1(privateKeyContent, 'PEM') requestId = str(uuid.uuid4()) partnerId = 'OKBZG0NX42' amount = 30000 payload = requestId + "|" + partnerId + "|" + str(amount) msg = payload.encode("utf-8") signature = rsa.sign(msg, privateKey, 'SHA-1') signature = base64.b64encode(signature) url = '/service/test' payload = { 'request_id': requestId, 'partner_id': partnerId, 'amount': amount, 'signature': signature } print("Params: " + str(payload)) with self.client.post( url, data = payload ) as response: if response.status_code == 200: print(Fore.WHITE + "Response Status (" + requestId + "): " + str(response.status_code) + Style.RESET_ALL) responseContent = response.json() banks = responseContent['data']['banks'] if len(banks) > 0: print(Fore.WHITE + "Response bank (" + requestId + "): " + str(banks) + Style.RESET_ALL) else: print(Fore.RED + "Response bank (" + requestId + "): is empty!" + Style.RESET_ALL) else: print(Fore.RED + "Response Status (" + requestId + "): " + str(response.status_code) + Style.RESET_ALL)