locust

------------恢复内容开始------------

from locust import  HttpLocust,task,TaskSet
import time
import requests
import hashlib
import hmac
import random


HOST=" "
baseUrl="http://%s"%HOST
login_html=baseUrl+"/bs/rule/loan/rs/generalfunction/get"
user_login_url=baseUrl+"/bs/rule/loan/j_spring_security_check"

'''查询规则'''
query_rule_url_01=baseUrl+"/bs/rule/loan/rs/sd/config/event/listChannelEvent?returnEmptyChild=false"
query_rule_url_02=baseUrl+"/bs/rule/loan/rs/sd/config/pkg/category/listPkgCate"
query_rule_url_03=baseUrl+"/bs/rule/loan/rs/sd/hit/rule/listRules"
'''查询特征'''
query_feater_url=baseUrl+"/bs/rule/loan/rs/sd/feature/list?"
'''查询机构'''
query_org_url=baseUrl+"/bs/rule/loan/rs/org?"
'''查询角色'''
query_character_url=baseUrl+"/bs/rule/loan/rs/role?"
'''查询用户'''
query_user_url=baseUrl+"/bs/rule/loan/rs/role?"



def jiami_sha256(value):
    sha256=hashlib.sha256()
    sha256.update(value.encode("utf-8"))
    res=sha256.hexdigest()
    return res

def getLoginSession(username,password):
    r=requests.get(login_html)
    loginSession=r.cookies.get_dict()["SESSION"]
    cookie="theme=bangsun; portal_biz_line=LOAN; Language=zh-CN; SESSION="+loginSession
    # print (cookie)
    headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        , "Accept-Encoding":"gzip, deflate"
        , "Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"
        , "Cache-Control":"max-age=0"
        , "Connection":"keep-alive"
        , "Host":HOST
        ,"cookie":cookie,
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
        , "If-Modified-Since":"Thu, 28 Nov 2019 07:45:02 GMT"
        , "Upgrade-Insecure-Requests":"1"
        }
    passwd=jiami_sha256(password)
    data={
        "username":username,
        "password":passwd
    }
    r1=requests.post(url=user_login_url,headers=headers,data=data)
    if "true" in r1.text:
        print ("用户:%s登录成功"%username)
    else:
        print (r1.text)
    return "theme=bangsun; portal_biz_line=LOAN; Language=zh-CN; SESSION="+r1.cookies.get_dict()["SESSION"]



class UserBehavior(TaskSet):
    def on_start(self):
        self.cookie = getLoginSession("admin","bangsun")
        self.headers = {"Accept": "*/*"
            , "Accept-Encoding": "gzip, deflate"
            , "Accept-Language": "zh-CN,zh;q=0.9"
            , "Connection": "keep-alive"
            , "Cookie": self.cookie
            , "Host": HOST
            ,
                   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
            , "X-Requested-With": "XMLHttpRequest"
            , "Content-Type": "application/json"
            , "Content-Length": "120"
                }
    @task(2)  #查询规则
    def test_query_rule(self):
        data = {"start":1,"limit":25,"keyword":"tyxtyx","cateId":"",
                "orgId":"","pkgType":"HIT,SCORE","orgType":"belong",
                "ruleType":"HIT,SCORE","tabCode":"COMMON","pkgId":""}
        data["keyword"]="测试规则%s"%str(random.randint(0,100))
        self.client.post(url=query_rule_url_03, data=data, headers=self.headers,name='规则页面:查询规则接口1')
        self.client.get(url=query_rule_url_01, data=data, headers=self.headers, name='规则页面:查询规则接口2')
        self.client.get(url=query_rule_url_02, data=data, headers=self.headers, name='规则页面:查询规则接口3')

    @task(2) #查询特征
    def test_query_feater(self):
        data = {"type": "",
                "enabled": "",
                "keyword": "测试%s"%str(random.randint(0,100)),
                "groupId": "",
                "limit": "15",
                "start": "1"}
        self.client.get(url=query_feater_url,data=data,headers=self.headers,name="特征管理页面,查询特征接口")
    @task(2) #查询机构
    def test_query_org(self):
        data={"keyword":"Orgname_%s"%str(random.randint(1,100)),
              "orgId":"",
              "start":"1",
              "limit":"25",
        }
        self.client.get(url=query_org_url,data=data,headers=self.headers,name="系统配置页面:机构查询接口")
    @task(2) #查询角色
    def test_query_character(self):
        data = {"keyword":"",
                "orgId":"",
                "start":"0",
                "limit":"0"
                }
        data1 = {"keyword":"TestCharacter%s"%str(random.randint(1,400)),
                "orgId":"",
                "start":"1",
                "limit":"25"
                }
        self.client.get(url=query_character_url,data=data,headers=self.headers,name="系统配置页面:角色查询接口1")
        self.client.get(url=query_character_url,data=data1,headers=self.headers,name="系统配置页面:角色查询接口2")
    @task(2)#查询用户
    def test_query_user1(self):
        data = {"keyword":"",
                "orgId":"",
                "start":"0",
                "limit":"0"
                }
        data1 = {"keyword": "testuser%s" % str(random.randint(1, 400)),
                 "orgId":"",
                 "roleName":"",
                 "start":"1",
                 "limit":"25"
                 }
        self.client.get(url=query_user_url,data=data,headers=self.headers,name="系统配置页面:用户查询接口1")
        self.client.get(url=query_user_url,data=data1,headers=self.headers,name="系统配置页面:用户查询接口2")


class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    host='http://%s'%HOST
    min_wait = 2000
    max_wait = 4000

  @task()控制执行权限,   on_start( ) 类似于 setup    

------------恢复内容结束------------

posted @ 2020-07-21 15:51  强小白  阅读(160)  评论(0编辑  收藏  举报