环境:python3.7,locust1.0.0.0,macOS
搜索地址:在https://www.baidu.com 通过chrome抓的。
命令行运行命令 :locust -f run.py --host=https://www.baidu.com
""" TODO:主类 """ import random from locust import HttpUser,TaskSet,task class UserBehavior(TaskSet): def on_start(self): ''' 在task之前运行,每个虚拟用户执行一次 :return: ''' # 搜索地址 self.searchUrl = "/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&" \ "sugsid=32757,1458,32843,7567,31660,32706,32230,7517,32270,32117&req=2&" \ "csor=4&pwd=439&cb=jQuery110207068834180420038_1603353005552&_=1603353005579" # 搜索参数 self.searchWord = [ "共和国之辉", "阿婆克烈", "圣小泽玛利亚", ] @task(1) def baidu(self): ''' 模拟用户搜索 :return: ''' # 随机参数 ranIndex = random.randint(1, 1000) % len(self.searchWord) # 拼装 搜索地址 和 搜索参数 url = self.searchUrl + "&wd=" + self.searchWord[ranIndex] # 请求结果并打印 re = self.client.get(url, catch_response=True) print(re.text) class WebsiteUser(HttpUser): tasks = [UserBehavior] min_wait = 1000 max_wait = 2000
