Skip to content

Commit 9b68218

Browse files
authored
Merge pull request #45 from omnia-network/tests/fix-load-tests
tests: fix load tests
2 parents f976134 + 8d945d6 commit 9b68218

File tree

19 files changed

+243
-70
lines changed

19 files changed

+243
-70
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Setup DFX
2+
description: Setup DFX
3+
4+
runs:
5+
using: 'composite'
6+
steps:
7+
- name: Setup DFX
8+
uses: dfinity/setup-dfx@main
9+
with:
10+
dfx-version: 0.24.1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Setup Node.js
2+
description: Setup Node.js
3+
4+
runs:
5+
using: 'composite'
6+
steps:
7+
- uses: actions/setup-node@v4
8+
with:
9+
node-version: 20
10+
cache: "npm"
11+
cache-dependency-path: "tests/package-lock.json"

.github/workflows/integration-tests.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: Integration tests
22

3-
# only run when a commit is pushed to the main branch
43
on:
54
push:
65
branches:
@@ -20,15 +19,9 @@ jobs:
2019
with:
2120
cache-key: "build-native"
2221

23-
- uses: actions/setup-node@v4
24-
with:
25-
node-version: 20
26-
cache: "npm"
27-
cache-dependency-path: "tests/package-lock.json"
22+
- uses: ./.github/actions/setup-node
2823

29-
- uses: dfinity/setup-dfx@main
30-
with:
31-
dfx-version: 0.24.1
24+
- uses: ./.github/actions/setup-dfx
3225

3326
- name: Install mops
3427
run: npm i ic-mops -g
@@ -37,7 +30,7 @@ jobs:
3730
run: npx mocv use latest
3831

3932
- name: Prepare environment for integration tests
40-
run: ./scripts/prepare_integration_tests.sh
33+
run: ./scripts/prepare_tests.sh
4134

4235
- name: Run integration tests
4336
run: ./scripts/ci_cd_test_integration.sh

.github/workflows/load-tests.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Load tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
load-tests:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Code
16+
uses: actions/checkout@v4
17+
18+
- uses: ./.github/actions/setup-rust
19+
with:
20+
cache-key: "build-native"
21+
22+
- uses: ./.github/actions/setup-node
23+
24+
- uses: ./.github/actions/setup-dfx
25+
26+
- name: Prepare environment for load tests
27+
run: ./scripts/prepare_tests.sh
28+
29+
- name: Execute load tests
30+
run: ./scripts/ci_cd_test_load.sh
31+
32+
- name: Upload load test results
33+
if: always() # This ensures the upload happens even if tests fail
34+
uses: actions/upload-artifact@v4
35+
with:
36+
name: load-test-results
37+
path: tests/reports/gateway_load_tests.json

.github/workflows/rust-tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: Rust tests
22

3-
# only run when a commit is pushed to the main branch
43
on:
54
push:
65
branches:

README.md

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ Make sure you have the **Rust toolchain** installed. You can find instructions [
1919
1. Run the gateway:
2020

2121
In **debug** mode:
22+
2223
```bash
2324
cargo run
2425
```
26+
2527
In **release** mode:
28+
2629
```bash
2730
cargo build --release
2831
@@ -77,6 +80,7 @@ docker run -p 8080:8080 omniadevs/ic-websocket-gateway --ic-network-url http://h
7780
```
7881

7982
> Note: if you're on an ARM machine, you have to add the `--platform` flag to the command above, since the published image is built only for the `x86_64` architecture:
83+
>
8084
> ```bash
8185
> docker run --platform linux/amd64 -p 8080:8080 omniadevs/ic-websocket-gateway --ic-network-url http://host.docker.internal:4943
8286
> ```
@@ -89,9 +93,9 @@ Make sure you have [Docker Compose](https://docs.docker.com/compose/install/) in
8993
9094
The following compose files are available:
9195
92-
- [docker-compose.yml](./docker-compose.yml)
93-
- [docker-compose-local.yml](./docker-compose-local.yml)
94-
- [docker-compose-prod.yml](./docker-compose-prod.yml)
96+
- [docker-compose.yml](./docker-compose.yml)
97+
- [docker-compose-local.yml](./docker-compose-local.yml)
98+
- [docker-compose-prod.yml](./docker-compose-prod.yml)
9599
96100
The following sections describe how to use the different compose files to run the gateway with Docker Compose.
97101
@@ -100,36 +104,34 @@ A visual representation of the containers in the compose files is provided in th
100104
### Local
101105
102106
To run the gateway in a local environment with Docker Compose, follow these steps:
107+
103108
1. To run all the required local structure you can execute the [start_local_docker_environment.sh](./scripts/start_local_docker_environment.sh) with the command:
104-
105-
```
106-
./scripts/start_local_docker_environment.sh
109+
110+
```
111+
./scripts/start_local_docker_environment.sh
107112
```
108113
109114
This script simply run a dfx local replica and execute the gateway with the following command:
110115
111116
```
112117
docker compose -f docker-compose.yml -f docker-compose-local.yml --env-file .env.local up -d --build
113118
```
114-
119+
115120
2. To stop and clean all the local environment, the bash script [stop_local_docker_environment.sh](./scripts/stop_local_docker_environment.sh) is provided. You can execute it with the command:
116-
117-
```
118-
./scripts/stop_local_docker_environment.sh
121+
122+
```
123+
./scripts/stop_local_docker_environment.sh
119124
```
120125
121126
3. The Gateway will print its principal in the container logs, just as explained in the [Standalone](#standalone) section.
122-
4. If you want to verify that everything started correctly, the bash script [run_test_canister.sh](./scripts/run_test_canister.sh) is provided. This script assumes that the gateway is already running and reachable locally. You can execute it with the command:
123-
124-
```
125-
./scripts/run_test_canister.sh
126-
```
127+
4. You can verify that things are working properly by running the tests, see [Testing](#Testing)
127128
128129
### Production
129130
130131
This configuration uses the [omniadevs/ic-websocket-gateway](https://hub.docker.com/r/omniadevs/ic-websocket-gateway) image.
131132
132133
To run the gateway in a production environment with Docker Compose, follow these steps:
134+
133135
1. Set the environment variables:
134136
135137
```
@@ -140,21 +142,22 @@ To run the gateway in a production environment with Docker Compose, follow these
140142
3. Open the `443` port (or the port that you set in the `LISTEN_PORT` environment variable) on your server and make it reachable from the Internet.
141143
4. To run all the required production containers you can execute the [start_prod_docker_environment.sh](./scripts/start_prod_docker_environment.sh) with the command:
142144
143-
```
144-
./scripts/start_prod_docker_environment.sh
145+
```
146+
./scripts/start_prod_docker_environment.sh
145147
```
146148
147-
This script first generates the `telemetry/prometheus/prometheus-prod.yml` config file from the `telemetry/prometheus/prometheus-template.yml` template (step required to perform the environment variables substitution) and then runs the gateway with the following command:
149+
This script first generates the `telemetry/prometheus/prometheus-prod.yml` config file from the `telemetry/prometheus/prometheus-template.yml` template (step required to perform the environment variables substitution) and then runs the gateway with the following command:
148150
149151
```
150152
docker compose -f docker-compose.yml -f docker-compose-prod.yml --env-file .env up -d
151153
```
152154
153155
5. To stop and clean the containers, the bash script [stop_prod_docker_environment.sh](./scripts/stop_prod_docker_environment.sh) is provided. You can execute it with the command:
154156
155-
```
156-
./scripts/stop_prod_docker_environment.sh
157157
```
158+
./scripts/stop_prod_docker_environment.sh
159+
```
160+
158161
6. The Gateway will print its principal in the container logs, just as explained in the [Standalone](#standalone) section.
159162
160163
#### Obtain a TLS certificate
@@ -198,13 +201,15 @@ The gateway uses the [opentelemetry](https://docs.rs/opentelemetry) crate and [G
198201
If you're deploying the gateway with Docker (see the [Docker](#docker) section), make sure you set the following varibales in the `.env` file:
199202

200203
**Local**:
204+
201205
```
202206
OPENTELEMETRY_COLLECTOR_ENDPOINT=grpc://otlp_collector:4317
203207
GRAFANA_TEMPO_ENDPOINT=tempo:4318
204208
GRAFANA_TEMPO_LOCAL=true
205209
```
206210

207211
**Production**:
212+
208213
```
209214
OPENTELEMETRY_COLLECTOR_ENDPOINT=grpc://otlp_collector:4317
210215
GRAFANA_TEMPO_ENDPOINT=your-grafana-cloud-tempo-endpoint
@@ -240,19 +245,21 @@ After installing Node.js and dfx, you can run the integration tests as follows:
240245

241246
1. Prepare the test environment by installing dependencies and building the components by running the following command:
242247
```
243-
./scripts/prepare_integration_tests.sh
248+
./scripts/prepare_tests.sh
244249
```
245250
2. Set the environment variables:
246251
```
247252
cp tests/.env.example tests/.env
248253
```
249-
When running the tests, the `tests/.env` file is modified by dfx, which will add some variables.
254+
When running the tests, the `tests/.env` file will be modified by dfx to add some variables.
250255
3. Run integration tests using the Rust test canister:
256+
251257
```
252258
./scripts/integration_test_rs.sh
253259
```
254260

255-
If you instead want to run tests using the Motoko test canister, run the following command instead:
261+
If you want to run tests using the Motoko test canister, run the following command instead:
262+
256263
```
257264
./scripts/integration_test_mo.sh
258265
```
@@ -263,21 +270,21 @@ Tests canisters used in the integration tests can be found in the [tests/src/tes
263270

264271
### Local test script
265272

266-
After setting up and running the tests for the first time following the steps above, you can use the following command to run the unit and integration tests (using Rust test canister) together:
273+
After setting up and running the tests for the first time following the steps above, you can use the following command to run the unit and integration tests (using both the Rust and Motoko test canisters):
267274

268275
```
269276
./scripts/local_test.sh
270277
```
271278

272279
### Load tests
273280

274-
Load tests are provided in the [tests/src/load](./tests/src/load/) folder. You can run them with:
281+
We use [Artillery](https://github.com/artilleryio/artillery) to run load tests. The load test configuration is provided in the [tests/gateway_load_tests.yml](./tests/gateway_load_tests.yml) file and the source code is provided in the [tests/src/load](./tests/src/load/) folder. You can run them with:
275282

276283
```
277284
./scripts/run_load_test.sh
278285
```
279286

280-
This script requires you to set up the test environment manually, because you usually want to keep an eye on the logs of the different components. You have to start the local replica, start the gateway and deploy the test canister. The [scripts/integration_test_rs.sh](./scripts/integration_test_rs.sh) is a good reference for how to do that.
287+
This script requires you to set up the test environment manually, because you usually want to keep an eye on the logs of the different components. You have to start the local replica, start the gateway and deploy the test canister. The [scripts/ci_cd_test_load.sh](./scripts/ci_cd_test_load.sh) is a good reference for how to do that.
281288

282289
# How it works
283290

scripts/ci_cd_test_integration.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
# requires running the prepare_integration_tests.sh script first
5+
# requires running the prepare_tests.sh script first
66

77
echo "Starting local replica..."
88
dfx start --clean --background

scripts/ci_cd_test_load.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# requires running the prepare_tests.sh script first
6+
7+
echo "Starting local replica..."
8+
dfx start --clean > /dev/null 2>&1 &
9+
10+
echo "Waiting for replica to start..."
11+
sleep 10
12+
13+
echo "Starting gateway in the background..."
14+
cargo run --release > /dev/null 2>&1 &
15+
gateway_pid=$!
16+
17+
echo "Waiting for gateway to start..."
18+
sleep 1
19+
20+
export DFX_NETWORK=local
21+
export WS_GATEWAY_URL=ws://127.0.0.1:8080
22+
export IC_URL=http://127.0.0.1:4943
23+
export FETCH_IC_ROOT_KEY=true
24+
25+
echo "Deploying test canister (Rust)..."
26+
cd tests
27+
dfx deploy test_canister_rs --no-wallet
28+
# generate test_canister JS declarations,
29+
# which will be used for both Rust and Motoko tests
30+
npm run generate:test_canister_rs
31+
32+
# Compile load test script
33+
echo "Compiling load test script..."
34+
npm run load:bundle
35+
36+
# Create reports directory if it doesn't exist
37+
mkdir -p reports
38+
39+
# Run load tests
40+
echo "Running load tests..."
41+
LOG_LEVEL=error npx artillery run gateway_load_tests.yml --output reports/gateway_load_tests.json
42+
43+
echo "Results:"
44+
cat reports/gateway_load_tests.json
45+
46+
# Check that no users have failed
47+
echo -e "\nChecking that no users have failed..."
48+
failed=$(jq '.aggregate.counters."vusers.failed"' reports/gateway_load_tests.json)
49+
if [ "$failed" != 0 ]; then
50+
echo "ERROR: Load test failed with $failed users failing."
51+
exit 1
52+
else
53+
echo "All users were successful."
54+
fi
55+
56+
echo -e "\nStopping gateway..."
57+
kill $gateway_pid
58+
59+
echo "Stopping local replica..."
60+
dfx stop

scripts/integration_test_mo.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
# requires running the prepare_tests.sh script first
4+
35
echo "Starting local replica"
46
dfx start --clean --background
57

@@ -9,6 +11,7 @@ pid=$!
911

1012
echo "Deploying Motoko test canister"
1113
cd tests
14+
npx mops install
1215
npm run generate:test_canister_mo
1316
dfx deploy test_canister_mo --no-wallet
1417

scripts/integration_test_rs.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
# requires running the prepare_tests.sh script first
4+
35
echo "Starting local replica"
46
dfx start --clean --background
57

0 commit comments

Comments
 (0)