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

A profile can be saved to disk:

profile.write_json("profile.json")

Or served directly instead:

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.2.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.2.0
File Size Uploaded Provenance Actions
fxprofiler-0.2.0.tar.gz 20.0 kB Without provenance Details for fxprofiler-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 36.5 kB

Release files / fxprofiler-0.2.0.tar.gz

Download URL fxprofiler-0.2.0.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
245ebe6fedca542ce487a31040153524746f71b29e271b614760172f1c66ccf5
BLAKE2b-256 checksum
How to use checksums
7dd51ffbe7c6e1ff3c071ba2b953dbfb0979c44fdf5562ab997befe9422dd935
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.12

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

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

Security for fxprofiler 0.2.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