Skip to content

Implement Chromium tracing in SDK and some benchmark code under tests#85

Merged
xianshijing-lk merged 13 commits intomainfrom
sxian/CLT-2717/add_chromium_tracing_and_benchmark_foundation
Apr 16, 2026
Merged

Implement Chromium tracing in SDK and some benchmark code under tests#85
xianshijing-lk merged 13 commits intomainfrom
sxian/CLT-2717/add_chromium_tracing_and_benchmark_foundation

Conversation

@xianshijing-lk
Copy link
Copy Markdown
Collaborator

Summary

  • Add Chrome trace format implementation with background file writer for performance profiling
  • Add benchmark utilities for analyzing trace files in tests
  • Add comprehensive unit tests for tracing system

Description

This PR introduces a tracing infrastructure based on Chrome's trace event format, enabling performance profiling and latency measurement across the LiveKit C++ SDK.

Tracing Implementation

The tracing system provides:

  • Background file writer: Events are queued and written asynchronously by a dedicated thread, minimizing latency impact on the main code path
  • Chrome trace format: Output is compatible with chrome://tracing and https://ui.perfetto.dev for visualization
  • Category filtering: Support for enabling specific trace categories with wildcard matching (e.g., livekit.*)
  • Cross-platform support: Works on Windows, macOS, and Linux

See tests traces examples in the screen shoot
Screenshot 2026-04-03 at 2 15 59 PM

Public API (include/livekit/tracing.h):
bool startTracing(const std::string& trace_file_path,
const std::vectorstd::string& categories = {});
void stopTracing();
bool isTracingEnabled();

Benchmark Utilities

New utilities in src/tests/benchmark/ for test instrumentation:

  • Trace file parsing: Load and parse Chrome trace JSON files
  • Duration calculation: Pair begin/end events (scoped B/E and async S/F)
  • Statistics: Calculate min, max, avg, p50, p95, p99 percentiles
  • BenchmarkSession: RAII wrapper for managing trace sessions in tests
  • TracedStressStats: Thread-safe statistics collector for stress tests

Unit Tests

17 unit tests covering:

  • Tracing lifecycle (start/stop, error handling)
  • Event writing (scoped, async, with arguments)
  • Category filtering and wildcard matching
  • Trace file parsing and analysis
  • Multiple sessions and thread safety

Test Plan

  • Unit tests pass on macOS: ./build.sh release-tests && ./build-release/bin/livekit_unit_tests
  • Unit tests pass on Linux: ./build.sh release-tests
  • Unit tests pass on Windows: build.cmd release-tests
  • Verify trace output opens in chrome://tracing or Perfetto UI

Tracing

Copy link
Copy Markdown
Contributor

@stephen-derosa stephen-derosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing this looks great! Definitely double check copyright year on new files.

Also, I ran the utests locally but i wasn't able to find the trace jsons. I would have thought they'd be in /tmp/ based on this line but i dont see them there or in my client-sdk-cpp repo

Comment thread include/livekit/tracing.h Outdated
@@ -0,0 +1,56 @@
/*
* Copyright 2024 LiveKit
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

year

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done with changing it to 2026 on all the relevant files.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still says 2024?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this file, now it should be fixed.

Comment thread src/trace/tracing.cpp
Comment thread src/tests/benchmark/benchmark_utils.h Outdated
Comment thread src/room.cpp
@xianshijing-lk
Copy link
Copy Markdown
Collaborator Author

Thanks for implementing this looks great! Definitely double check copyright year on new files.

Also, I ran the utests locally but i wasn't able to find the trace jsons. I would have thought they'd be in /tmp/ based on this line but i dont see them there or in my client-sdk-cpp repo

It is under the folder where you run the tests.

For instance, if you are running the tests like ./build-release/bin/livekit_integration_tests, it will be in your repo root folder, and I git ignore them to avoid corrupting the workspace.

@xianshijing-lk
Copy link
Copy Markdown
Collaborator Author

@stephen-derosa could you please take another look ?

Copy link
Copy Markdown
Contributor

@stephen-derosa stephen-derosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will applications have the ability to turn tracing on? if so, will there be an associated tracing example?

In general looks good to me! One comment about date and one about including in livekit.h

Comment thread include/livekit/livekit.h
#include "room.h"
#include "room_delegate.h"
#include "room_event_types.h"
#include "tracing.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for including this here if it doesnt get used?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The APIs at tracing.h is public:
bool startTracing(const std::string &trace_file_path,
const std::vectorstd::string &categories = {});
void stopTracing();
bool isTracingEnabled();

Developers can start / stop tracing based on their needs, by default it is stopped.

Comment thread include/livekit/tracing.h Outdated
@@ -0,0 +1,56 @@
/*
* Copyright 2024 LiveKit
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still says 2024?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great this should be useful in the cpp throughput tests

@xianshijing-lk
Copy link
Copy Markdown
Collaborator Author

will applications have the ability to turn tracing on? if so, will there be an associated tracing example?

In general looks good to me! One comment about date and one about including in livekit.h

Yes, that public interface is at tracing.h, they can call startTracing(const std::string &trace_file_path); to turn the tracing on.

All the tests now enable the tracing, and also it has its own unit tests.
If you want, I can update our examples to turn on the tracing. And I don't see the needs to have a dedicated example for tracing, as it is a debugging feature for our SDK.

@stephen-derosa
Copy link
Copy Markdown
Contributor

will applications have the ability to turn tracing on? if so, will there be an associated tracing example?
In general looks good to me! One comment about date and one about including in livekit.h

Yes, that public interface is at tracing.h, they can call startTracing(const std::string &trace_file_path); to turn the tracing on.

All the tests now enable the tracing, and also it has its own unit tests. If you want, I can update our examples to turn on the tracing. And I don't see the needs to have a dedicated example for tracing, as it is a debugging feature for our SDK.

no need to add an example then, maybe just a comment somewhere saying "to turn tracing on..."

@xianshijing-lk
Copy link
Copy Markdown
Collaborator Author

will applications have the ability to turn tracing on? if so, will there be an associated tracing example?
In general looks good to me! One comment about date and one about including in livekit.h

Yes, that public interface is at tracing.h, they can call startTracing(const std::string &trace_file_path); to turn the tracing on.
All the tests now enable the tracing, and also it has its own unit tests. If you want, I can update our examples to turn on the tracing. And I don't see the needs to have a dedicated example for tracing, as it is a debugging feature for our SDK.

no need to add an example then, maybe just a comment somewhere saying "to turn tracing on..."

Sounds good. I added a section in README.md to explain how to start / stop tracing.

BTW, I can't add the tracing to examples since we need to make a release first before changing the examples, otherwise things will break there.

@xianshijing-lk xianshijing-lk merged commit 65b3869 into main Apr 16, 2026
13 of 14 checks passed
@xianshijing-lk xianshijing-lk deleted the sxian/CLT-2717/add_chromium_tracing_and_benchmark_foundation branch April 16, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants