# longxinh/xhprof architecture

> Architecture diagram of the GitHub repository longxinh/xhprof, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/longxinh/xhprof
- Repository: https://github.com/longxinh/xhprof

## Overview

XHProf profiles PHP execution through a native extension, stores run data, then presents hierarchical reports in a browser. The main path is enable/profile, stop and obtain metrics, persist a run, and inspect it; reports also support run comparisons, aggregation, call graphs, and function lookup. The README documents sampling and optional SQL/cURL trace details. Some subsystem links rely on documented capabilities and filenames because their implementations were not sampled.

Main entry points: [`extension/xhprof.c`](https://github.com/longxinh/xhprof/blob/master/extension/xhprof.c) (Profiler Extension), [`xhprof_html/index.php`](https://github.com/longxinh/xhprof/blob/master/xhprof_html/index.php) (Report Entry), [`xhprof_lib/utils/xhprof_runs.php`](https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/xhprof_runs.php) (Run Storage API), [`xhprof_html/callgraph.php`](https://github.com/longxinh/xhprof/blob/master/xhprof_html/callgraph.php) (Call Graph)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_capture["Profiling"]
  node_xhprof_ext["Profiler Extension<br/>[xhprof.c]"]
  node_profile_metrics["Profile Metrics<br/>[xhprof.c]"]
end

subgraph group_storage["Run Data"]
  node_run_writer["Run Storage API<br/>[xhprof_runs.php]"]
  node_run_store[("Run Files")]
end

subgraph group_analysis["Analysis"]
  node_metric_analysis["Metric Analysis<br/>[xhprof_lib.php]"]
  node_callgraph["Call Graph<br/>[callgraph.php]"]
  node_graph_utils["Graph Utilities"]
end

subgraph group_presentation["Report UI"]
  node_report_entry["Report Entry<br/>[index.php]"]
  node_report_display["Report Renderer<br/>[xhprof.php]"]
  node_typeahead["Function Lookup<br/>[typeahead.php]"]
  node_typeahead_ui["Lookup UI"]
end

node_php_app(("PHP Application"))
node_sql_service(("SQL Workload"))
node_curl_service(("HTTP Workload"))
node_browser(("Browser User"))

node_php_app -->|"enables profiling"| node_xhprof_ext
node_xhprof_ext -->|"collects metrics"| node_profile_metrics
node_php_app -->|"stops profiling"| node_xhprof_ext
node_xhprof_ext -->|"returns metrics"| node_php_app
node_xhprof_ext -->|"provides run data"| node_run_writer
node_run_writer -->|"writes runs"| node_run_store
node_run_store -->|"reads runs"| node_run_writer
node_browser -->|"requests report"| node_report_entry
node_report_entry -->|"loads runs"| node_run_writer
node_report_entry -->|"renders report"| node_report_display
node_report_display -->|"analyzes metrics"| node_metric_analysis
node_report_entry -->|"requests graph"| node_callgraph
node_callgraph -->|"builds graph"| node_graph_utils
node_browser -->|"requests lookup"| node_typeahead
node_typeahead -->|"uses lookup logic"| node_typeahead_ui
node_xhprof_ext -.->|"traces SQL calls"| node_sql_service
node_xhprof_ext -.->|"traces HTTP calls"| node_curl_service

click node_xhprof_ext "https://github.com/longxinh/xhprof/blob/master/extension/xhprof.c"
click node_profile_metrics "https://github.com/longxinh/xhprof/blob/master/extension/xhprof.c"
click node_run_writer "https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/xhprof_runs.php"
click node_report_entry "https://github.com/longxinh/xhprof/blob/master/xhprof_html/index.php"
click node_report_display "https://github.com/longxinh/xhprof/blob/master/xhprof_lib/display/xhprof.php"
click node_metric_analysis "https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/xhprof_lib.php"
click node_callgraph "https://github.com/longxinh/xhprof/blob/master/xhprof_html/callgraph.php"
click node_graph_utils "https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/callgraph_utils.php"
click node_typeahead "https://github.com/longxinh/xhprof/blob/master/xhprof_html/typeahead.php"
click node_typeahead_ui "https://github.com/longxinh/xhprof/blob/master/xhprof_lib/display/typeahead_common.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_xhprof_ext,node_profile_metrics,node_browser toneBlue
class node_run_writer,node_run_store toneAmber
class node_metric_analysis,node_callgraph,node_graph_utils toneMint
class node_report_entry,node_report_display,node_typeahead,node_typeahead_ui toneRose
class node_php_app,node_sql_service,node_curl_service toneIndigo
```

## Components

### Profiling

- [Profiler Extension](https://github.com/longxinh/xhprof/blob/master/extension/xhprof.c): `extension/xhprof.c`
- [Profile Metrics](https://github.com/longxinh/xhprof/blob/master/extension/xhprof.c): `extension/xhprof.c`

### Run Data

- [Run Storage API](https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/xhprof_runs.php): `xhprof_lib/utils/xhprof_runs.php`
- **Run Files**

### Analysis

- [Metric Analysis](https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/xhprof_lib.php): `xhprof_lib/utils/xhprof_lib.php`
- [Call Graph](https://github.com/longxinh/xhprof/blob/master/xhprof_html/callgraph.php): `xhprof_html/callgraph.php`
- [Graph Utilities](https://github.com/longxinh/xhprof/blob/master/xhprof_lib/utils/callgraph_utils.php): `xhprof_lib/utils/callgraph_utils.php`

### Report UI

- [Report Entry](https://github.com/longxinh/xhprof/blob/master/xhprof_html/index.php): `xhprof_html/index.php`
- [Report Renderer](https://github.com/longxinh/xhprof/blob/master/xhprof_lib/display/xhprof.php): `xhprof_lib/display/xhprof.php`
- [Function Lookup](https://github.com/longxinh/xhprof/blob/master/xhprof_html/typeahead.php): `xhprof_html/typeahead.php`
- [Lookup UI](https://github.com/longxinh/xhprof/blob/master/xhprof_lib/display/typeahead_common.php): `xhprof_lib/display/typeahead_common.php`

### Other components

- **PHP Application**
- **SQL Workload**
- **HTTP Workload**
- **Browser User**

## Connections

- PHP Application → Profiler Extension: enables profiling
- Profiler Extension → Profile Metrics: collects metrics
- PHP Application → Profiler Extension: stops profiling
- Profiler Extension → PHP Application: returns metrics
- Profiler Extension → Run Storage API: provides run data
- Run Storage API → Run Files: writes runs
- Run Files → Run Storage API: reads runs
- Browser User → Report Entry: requests report
- Report Entry → Run Storage API: loads runs
- Report Entry → Report Renderer: renders report
- Report Renderer → Metric Analysis: analyzes metrics
- Report Entry → Call Graph: requests graph
- Call Graph → Graph Utilities: builds graph
- Browser User → Function Lookup: requests lookup
- Function Lookup → Lookup UI: uses lookup logic
- Profiler Extension → SQL Workload: traces SQL calls
- Profiler Extension → HTTP Workload: traces HTTP calls
