Skip to main content

fxprofiler

Create marker-only performance profiles in the Firefox Profiler. The profiling module allows for sampling based profiling, but no markers. Unfortunately it's not possible yet to do both sampling and markers in Python together. This package is designed for manually instrumenting python code with deterministic tracing markers or spans.

Define a marker type

A simple Text marker can be created to create an Instant marker at a specific point in time.

from dataclasses import dataclass
from fxprofiler import Marker, MarkerFormat, marker_field


@dataclass
class TextMarker(Marker):
    type_name = "Text"
    chart_label = "{marker.data.text}"
    table_label = "{marker.name} - {marker.data.text}"

    text: str = marker_field(label="Contents", format=MarkerFormat.UNIQUE_STRING)

Additional information can be collected through the marker_field mechannism.

from dataclasses import dataclass
from fxprofiler import (
    Category,
    CategoryColor,
    Marker,
    MarkerFormat,
    MarkerGraph,
    MarkerGraphType,
    marker_field,
)


@dataclass
class AllocationMarker(Marker):
    type_name = "custom"
    category = Category("Graphics", CategoryColor.GREEN)
    description = "Tracks a single allocation event."
    tooltip_label = "Allocation - {marker.data.url}"
    graphs = (MarkerGraph(key="latency_ms", type=MarkerGraphType.LINE),)

    event_name: str = marker_field(
        label="Event name", format=MarkerFormat.UNIQUE_STRING
    )
    allocation_size: int = marker_field(
        label="Allocation size", format=MarkerFormat.BYTES
    )
    url: str = marker_field(label="URL", format=MarkerFormat.URL)
    latency_ms: float = marker_field(label="Latency", format=MarkerFormat.DURATION)

Building a profile

In order to add markers, a full profile must be built. The profiles follow a thread and process model. Even if your system has a different abstraction, you can fit your data into these groupings. Threads are grouped by pid in the profiler UI. You can create synthetic PIDs to group threads.

from fxprofiler import Profile, MarkerTiming

profile = Profile(product="My Script", start_time_ms_since_epoch=0.0)
thread = profile.add_thread("Main Thread", pid=1, tid=1, is_main=True)

profile.add_marker(
    thread, "startup", MarkerTiming.instant(0.0), TextMarker(text="App launched")
)
profile.add_marker(
    thread,
    "allocate",
    MarkerTiming.interval(5.0, 42.0),
    AllocationMarker(
        event_name="alloc",
        allocation_size=512_000,
        url="https://example.com/",
        latency_ms=12.3,
    ),
)

Context managers for timing

measure() records an interval marker for the duration of the context block.

with profile.measure(thread, "render", TextMarker(text="Rendered frame")):
    render_frame()

thread() adds a thread that is alive for the duration of the context block.

import threading


def worker(profile):
    with profile.thread("Worker") as thread:
        with profile.measure(thread, "task", TextMarker(text="task")):
            do_work()


threading.Thread(target=worker, args=(profile,)).start()

Interval-start / interval-end pairs

If markers involve a duration, the API supports adding them with two different markers.

profile.add_marker(
    thread, "fetch", MarkerTiming.interval_start(2.0), TextMarker(text="fetch started")
)
# ... later, possibly after the end time wasn't known yet when the marker started ...
profile.add_marker(
    thread, "fetch", MarkerTiming.interval_end(30.0), TextMarker(text="fetch finished")
)

Writing and opening a profile

When integrating into your app, the profile can be saved and then served directly.

profile.write_json("profile.json")
from fxprofiler import show

server = show(profile)
input("Press Enter to stop serving...\n")
server.shutdown()

Examples

Runnable scripts that open profiles live in examples/:

task example -- markers

Development

All of the tasks can be run through Taskfile.

Releasing

Describe the release under ## Unreleased in CHANGELOG.md, then run:

task publish -- patch   # or minor / major

Metadata

Release files for fxprofiler 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fxprofiler 0.1.0
File Size Uploaded Provenance Actions
fxprofiler-0.1.0.tar.gz 20.1 kB Without provenance Details for fxprofiler-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fxprofiler 0.1.0
File Interpreter ABI Platform Actions
fxprofiler-0.1.0-py3-none-any.whl Python 3 none any Details for fxprofiler-0.1.0-py3-none-any.whl

Total release size: 36.6 kB

Release files / fxprofiler-0.1.0.tar.gz

Download URL fxprofiler-0.1.0.tar.gz
Size 20.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6ca15b80cf7e3cc1a80ef3da9b5d740ba321c888e92b57c8eb89cef833a9c164
BLAKE2b-256 checksum
How to use checksums
ce3c68d245da2fd63ed3cc111fee4d6d9c80744b4233227e6b76e7931ad5aa2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.12

Release files / fxprofiler-0.1.0-py3-none-any.whl

Download URL fxprofiler-0.1.0-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b9d5b0e306548c1805737394a6a2572e07afb192e6c86d24a409276cbf2b7767
BLAKE2b-256 checksum
How to use checksums
cf0dae458f32745999c21007a89e0cd0ce20c15b4ead4486e41d21b7bfca426e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.12

Security for fxprofiler 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page