イナゴを知る
1. Locustの基本的な紹介
1.1はじめに
現在、インターネット業界でも従来の業界でも、パフォーマンスの要件は日々高まっ
ています。問題をより迅速かつ正確に特定し、問題を見つけて解決するために、
Jmeterなどのパフォーマンステストツールが市場に登場しています。 、ロードランナー、軌跡など、今日は主にイナゴを紹介します!
pythonを使用している人を含め、多くの人
はlocustが何であるかを知りません。なぜならpythonを使用していないため、それについて知らないからです。次に、locustとlocustの機能、それは何ですか、
Xiaoyuに従って、下を見下ろします〜
1.2はじめに
Locustは、イベントに基づいてPythonで開発されたオープンソースのパフォーマンステストツールであり、テストの実行と結果の表示のための分散および提供Web UIをサポートしています。
1.比較
リソースは、イナゴによって占有し、JMeterのイナゴが理由よりも優れたオープンソースのJMeterをあるリソース占有の観点
:主な理由は
、ユーザーの二つのモードが異なっていて>>:
①Jmeterは、仮想ユーザとしてスレッドを使用し
②Locustはコルーチンのためgeventライブラリを使用していますサポート、グリーンレットでエミュレートします。
したがって、同じ構成で、Locustがサポートできる同時ユーザーの数は、Jmeterよりも1レベル以上高くなります。
2.
Locustは言語を使用します。LocustはPythonコードを使用してテストシナリオを定義します。現在、Python2.7、3.3〜3.7をサポートしています。
ユーザーモデルの定義、テストの開始、リアルタイムテストデータ、エラー統計などのWeb UIが付属しています。
3.公式ドキュメント詳細
な内容については、次を参照してください
。1.ドキュメントコンテンツ、クリック:公式ドキュメント
2.ソースコード、クリック:Github
2. Locsutのインストール方法
同様に、pipで直接インストールできます
2.1 Locustをインストールする
pip install locust
2.2 pyzmqをインストールする
Locustを複数のプロセス/プロセスで実行する場合は、pyzmqをインストールし、
pipを使用してインストールする必要があります
pip install pyzmq
注:locust-1.2.3バージョン
をインストールする場合、pyzmqを再度インストールする必要はないかもしれません。pyzmqで直接インストールされるようです
2.3インストールが成功したことの確認
コマンドウィンドウを開き、直接入力します
locust --help
次の画像が表示されれば、インストールは成功しています
3. Locustパラメータの説明
上図のインストールが成功したら、パラメータを分析しましょう
3.1パラメータ情報
まず、イナゴのすべての情報をコピーします--help
Common options:
-h, --help show this help message and exit
-f LOCUSTFILE, --locustfile LOCUSTFILE
Python module file to import, e.g. '../other.py'.
Default: locustfile
--config CONFIG Config file path
-H HOST, --host HOST Host to load test in the following format:
http://10.21.32.33
-u NUM_USERS, --users NUM_USERS
Number of concurrent Locust users. Primarily used
together with --headless
-r SPAWN_RATE, --spawn-rate SPAWN_RATE
The rate per second in which users are spawned.
Primarily used together with --headless
-t RUN_TIME, --run-time RUN_TIME
Stop after the specified amount of time, e.g. (300s,
20m, 3h, 1h30m, etc.). Only used together with
--headless
-l, --list Show list of possible User classes and exit
Web UI options:
--web-host WEB_HOST Host to bind the web interface to. Defaults to '*'
(all interfaces)
--web-port WEB_PORT, -P WEB_PORT
Port on which to run web host
--headless Disable the web interface, and instead start the load
test immediately. Requires -u and -t to be specified.
--web-auth WEB_AUTH Turn on Basic Auth for the web interface. Should be
supplied in the following format: username:password
--tls-cert TLS_CERT Optional path to TLS certificate to use to serve over
HTTPS
--tls-key TLS_KEY Optional path to TLS private key to use to serve over
HTTPS
Master options:
Options for running a Locust Master node when running Locust distributed. A Master node need Worker nodes that connect to it before it can run load tests.
--master Set locust to run in distributed mode with this
process as master
--master-bind-host MASTER_BIND_HOST
Interfaces (hostname, ip) that locust master should
bind to. Only used when running with --master.
Defaults to * (all available interfaces).
--master-bind-port MASTER_BIND_PORT
Port that locust master should bind to. Only used when
running with --master. Defaults to 5557.
--expect-workers EXPECT_WORKERS
How many workers master should expect to connect
before starting the test (only when --headless used).
Worker options:
Options for running a Locust Worker node when running Locust distributed.
Only the LOCUSTFILE (-f option) need to be specified when starting a Worker, since other options such as -u, -r, -t are specified on the Master node.
--worker Set locust to run in distributed mode with this
process as worker
--master-host MASTER_NODE_HOST
Host or IP address of locust master for distributed
load testing. Only used when running with --worker.
Defaults to 127.0.0.1.
--master-port MASTER_NODE_PORT
The port to connect to that is used by the locust
master for distributed load testing. Only used when
running with --worker. Defaults to 5557.
Tag options:
Locust tasks can be tagged using the @tag decorator. These options let specify which tasks to include or exclude during a test.
-T [TAG [TAG ...]], --tags [TAG [TAG ...]]
List of tags to include in the test, so only tasks
with any matching tags will be executed
-E [TAG [TAG ...]], --exclude-tags [TAG [TAG ...]]
List of tags to exclude from the test, so only tasks
with no matching tags will be executed
Request statistics options:
--csv CSV_PREFIX Store current request stats to files in CSV format.
Setting this option will generate three files:
[CSV_PREFIX]_stats.csv, [CSV_PREFIX]_stats_history.csv
and [CSV_PREFIX]_failures.csv
--csv-full-history Store each stats entry in CSV format to
_stats_history.csv file. You must also specify the '--
csv' argument to enable this.
--print-stats Print stats in the console
--only-summary Only print the summary stats
--reset-stats Reset statistics once spawning has been completed.
Should be set on both master and workers when running
in distributed mode
Logging options:
--skip-log-setup Disable Locust's logging setup. Instead, the
configuration is provided by the Locust test or Python
defaults.
--loglevel LOGLEVEL, -L LOGLEVEL
Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.
Default is INFO.
--logfile LOGFILE Path to log file. If not set, log will go to
stdout/stderr
Step load options:
--step-load Enable Step Load mode to monitor how performance
metrics varies when user load increases. Requires
--step-users and --step-time to be specified.
--step-users STEP_USERS
User count to increase by step in Step Load mode. Only
used together with --step-load
--step-time STEP_TIME
Step duration in Step Load mode, e.g. (300s, 20m, 3h,
1h30m, etc.). Only used together with --step-load
Other options:
--show-task-ratio Print table of the User classes' task execution ratio
--show-task-ratio-json
Print json data of the User classes' task execution
ratio
--version, -V Show program's version number and exit
--exit-code-on-error EXIT_CODE_ON_ERROR
Sets the process exit code to use when a test result
contain any failure or error
-s STOP_TIMEOUT, --stop-timeout STOP_TIMEOUT
Number of seconds to wait for a simulated user to
complete any executing task before exiting. Default is
to terminate immediately. This parameter only needs to
be specified for the master process when running
Locust distributed.
User classes:
UserClass Optionally specify which User classes that should be
used (available User classes can be listed with -l or
--list)
3.2パラメータ情報分析
ここではほとんどの部分しか書かれていませんが、不足している部分については小余はあまり使われていないので電力を無駄にしないと思います〜やっぱり1キロワット時の電力は1元以上!!
シャオユもランチします〜
-h, --help 查看帮助
-H HOST, --host=HOST 指定被测试的主机,采用以格式:http://10.21.32.33
--web-host=WEB_HOST 指定运行 Locust Web 页面的主机,默认为空 ''。
-P PORT, --port=PORT, --web-port=PORT 指定 --web-host 的端口,默认是8089
-f LOCUSTFILE, --locustfile=LOCUSTFILE 指定运行 Locust 性能测试文件,默认为: locustfile.py
--csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE 以CSV格式存储当前请求测试数据。
--master Locust 分布式模式使用,当前节点为 master 节点。
--slave Locust 分布式模式使用,当前节点为 slave 节点。
--master-host=MASTER_HOST 分布式模式运行,设置 master 节点的主机或 IP 地址,只在与 --slave 节点一起运行时使用,默认为:127.0.0.1.
--master-port=MASTER_PORT 分布式模式运行, 设置 master 节点的端口号,只在与 --slave 节点一起运行时使用,默认为:5557。注意,slave 节点也将连接到这个端口+1 上的 master 节点。
--master-bind-host=MASTER_BIND_HOST Interfaces (hostname, ip) that locust master should bind to. Only used when running with --master. Defaults to * (all available interfaces).
--master-bind-port=MASTER_BIND_PORT Port that locust master should bind to. Only used when running with --master. Defaults to 5557. Note that Locust will also use this port + 1, so by default the master node will bind to 5557 and 5558.
--expect-slaves=EXPECT_SLAVES How many slaves master should expect to connect before starting the test (only when --no-web used).
--no-web no-web 模式运行测试,需要 -c 和 -r 配合使用.
-c NUM_CLIENTS, --clients=NUM_CLIENTS 指定并发用户数,作用于 --no-web 模式。
-r HATCH_RATE, --hatch-rate=HATCH_RATE 指定每秒启动的用户数,作用于 --no-web 模式。
-t RUN_TIME, --run-time=RUN_TIME 设置运行时间, 例如: (300s, 20m, 3h, 1h30m). 作用于 --no-web 模式。
-L LOGLEVEL, --loglevel=LOGLEVEL 选择 log 级别(DEBUG/INFO/WARNING/ERROR/CRITICAL). 默认是 INFO.
--logfile=LOGFILE 日志文件路径。如果没有设置,日志将去 stdout/stderr
--print-stats 在控制台中打印数据
--only-summary 只打印摘要统计
--no-reset-stats Do not reset statistics once hatching has been completed。
-l, --list 显示测试类, 配置 -f 参数使用
--show-task-ratio 打印 locust 测试类的任务执行比例,配合 -f 参数使用.
--show-task-ratio-json 以 json 格式打印 locust 测试类的任务执行比例,配合 -f 参数使用.
-V, --version 查看当前 Locust 工具的版本.
3.3 Locustメインライブラリ
① gevent
geventコルーチンに基づいて、Pythonのネットワークライブラリがある。これは、Greenletによって提供される高レベルの同期のAPIを使用し、libeventのイベントループをカプセル化します。
② フラスコ
Pythonで書かれた軽量のWebアプリケーションフレームワーク。
フラスコと実際のコードを理解したい場合は、Xiaoyuの記事
「Python3、ウェブサイトの構築とFlaskプロジェクトの構築」を読んでください。
③ リクエスト
PythonのHTTPライブラリ
はXiaoyuによるこの記事を参照できます「リクエストライブラリで一般的に使用される7つの主要なメソッドとアクセスパラメータの制御」
④ msgpack-パイソン
MessagePackはJSONと同様のデータフォーマットに適した高速かつコンパクトバイナリシリアライゼーションフォーマットです。msgpack-pythonは、主にMessagePackデータをシリアル化および逆シリアル化するためのメソッドを提供します。
⑤ 6
Python2および3つの互換性のあるライブラリーは、Python2とのpython3の違いをカプセル化するために使用しました
⑥ pyzmq
pyzmqは主ローカストの分散モード動作を実現するために使用されるzeromqの結合パイソン(通信キュー)です。
4. Locustクラスの説明
4.1クライアント属性
①Locustクラスでは、静的フィールドclientがクライアントのリクエストメソッドです。ここでのclientフィールドはクライアントリクエストメソッドにバインドされていません。そのため、Locustを使用する場合、最初にLocustクラスクラスHttpLocust(Locust)を継承し、次にself.client =を設定する必要がありますHttpSession(base_url = self.host)はクライアント要求メソッドをバインドします。
②For共通のHTTP(S)プロトコル、ローカストはと、HttpLocustクラスを実装しているself.client =のHttpSession(BASE_URL = self.host) 、及びrequests.SessionからのHttpSession継承。
③HTTPをテストするためのLocustスクリプトでは、Pythonリクエストライブラリのすべてのメソッドをclient属性を介して使用でき、呼び出しメソッドはreqeustsのメソッドとまったく同じです。
④に起因するの使用requests.Session、状態メモリ機能が自動的にクライアントメソッド呼び出しの間に設けられています。一般的なシナリオは、システムにログインした後、セッションをログイン状態に維持できるため、後続のHTTPリクエスト操作でログイン状態を
取得できるというものです。
4.2その他の属性
4.2.1 task_set
TaskSetクラスをポイントします。TaskSetクラスはユーザーのタスク情報を定義します。この静的フィールドは必須です。
4.2.2 max_wait / min_wait
各ユーザーが2つのタスクを実行する間隔の上限と下限(ミリ秒)。特定の値は、上限と下限からランダムに選択されます。指定されていない場合、既定の間隔は1秒です。
4.2.3ホスト
テストされたシステムのホストは、ターミナルでイナゴを開始するときに-hostパラメーターが指定されていない場合にのみ使用されます。
4.2.4重量
複数のLocustクラスを同時に実行する場合は、さまざまなタイプのタスクの実行ウェイトを制御するために使用されます。
5. Loucst実行プロセス
次のように具体的なプロセスである:
①(一度だけ)WebsiteTasks初期化として、あるon_start最初に行います。
②ランダムに実行するタスク(タスク間の重量関係が定義されている場合、ランダム重量関係に従って選択)WebsiteTasksから選択します。
③定義された時間間隔とLocustクラスmin_wait MAX_WAIT(クラスが優先的にタスクセットでタスクセットまたはmin_wait MAX_WAITも定義されている場合)、時間値の範囲でランダムに、待機待機。
④テストタスクが終了するまで、手順2〜3を繰り返します。
6.パフォーマンステストツールの比較
Xiaoyu は、パフォーマンステストツールの選択方法について、「はじめにからギブアップするまでのパフォーマンステストに関するディープトーキング:初めてのパフォーマンステスト」に書いています。
各ツールには独自の価値があります。つまり、存在は意味を意味します。
次に、Xiaoyuは分析、Jmeter、Loadrunner、Locustの3つのツールを提供します。
事業 | Jmeter | ロードランナー | イナゴ |
---|---|---|---|
認可方法 | オープンソース | 商業料金 | オープンソース |
開発言語 | ジャワ | C / Java | パイトン |
テストスクリプトフォーム | GUI | C / Java | パイソン |
同時実行メカニズム | 糸 | プロセス/スレッド | コルーチン |
単一マシンの並行性 | 低い | 低い | 高い |
分散圧力 | 待機する | 待機する | 待機する |
リソース監視 | サポートしません | 待機する | サポートしません |
レポートと分析 | シンプルなチャート | 完璧な | シンプルなチャート |
二次開発をサポート | 待機する | サポートしません | 待機する |
比較すると、Locustが支配的ではないことがわかりますが、Xiaoyuは次の
理由でこれを使用することを好み
ます:1.最初に、ユーザー操作をシミュレートします
。LocustはPure Pythonスクリプト記述を使用し、HTTPリクエストは完全にリクエストライブラリに基づいています。
②リクエストは非常に簡潔で使いやすいライブラリですが、非常に強力な機能を備えています。他のプログラミング言語の多くのHTTPライブラリは、そのアイデアとパターンを借用しています。最高のHTTPライブラリの1つとして選択された場合(言語制限なし)、それは多くの論争はありません。
③HTTP(S)プロトコルに加えて、Locustは他のプロトコルのシステムもテストでき、Pythonを使用して、要求の説明に対応するライブラリを呼び出すだけで済みます。
2.同時実行メカニズム
① イナゴの同時実行メカニズムはgeventのメカニズムを採用しています。
②マルチスレッドで複数ユーザーをシミュレートすると、同時実行数の増加に伴いスレッド数が増加し、スレッドの切り替えにはリソースが必要となりますが、IOのブロックやスレッドスリープは必然的に同時実行効率の低下につながります。このため、プロセスとスレッドを使用するLoadRunnerやJmeterなどのテストツールでは、単一のマシンで高い同時実行性のプレッシャーをシミュレートすることは困難です。
③ コルーチンとスレッドの違いは次のとおりです。コルーチンはシステムレベルのリソーススケジューリングを回避するため、パフォーマンスが大幅に向上します。
④通常の状況では、共通の構成を持つ単一のテストマシンで数千の同時圧力が発生する可能性があります。これは、LoadRunnerとJmeterでは不可能です。
7.イナゴコードの戦闘
実際のコード部分は、第2章に示します。
直接クリックして「Deep Chat Performance Test、Entry to Abandonment:Locust Performance Automation(2)Code Actual Combat」を送信できます
。
Xiao Diaosi:ゆう兄弟、最初に少し話してもらえますか、イナゴのインターフェースは何ですか?
Xiaoyu:ええと、あなたは待つことができません~~
Xiao Diaosi:私はそうは思わない、ただ写真を与えて見て、Niu Xの場所は何ですか?
Xiaoyu:さて、明日を待っています!食べなきゃ!!!