@ably-labs/locust 中文文档教程
ably-locust
Locust.io 的 JavaScript 负载生成器。
Ably 是支持实时同步数字体验的平台。 无论是在虚拟场所参加活动、接收实时财务信息,还是监控实时汽车性能数据——消费者只是希望将实时数字体验作为标准。 Ably 提供一套 API,每月为 80 个国家/地区的 2.5 亿多台设备实时构建、扩展和交付强大的数字体验。 Bloomberg、HubSpot、Verizon 和 Hopin 等组织依靠 Ably 的平台在全球范围内卸载日益复杂的关键业务实时数据同步。 有关详细信息,请参阅 Ably 文档。
Quick Start
请参阅example/users .ts 是在 TypeScript 中定义 Locust 用户的示例,example/index.ts 是连接到 Locust master 和在负载测试期间运行定义的用户。
可以通过将 example/.env.sample
复制到 example/.env
,将 ABLY_API_KEY
设置为您的 Ably API 密钥,然后运行Docker Compose:
cd example
cp .env.sample .env
# ... edit ABLY_API_KEY in .env ...
docker compose up --build
访问位于 http://localhost:8089 的 Locust web UI,开始负载测试,您应该看到 < JavaScript 用户报告的 code>subscribe 和 publish
事件。
Locust
Locust 是一款开源负载测试工具,支持两种运行模式。 第一种是独立模式,其中有一个 Locust 进程生成负载、收集统计信息并为 Web UI 提供服务,第二种是分布式模式,其中有一个 Locust 主进程为 Web UI 提供服务,但也将 ZeroMQ 套接字暴露给与 Locust worker 进程通信,后者生成负载并将统计信息报告回 Locust master。 ably-locust
希望 Locust 以分布式模式运行。
Locust 有一个用户的概念,它在负载测试期间运行一组请求。 用户定期报告有关出现在 Web UI 中的那些任务的统计信息(例如,如果他们成功或失败,遇到什么延迟,内容大小等)。 Locust 用户通常在名为 locustfile.py
的文件中用 Python 代码定义,并作为命令行标志传递给 Locust,而使用 ably-locust
时,用户在 JavaScript 代码中定义>。
Usage
ably-locust
是 Locust 工作进程在 JavaScript 中的实现。 它可以通过运行来安装:
npm install @ably-labs/locust
ably-locust
不是像 Locust Python worker 那样通过命令行标志接受用户定义的程序,而是一个在包含程序中使用的库,该程序还包括用户定义。
Worker
类使用 Locust master ZeroMQ 套接字的 URI 实例化,并向 worker 注册一个函数以实例化 Locust master 配置的每个用户类(即 < code>locustfile.py 传递给继承自 Locust User
类的 Locust master,参见 此处)。 worker 然后通过 ZeroMQ 套接字发送和接收 msgpack 编码的消息与 Locust master 交互。
worker 收到的消息之一是 spawn
消息,它声明在任何给定时间每个类应该运行多少用户,并且 worker 通过适当地启动和/或停止用户来响应。
例如,假设有一个 Locust master 在 locust.example.com:5557
运行,它使用以下 locustfile.py
:
from locust import User
class ExampleUser(User):
pass
那么这里是一个示例程序,其中每个用户报告每秒一个成功的请求:
const { Worker } = require('@ably-labs/locust');
const worker = new Worker({
locustUri: 'tcp://locust.example.com:5557',
workerID: 'my-worker-123',
});
class User {
start() {
this.interval = setInterval(() => {
const latency = 100;
const contentLength = 1024;
worker.stats.logRequest('my-request-type', latency, contentLength);
}, 1000);
}
stop() {
clearInterval(this.interval);
}
}
worker.register('ExampleUser', () => new User());
worker.run();
Testing
启动一个 Locust 实例并运行测试:
cd tests
docker compose up --build
npm test
ably-locust
A JavaScript load generator for Locust.io.
Ably is the platform that powers synchronized digital experiences in realtime. Whether attending an event in a virtual venue, receiving realtime financial information, or monitoring live car performance data – consumers simply expect realtime digital experiences as standard. Ably provides a suite of APIs to build, extend, and deliver powerful digital experiences in realtime for more than 250 million devices across 80 countries each month. Organizations like Bloomberg, HubSpot, Verizon, and Hopin depend on Ably’s platform to offload the growing complexity of business-critical realtime data synchronization at global scale. For more information, see the Ably documentation.
Quick Start
See example/users.ts for an example of defining Locust users in TypeScript, and example/index.ts for an example program which connects to a Locust master and runs the defined users during a load test.
The example can be run by copying example/.env.sample
to example/.env
, setting ABLY_API_KEY
to your Ably API key, and running Docker Compose:
cd example
cp .env.sample .env
# ... edit ABLY_API_KEY in .env ...
docker compose up --build
Visit the Locust web UI at http://localhost:8089, start a load test, and you should see subscribe
and publish
events reported by the JavaScript users.
Locust
Locust is an open source load testing tool which supports two modes of operation. The first is standalone mode where there is a single Locust process generating the load, collecting statistics, and serving the web UI, and the second is distributed mode where there is a Locust master process serving the web UI, but also exposing a ZeroMQ socket to communicate with Locust worker processes which generate the load and report statistics back to the Locust master. ably-locust
expects Locust to be running in distributed mode.
Locust has the concept of a User which runs a set of requests during a load test. The User periodically reports statistics about those tasks which appear in the web UI (e.g. if they succeed or fail, what latency is encountered, the content size, etc.). Locust Users are typically defined in Python code in a file named locustfile.py
and passed to Locust as a command line flag, whereas Users are defined in JavaScript code when using ably-locust
.
Usage
ably-locust
is an implementation of a Locust worker process in JavaScript. It can be installed by running:
npm install @ably-labs/locust
Rather than being a program which accepts user definitions via a command line flag like a Locust Python worker, ably-locust
is a library which is used within a containing program which also includes the user definition.
The Worker
class is instantiated with the URI of the Locust master ZeroMQ socket, and a function is registered with the worker to instantiate each of the user classes the Locust master is configured with (i.e. any classes in the locustfile.py
passed to the Locust master which inherit from the Locust User
class, see here). The worker then interacts with the Locust master by sending and receiving msgpack-encoded messages over the ZeroMQ socket.
One of the messages a worker receives is a spawn
message which declares how many users of each class should be running at any given time, and the worker responds by starting and/or stopping users as appropriate.
For example, assuming there is a Locust master running at locust.example.com:5557
which is using the following locustfile.py
:
from locust import User
class ExampleUser(User):
pass
Then here's an example program where each user reports a successful request every second:
const { Worker } = require('@ably-labs/locust');
const worker = new Worker({
locustUri: 'tcp://locust.example.com:5557',
workerID: 'my-worker-123',
});
class User {
start() {
this.interval = setInterval(() => {
const latency = 100;
const contentLength = 1024;
worker.stats.logRequest('my-request-type', latency, contentLength);
}, 1000);
}
stop() {
clearInterval(this.interval);
}
}
worker.register('ExampleUser', () => new User());
worker.run();
Testing
Start an instance of Locust and run the tests:
cd tests
docker compose up --build
npm test