Skip to content

Commit 2c56945

Browse files
Memory regression tests (#275)
Add livekit_memory_tester binary with common leak scenario testing, and add gating support in CI.
1 parent 2440f75 commit 2c56945

7 files changed

Lines changed: 1186 additions & 29 deletions

File tree

‎.github/actions/test-capture-variant/action.yml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ runs:
4747
import os
4848
import pathlib
4949
import subprocess
50-
import time
5150
sources = sorted(set(os.environ["VARIANT_SOURCES"].split(",")))
5251
if not sources or any(source not in {"clock", "gstreamer", "pattern"} for source in sources):
5352
raise SystemExit("Invalid selective capture source set")
@@ -62,7 +61,6 @@ runs:
6261
os.environ["VARIANT_CONFIG_HASH"], rust_sha])
6362
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
6463
output.write(f"cargo-dir={cargo_dir.as_posix()}\nkey={key}\n")
65-
output.write(f"started-at={time.monotonic()}\n")
6664
output.write("bundle-dir=sdk-out/capture-" + "-".join(sources) + "\n")
6765
PYCODE
6866
- name: Restore variant Cargo cache
@@ -99,9 +97,6 @@ runs:
9997
shell: bash
10098
env:
10199
VARIANT_CARGO_DIR: ${{ steps.variant.outputs.cargo-dir }}
102-
VARIANT_SOURCES: ${{ inputs.sources }}
103-
VARIANT_STARTED_AT: ${{ steps.variant.outputs.started-at }}
104-
VARIANT_CACHE_HIT: ${{ steps.cache.outputs.cache-hit }}
105100
run: |
106101
set -euo pipefail
107102
variant_python=python
@@ -110,15 +105,10 @@ runs:
110105
import os
111106
import pathlib
112107
import shutil
113-
import time
114108
path = pathlib.Path(os.environ["VARIANT_CARGO_DIR"]).resolve()
115109
root = pathlib.Path(os.environ["CARGO_TARGET_DIR"]).resolve()
116110
if path.parent != root or not path.name.startswith("cpp-"):
117111
raise SystemExit("Refusing to remove a path outside the feature-specific Cargo root")
118112
if path.exists():
119113
shutil.rmtree(path)
120-
elapsed = time.monotonic() - float(os.environ["VARIANT_STARTED_AT"])
121-
cache = "hit" if os.environ["VARIANT_CACHE_HIT"] == "true" else "miss"
122-
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary:
123-
summary.write(f"Capture `{os.environ['VARIANT_SOURCES']}`: {elapsed:.0f}s; Cargo cache {cache}.\n\n")
124114
PYCODE

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
- client-sdk-rust
6868
- client-sdk-rust/**
6969
- cmake/**
70+
- scripts/memory-tester.sh
7071
- scripts/set-test-tokens.sh
7172
- scripts/run-with-backtrace.sh
7273
- scripts/setup-linux-toolchain.sh

‎.github/workflows/tests.yml‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ jobs:
380380
--gtest_output="xml:${{ env.BUILD_DIR }}\unit-test-results.xml"
381381
382382
- name: Start livekit-server
383-
if: matrix.e2e-testing && (inputs.integration_repeat > 0 || inputs.run_stress_tests)
383+
if: >-
384+
matrix.e2e-testing &&
385+
(inputs.integration_repeat > 0 ||
386+
inputs.run_stress_tests ||
387+
(matrix.name == 'ubuntu-24.04-x64' && inputs.build_type == 'release'))
384388
id: livekit_server
385389
uses: livekit/dev-server-action@5d4d5337a875e2d1afd37bed03c601d159dab002 # v1.1.1
386390
with:
@@ -391,7 +395,11 @@ jobs:
391395
392396
# Needed by token helper script
393397
- name: Install livekit-cli
394-
if: matrix.e2e-testing && (inputs.integration_repeat > 0 || inputs.run_stress_tests)
398+
if: >-
399+
matrix.e2e-testing &&
400+
(inputs.integration_repeat > 0 ||
401+
inputs.run_stress_tests ||
402+
(matrix.name == 'ubuntu-24.04-x64' && inputs.build_type == 'release'))
395403
shell: bash
396404
env:
397405
# Windows installs lk via `gh api` / `gh release download`, which need this env var
@@ -448,6 +456,23 @@ jobs:
448456
--gtest_recreate_environments_when_repeating=1 \
449457
--gtest_output=xml:${{ env.BUILD_DIR }}/integration-test-results.xml
450458
459+
- name: Run memory tester
460+
if: matrix.name == 'ubuntu-24.04-x64' && inputs.build_type == 'release'
461+
timeout-minutes: 10
462+
shell: bash
463+
run: |
464+
set -euo pipefail
465+
source scripts/set-test-tokens.sh
466+
scripts/memory-tester.sh \
467+
--build-dir "${{ env.BUILD_DIR }}" \
468+
--scenario all \
469+
--iterations 100 \
470+
--warmup 20 \
471+
--max-rss-growth-mib 24 \
472+
--max-thread-growth 2 \
473+
--advisory-max-rss-growth-mib 8 \
474+
--advisory-max-thread-growth 0
475+
451476
- name: Run stress tests
452477
if: matrix.e2e-testing && inputs.run_stress_tests
453478
timeout-minutes: ${{ inputs.stress_timeout_minutes }}

‎docs/testing.md‎

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
# Testing
22

3-
The SDK includes integration and stress tests using
4-
[Google Test](https://github.com/google/googletest).
3+
The SDK includes unit, integration and stress tests using
4+
[Google Test](https://github.com/google/googletest). It also
5+
includes a memory-leak/resource checking tester.
6+
7+
Unit, integration, and memory tests are run on every PR in CI.
8+
Stress tests are reserved for nightly tests in CI due to their
9+
length.
10+
11+
## Test binaries
12+
13+
| Executable | Description |
14+
|------------|-------------|
15+
| `livekit_unit_tests` | Unit tests (no server required) |
16+
| `livekit_integration_tests` | Integration tests (server required) |
17+
| `livekit_stress_tests` | Long-running stress tests (server required) |
18+
| `livekit_memory_tester` | Resource leak tester |
519

620
## Building the test binaries
721

@@ -19,31 +33,23 @@ The SDK includes integration and stress tests using
1933

2034
## Running tests
2135

22-
After building, run tests using `ctest` or invoke the binaries directly:
23-
2436
```bash
25-
# Run all tests via ctest
26-
cd build-debug
37+
# Run all tests via ctest (warning, runs all GTest-based tests including stress)
38+
cd build-debug # or build-release
2739
ctest --output-on-failure
2840

2941
# Or run test executables directly
42+
./build-debug/bin/livekit_unit_tests
3043
./build-debug/bin/livekit_integration_tests
3144
./build-debug/bin/livekit_stress_tests
3245

3346
# Run specific test suites
47+
./build-debug/bin/livekit_unit_tests --gtest_filter="*Room*"
3448
./build-debug/bin/livekit_integration_tests --gtest_filter="*Rpc*"
3549
./build-debug/bin/livekit_stress_tests --gtest_filter="*MaxPayloadStress*"
3650
```
3751

38-
__Note:__ The tests require tokens and a running LiveKit server. See the section below for details.
39-
40-
## Test binaries
41-
42-
| Executable | Description |
43-
|------------|-------------|
44-
| `livekit_unit_tests` | Pure unit tests (no server required) |
45-
| `livekit_integration_tests` | Quick tests (~1-2 minutes) for SDK functionality |
46-
| `livekit_stress_tests` | Long-running tests (configurable, default 1 hour) |
52+
__Note:__ The integration and stress tests require tokens and a running LiveKit server. See the section below for details.
4753

4854
## Running a local LiveKit server for tests
4955

@@ -52,9 +58,10 @@ path is `livekit-server --dev`, which uses the well-known dev API
5258
key/secret (`devkey` / `secret`).
5359

5460
Install [`livekit-server`](https://docs.livekit.io/home/self-hosting/local/)
55-
and start it with data tracks enabled:
61+
and start it with data blobs enabled:
5662

5763
```bash
64+
LIVEKIT_CONFIG="enable_participant_data_blob: true"
5865
livekit-server --dev
5966
```
6067

@@ -177,7 +184,88 @@ cold runs even though C++ dependencies are reused.
177184
- **RPC**: round-trip calls, max payload (15 KB), timeouts, errors, concurrent calls.
178185
- **Stress**: high throughput, bidirectional RPC, memory pressure.
179186

180-
## Memory checks (valgrind)
187+
## Memory tester
188+
189+
> Note: Linux only
190+
191+
The memory tester binary catches resource leak regressions in an
192+
automated fashion, and is invoked in CI. This single binary runs individual
193+
or groups of scenarios:
194+
195+
- `audio-track` repeatedly initializes the SDK, connects, publishes an audio
196+
track, captures synthetic PCM frames, unpublishes, disconnects, and shuts down.
197+
- `video-track` repeatedly initializes the SDK, connects, publishes a video
198+
track, captures synthetic I420 frames, unpublishes, disconnects, and shuts down.
199+
- `data-track` repeatedly connects, publishes a data track, pushes synthetic
200+
payloads, unpublishes it, and disconnects.
201+
- `room-reuse` repeatedly connects and disconnects the same room object.
202+
- `room-client-leave` repeatedly connects, disconnects, and destroys a room.
203+
- `room-server-delete` repeatedly connects, publishes one synthetic video
204+
track, captures I420 frames without unpublishing, and uses `lk` to delete
205+
the local test room. It then verifies the disconnect and end-of-stream
206+
callbacks.
207+
- `capture-clock` repeatedly initializes the SDK, creates and publishes the
208+
GPU-rendered clock capture source, starts and stops its frame pump, joins its
209+
worker, unpublishes, disconnects, and shuts down. Each cycle must produce at
210+
least three frames, so the default 100-cycle run captures at least 300 frames
211+
(10 seconds of video at 30 fps).
212+
- `capture-pattern` exercises the same lifecycle with the GPU-rendered gradient
213+
pattern capture source.
214+
- `capture-gstreamer` exercises the same lifecycle through an entirely
215+
software-based GStreamer pipeline (`videotestsrc` and `vp8enc`), including
216+
encoded-frame ingest and WebRTC passthrough.
217+
218+
Each scenario reports process RSS and thread count at cycle 0, after warmup
219+
cycle 20, and after final cycle 100. Checkpoint sampling waits for the thread
220+
count to remain stable for 500 ms so short-lived teardown work does not cause a
221+
false failure. The gate applies to warmup-to-final growth. CI fails above its
222+
blocking limits of 24 MiB RSS or two threads, which protects against large
223+
customer-impacting regressions while allowing for allocator and dependency
224+
variation. It also emits a non-blocking advisory above 8 MiB RSS or zero
225+
threads. If threads grow, the tester reports when the final sustained growth
226+
began and groups the warmup and final threads by their Linux thread names.
227+
228+
The binary can be run directly, or via a helper script at `scripts/memory-tester.sh`,
229+
which invokes the binary but also has GitHub actions support for a results summary table.
230+
The helper defaults `MALLOC_ARENA_MAX` to `1` when launching each tester process.
231+
This limits `glibc`'s allocator arenas, reducing Linux thread-churn RSS noise, but it is
232+
not evidence that the default allocator returns all freed pages to the OS.
233+
234+
Start `livekit-server --dev`, install the `lk` CLI, and source the local test
235+
tokens before running any scenario:
236+
237+
```bash
238+
./build.sh release-tests --capture=all
239+
source scripts/set-test-tokens.sh
240+
scripts/memory-tester.sh --scenario audio-track \
241+
--iterations 100 --warmup 20 \
242+
--max-rss-growth-mib 24 --max-thread-growth 2 \
243+
--advisory-max-rss-growth-mib 8 --advisory-max-thread-growth 0
244+
scripts/memory-tester.sh --scenario video-track \
245+
--iterations 100 --warmup 20 \
246+
--max-rss-growth-mib 24 --max-thread-growth 2 \
247+
--advisory-max-rss-growth-mib 8 --advisory-max-thread-growth 0
248+
```
249+
250+
Use `--scenario all` to run all nine scenarios, each in a fresh tester process:
251+
252+
```bash
253+
scripts/memory-tester.sh --scenario data-track
254+
scripts/memory-tester.sh --scenario room-reuse
255+
scripts/memory-tester.sh --scenario room-client-leave
256+
scripts/memory-tester.sh --scenario room-server-delete
257+
scripts/memory-tester.sh --scenario capture-clock
258+
scripts/memory-tester.sh --scenario capture-pattern
259+
scripts/memory-tester.sh --scenario capture-gstreamer
260+
scripts/memory-tester.sh --scenario all
261+
```
262+
263+
The blocking and advisory limits are command-line options so a longer local run
264+
can use different budgets. When advisory limits are omitted, they default to the
265+
blocking limits. RSS growth thresholds are expressed in MiB and may be
266+
fractional, such as `--advisory-max-rss-growth-mib 0.5`.
267+
268+
## Additional memory checks (valgrind)
181269

182270
Run `valgrind` against the test binaries to check for memory leaks and other
183271
issues. See [tools.md](tools.md) for the recipe.

0 commit comments

Comments
 (0)