from locust import FastHttpUser, SequentialTaskSet, TaskSet, between, task
from geventhttpclient import HTTPClient #Nginx
from geventhttpclient.url import URL
import random, string, json
host = 'http://localhost:8000'
# 继承TaskSequence 控制方法按从上往下的顺序执行
class GetProductListSeqTaskSet(SequentialTaskSet):
login_header={}
del_params={}
def on_start(self):
'''初始化数据'''
url = URL(host)
@task
def login(self):
self.login_header['content-type'] = "application/json"
data = r"""{
"operationName":"loginWithoutDetails",
"variables":{
"email":"admin@qq.com",
"password":"admin"
},
"query":"fragment AccountErrorFragment on AccountError {\n code\n field\n message\n __typename\n}\n\nfragment UserBaseFragment on User {\n id\n email\n firstName\n lastName\n isStaff\n __typename\n}\n\nmutation loginWithoutDetails($email: String!, $password: String!) {\n tokenCreate(email: $email, password: $password) {\n csrfToken\n token\n errors {\n ...AccountErrorFragment\n __typename\n }\n user {\n ...UserBaseFragment\n __typename\n }\n __typename\n }\n}\n"
}"""
_login_complete = self.client.post("/graphql/", data=json.dumps(json.loads(data)), headers=self.login_header)
dict_login_complete = _login_complete.json()
# print(dict_login_complete)
# self.login_header['authorization-bearer'] = _json_login_complete.get("data").get("tokenCreate").get("token")
self.login_header['authorization-bearer'] = dict_login_complete["data"]["tokenCreate"]["token"]
self.login_header['Cookie'] = 'refreshToken==' + dict_login_complete["data"]["tokenCreate"]["token"]
@task
def get_product_list(self):
data = r"""{
"operationName": "ProductList",
"variables": {
"first": 20,
"filter": {
"attributes": null,
"categories": null,
"collections": null,
"price": null,
"productTypes": null,
"search": "juice",
"giftCard": null,
"stockAvailability": null
},
"sort": {
"direction": "DESC",
"field": "RANK"
},
"hasChannel": false,
"hasSelectedAttributes": false,
"PERMISSION_MANAGE_USERS": true,
"PERMISSION_MANAGE_STAFF": true,
"PERMISSION_IMPERSONATE_USER": true,
"PERMISSION_MANAGE_APPS": true,
"PERMISSION_MANAGE_OBSERVABILITY": true,
"PERMISSION_MANAGE_CHECKOUTS": true,
"PERMISSION_HANDLE_CHECKOUTS": true,
"PERMISSION_HANDLE_TAXES": true,
"PERMISSION_MANAGE_TAXES": true,
"PERMISSION_MANAGE_CHANNELS": true,
"PERMISSION_MANAGE_DISCOUNTS": true,
"PERMISSION_MANAGE_GIFT_CARD": true,
"PERMISSION_MANAGE_MENUS": true,
"PERMISSION_MANAGE_ORDERS": true,
"PERMISSION_MANAGE_PAGES": true,
"PERMISSION_MANAGE_PAGE_TYPES_AND_ATTRIBUTES": true,
"PERMISSION_HANDLE_PAYMENTS": true,
"PERMISSION_MANAGE_PLUGINS": true,
"PERMISSION_MANAGE_PRODUCTS": true,
"PERMISSION_MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES": true,
"PERMISSION_MANAGE_SHIPPING": true,
"PERMISSION_MANAGE_SETTINGS": true,
"PERMISSION_MANAGE_TRANSLATIONS": true
},
"query": "query ProductList($first: Int, $after: String, $last: Int, $before: String, $filter: ProductFilterInput, $channel: String, $sort: ProductOrder, $hasChannel: Boolean!, $hasSelectedAttributes: Boolean!) {\n products(\n before: $before\n after: $after\n first: $first\n last: $last\n filter: $filter\n sortBy: $sort\n channel: $channel\n ) {\n edges {\n node {\n ...ProductWithChannelListings\n updatedAt\n attributes @include(if: $hasSelectedAttributes) {\n ...ProductListAttribute\n __typename\n }\n __typename\n }\n __typename\n }\n pageInfo {\n hasPreviousPage\n hasNextPage\n startCursor\n endCursor\n __typename\n }\n totalCount\n __typename\n }\n}\n\nfragment ProductWithChannelListings on Product {\n id\n name\n thumbnail {\n url\n __typename\n }\n productType {\n id\n name\n hasVariants\n __typename\n }\n channelListings {\n ...ChannelListingProductWithoutPricing\n pricing @include(if: $hasChannel) {\n priceRange {\n ...PriceRange\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n}\n\nfragment ChannelListingProductWithoutPricing on ProductChannelListing {\n isPublished\n publicationDate\n isAvailableForPurchase\n availableForPurchase\n visibleInListings\n channel {\n id\n name\n currencyCode\n __typename\n }\n __typename\n}\n\nfragment PriceRange on TaxedMoneyRange {\n start {\n net {\n ...Money\n __typename\n }\n __typename\n }\n stop {\n net {\n ...Money\n __typename\n }\n __typename\n }\n __typename\n}\n\nfragment Money on Money {\n amount\n currency\n __typename\n}\n\nfragment ProductListAttribute on SelectedAttribute {\n attribute {\n id\n __typename\n }\n values {\n ...AttributeValue\n __typename\n }\n __typename\n}\n\nfragment AttributeValue on AttributeValue {\n id\n name\n slug\n file {\n ...File\n __typename\n }\n reference\n boolean\n date\n dateTime\n value\n __typename\n}\n\nfragment File on File {\n url\n contentType\n __typename\n}\n"
}"""
get_product_list = self.client.post("/graphql/", data=json.dumps(json.loads(data)), headers=self.login_header)
# print(get_product_list.json())
# @task
# def logout(self):
# self.http.post("/graphql/")
class TestPerformance(FastHttpUser):
tasks=[GetProductListSeqTaskSet,]
# locust_task_weight = 10
# 定义虚拟用户执行每个task的间隔时间,1s到5s随机选一个值。
wait_time=between(1.0, 5.0)
if __name__ == '__main__':
import os
os.system('locust -f {0} --host=http://192.168.8.208:8000'.format(__file__))
Locust性能测试从入门到精通(一)
最新推荐文章于 2024-04-29 22:45:47 发布