ローカストパフォーマンス-ゼロベースのエントリシリーズ(9)-SequentialTaskSetアプリケーション

最初の部分:
ランダムな現象ではなく、トラフィックのプレッシャーの下で複数のテストタスクを実行するときに、複数のユーザー(スレッド)が特定のシーケンスに従うようにする方法。この要件はLocustでも実現でき、タスクセットはTaskSetのサブクラスSequentialTask​​Setから継承できます。この使用法を説明する例を見てください。

from locust import User,SequentialTaskSet,task,between

def function_task(l):
    print("This is the function task")

class MyUser(User):
    wait_time = between(5,9)
    @task
    class SequenceOfTasks(SequentialTaskSet):
        @task
        def first_task(self):
            print("this is the first task")

        tasks = [function_task]

        @task
        def second_task(self):
            print("this is the second task")

        @task
        def third_task(self):
            print("this is the third task")

上記のコード例では、SequeneOfTasksクラスがMyUserクラスに埋め込まれています。SequentialTask​​Set設定の意味から、この例のタスク実行順序は次のようになります。

  • first_task

  • function_task

  • second_task

  • third_task

スクリプトによって実行されるコンソール出力は次のとおりです。

 All users hatched: MyUser: 1 (0 already running)
this is the first task
This is the function task
this is the second task
this is the third task
this is the first task
This is the function task
this is the second task
this is the third task
this is the first task
This is the function task
...

パート2:
仮想ユーザーが最初と最後に実行するコードブロックを構成するにはどうすればよいですか?以前のツールLoadRunnerでは、lr_startとlr_endを設定できることがわかっています。その後、ローカストでも可能です。ここで説明する方法は2つあります。1つはユーザークラスレベルで、もう1つはモジュールレベルです。次に、これら2つの方法について詳しく説明します。

1>ユーザークラスレベル

最初のコード:

from locust import User,task,between

class MyUser(User):
    wait_time = between(5,8)

    def on_start(self):
        print("A test will start...")

    def on_stop(self):
        print("A test is ending...")

    @task
    def task_1(self):
        print("it is task1")

    @task
    def task_2(self):
        print("it is task2")

上記のコードからわかるように、すべての仮想ユーザーはセットアップ中にon_start関数を実行し、ティアダウン中にon_stop関数を実行します。実行結果(5人の仮想ユーザーがシナリオを20秒間実行)から、コンソール出力は次のようになります。

Hatching and swarming 5 users at the rate 5 users/s (0 users already running)...
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

A test will start...
it is task1
A test will start...
it is task1
A test will start...
it is task2
A test will start...
it is task2
[2020-11-20 08:52:18,681] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: MyUser: 5 (0 already running)
A test will start...
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
it is task2
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task2
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task2
it is task2
it is task1
[2020-11-20 08:52:37,696] jasondeMacBook-Pro.local/INFO/locust.main: Time limit reached. Stopping Locust.
A test is ending...
A test is ending...
A test is ending...
A test is ending...
A test is ending...
[2020-11-20 08:52:37,697] jasondeMacBook-Pro.local/INFO/locust.main: Running teardowns...
[2020-11-20 08:52:37,698] jasondeMacBook-Pro.local/INFO/locust.main: Shutting down (exit code 0), bye.
[2020-11-20 08:52:37,698] jasondeMacBook-Pro.local/INFO/locust.main: Cleaning up runner...
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

Percentage of the requests completed within given times
 Type                 Name                                                           # reqs    50%    66%    75%    80%    90%    95%    98%    99%  99.9% 99.99%   100%
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------

2>モジュールレベル。

module / locustファイルに複数のクラスがある場合はどうなりますか?モジュールレベルでon_startとon_stopを設定することにより、コードの量を減らすことができます。上記のコードも元のコードとして書き直されています。書き直されたバージョンは次のとおりですが、注意してください⚠️:on_startとon_stopは、仮想ユーザーの数に関係なく1回だけ実行されるため、必要に応じて別のモードを選択してください。このパターンのラベルはどこに

@ events.test_stop.add_listenerが鍵です。

from locust import events,User,task,between

@events.test_start.add_listener
def on_start(**kwargs):
    print("A test will start...")

@events.test_stop.add_listener
def on_stop(**kwargs):
    print("A test is ending...")

class MyUser(User):
    wait_time = between(5,8)
    @task
    def task_1(self):
        print("it is task1")

    @task
    def task_2(self):
        print("it is task2")

ローカストパフォーマンス-ゼロベースのエントリシリーズ(9)-SequentialTask​​Setアプリケーション

おすすめ

転載: blog.51cto.com/13734261/2571136