1. 程式人生 > 其它 >Locust:新舊版本對比,以及新舊版本對集合點的實現

Locust:新舊版本對比,以及新舊版本對集合點的實現

技術標籤:Locustpython

由最近的研究來看,locust大致分為新舊兩個版本:

  • 新版本以1開頭,現在最新穩定版為1.4.3;
  • 舊版本以0開頭,比如我們公司現在常用的開發版本為0.12.2.

那麼他們有什麼區別呢?目前有如下發現:

  • 安裝方式的變化:
    • 舊版本安裝命令為:pip install locustio
    • 新版本安裝命令為:pip install locust
  • 引數變化
    • 吳圖形模式啟動引數: 舊版本為--no-web 新版本為--headless
      -程式碼中類的變化

集合點

下面就新舊兩個版本,對集合點進行實現:

當前新版本:

  • Locust 1.4.3
  • gevent 21.1.2
  • greenlet 1.0.0
from locust import HttpLocust, TaskSet, task, events,HttpUser
from gevent._semaphore import Semaphore

all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

def on_hatch_complete(**kwargs):
    # 建立鉤子方法
    all_locusts_spawned.release()
# 掛在到locust鉤子函式(所有的Locust示例產生完成時觸發)
# events.hatch_complete += on_hatch_complete
events.spawning_complete.add_listener(on_hatch_complete) class Knight_Login(TaskSet): def on_start(self): all_locusts_spawned.wait() @task(1) def index(self): all_locusts_spawned.wait() url = 'https://www.baidu.com' self.client.get(url) @task(1) def
login(self): all_locusts_spawned.wait() self.client.get("http://www.baidu.com") # class Knight_User(HttpLocust): ###這是1.0之前寫法 class Knight_User(HttpUser): # task_set = Knight_Login ###這是1.0之前寫法 tasks = [Knight_Login] host = "http://www.whyfjz.com" min_wait = 1000 max_wait = 3000

舊版本實現:

  • locust 0.12.2
  • gevent 1.4.0
  • greenlet0.4.15
from locust import HttpLocust, TaskSet, task, events
from gevent._semaphore import Semaphore

all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

def on_hatch_complete(**kwargs):
    # 建立鉤子方法
    all_locusts_spawned.release()

# 掛在到locust鉤子函式(所有的Locust示例產生完成時觸發)
events.hatch_complete += on_hatch_complete
class Knight_Login(TaskSet):
    def on_start(self):
        all_locusts_spawned.wait()
    @task(1)
    def index(self):
        url = 'https://www.baidu.com'
        self.client.get(url)
    @task(1)
    def login(self):
        pass
class Knight_User(HttpLocust):
    task_set = Knight_Login
    host = "https://www.sina.com"